The 3D Target game is programmed in Arduino C, which is a flavor of C++. It can be downloaded here. Read it below.
#include <Stepper.h>
//This is where we establish the motor objects
//Stepper stepperA(48, 18, 19, 20, 21);
//Stepper stepperC(48, 11, 10, 9, 8);
Stepper stepperB(48, 4, 5, 6, 7);
Stepper stepperA(48, 21, 20, 19, 18);
//Stepper stepperB(48, 7, 6, 5, 4);
Stepper stepperC(48, 8, 9, 10, 11);
//This is where we setup I/O pins
//Pins for Control Panel
const int upPin = 33;
const int leftPin = 27;
const int rightPin = 29;
const int downPin = 31;
const int startPin = 23;
const int dropPin = 25;
// Pins for Fuel Gauge and Display
const int ShiftRegisterClock = 52;
const int ShiftRegisterReset = 50;
const int GameOver = 48;
const int CounterChipClock = 46;
const int CounterChipEnable = 44;
const int CounterChipReset = 42;
// Pins for Targets
const int CurrentTarget = 39;
const int KioskLED1 = 49;
const int KioskLED2 = 51;
const int KioskLED3 = 53;
//This is where we setup global variables
int origin[] = {125 * 16, 90 * 16}; //stepper C is at (x, 0), B is at (0, 0), A is at (0, y)
int xyz[] = {532,420,91*16};
int xyzNext[] = {532,420,91*16};
int len[3] = {0,0,0};
long fuelDelay = 0;
int KioskButtonCounter = 0;
int CurrentLED = 0;
int CurrentTargetState = 0;
int stepperSpeed = 100;
//===========================================================//
void setup(){
//Motor speeds
stepperA.setSpeed(stepperSpeed);
stepperB.setSpeed(stepperSpeed);
stepperC.setSpeed(stepperSpeed);
//Serial Output
Serial.begin(9600);
Serial.println("Test line");
Serial.println("Ready to start!");
//Joystick Pins
pinMode(upPin, INPUT);
pinMode(downPin, INPUT);
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(startPin, INPUT);
pinMode(dropPin, INPUT);
//Display and fuel gauge Pins
pinMode(ShiftRegisterClock, OUTPUT);
pinMode(ShiftRegisterReset, OUTPUT);
pinMode(GameOver, INPUT);
pinMode(CounterChipClock, OUTPUT);
pinMode(CounterChipEnable, OUTPUT);
pinMode(CounterChipReset, OUTPUT);
//Target Kiosk Pins
pinMode(CurrentTarget, INPUT);
pinMode(KioskLED1, OUTPUT);
pinMode(KioskLED2, OUTPUT);
pinMode(KioskLED3, OUTPUT);
endGame();
}
void loop(){
start(); //This is tested, it works. -Jake
calibrate();
gameLoop();
endGame();
}
//===========================================================//
//Game structure functions
void start(){
Serial.println("Press green to go");
int started = digitalRead(startPin);
while(started == 0){
started = digitalRead(startPin);
//Serial.println(started);
}
Serial.println("Started!");
while(digitalRead(GameOver) == 0){
Serial.print(digitalRead(GameOver));
useFuel();
delay(50);
}
digitalWrite(ShiftRegisterReset, HIGH);
delay(50);
digitalWrite(ShiftRegisterReset, LOW);
digitalWrite(CounterChipReset, HIGH); //reset score
delay(50);
digitalWrite(CounterChipReset, LOW);
randomSeed(analogRead(0));
pickNextTarget();
//scorePlusOne();
}
void calibrate(){
digitalWrite(ShiftRegisterReset, HIGH);
delay(50);
digitalWrite(ShiftRegisterReset, LOW);
}
void gameLoop(){
int noTimeLeft = LOW;
while(noTimeLeft == LOW){
FuelTimer();
for(; fuelDelay > 0; fuelDelay--){
joystick(); //This is tested, this works. I swapped the axes to reflect our lab setup. -Jake
//stepTo(); //This is identical to drawbot, but may need adjustments for accuracy. Also I added bounds to xyzNext. -Jake
drop(); //This has to be tested with the target stop. It works now. - Jake
delay(5);
// Serial.print(digitalRead(41));
if(digitalRead(41)==HIGH){
stepperA.step(1);
stepperB.step(1);
stepperC.step(1);
}
}
noTimeLeft = digitalRead(GameOver);
useFuel();
//Serial.println(fuelDelay);
}
}
void endGame(){ //this works - Jake
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(21,LOW);
digitalWrite(20,LOW);
digitalWrite(19,LOW);
digitalWrite(18,LOW);
KioskButtonCounter = 0;
Serial.println("Score has been reset");
Serial.println("Score is now: ");
Serial.println(KioskButtonCounter); //check if score has been reset
digitalWrite(CounterChipEnable, LOW); //blink 3 times
delay (300);
digitalWrite(CounterChipEnable, HIGH);
delay (300);
digitalWrite(CounterChipEnable, LOW);
delay (300);
digitalWrite(CounterChipEnable, HIGH);
delay (300);
digitalWrite(CounterChipEnable, LOW);
delay (300);
digitalWrite(CounterChipEnable, HIGH);
digitalWrite(ShiftRegisterReset, HIGH);
delay(50);
digitalWrite(ShiftRegisterReset, LOW);
delay (500); //wait a bit
Serial.println("Game Over");
delay(500);
}
//===========================================================//
//Movement functions
void joystick(){
int UpMotion = digitalRead(upPin); //digitalRead reads input from the switch/button as either a "1" or "0" and stores into variable
int LeftMotion = digitalRead(leftPin); //1= on 0= off
int RightMotion= digitalRead(rightPin);
int DownMotion= digitalRead(downPin);
if (UpMotion == 1 && RightMotion != 1 && LeftMotion != 1){ //Move UP
Serial.print("Up...");
//delay(10);
stepperA.step(-1);
stepperB.step(1);
stepperC.step(1);
xyzNext[1]--;
}
if (LeftMotion == 1 && UpMotion != 1 && DownMotion != 1){ // Move LEFT
Serial.print("Left...");
//delay(10);
if (fuelDelay % 2 == 1){
stepperA.step(1);
stepperB.step(1);
stepperC.step(-1);
}
else{
stepperC.step(-1);
}
xyzNext[0]--;
}
if (RightMotion == 1 && UpMotion != 1 && DownMotion != 1){ // Move RIGHT
Serial.print("Right..");
//delay(10);
if (fuelDelay % 2 == 1){
stepperA.step(-1);
stepperB.step(-1);
stepperC.step(1);
}
else{
stepperC.step(1);
}
xyzNext[0]++;
}
if (DownMotion == 1 && RightMotion != 1 && LeftMotion != 1){ // Move DOWN
Serial.print("Down...");
// //delay(10);
if (fuelDelay % 2 == 1){
stepperA.step(1);
stepperB.step(-1);
stepperC.step(-1);
}
else{
stepperA.step(1);
}
xyzNext[1]++;
}
}
void drop(){
int dropped = digitalRead(dropPin);
int i;
if (dropped == 1){
Serial.println("This is where our drop routine goes");
for(i = 0; i < 550 && CurrentTargetState == LOW; i++){
stepperA.step(-1);
stepperB.step(-1);
stepperC.step(-1);
CurrentTargetState = digitalRead(CurrentTarget);
Serial.print(CurrentTargetState);
}
//Serial.println(i);
//Serial.println("Rising");
for(i = i - 10; i > 0; i--){
stepperA.step(1);
stepperB.step(1);
stepperC.step(1);
//Serial.print("d");
}
if(CurrentTargetState == 1){
pickNextTarget();
scorePlusOne();
CurrentTargetState = LOW;
}
}
}
//===========================================================//
//Score related functions
void scorePlusOne(){
KioskButtonCounter++;
Serial.print("Score: ");
Serial.println(KioskButtonCounter);
digitalWrite(CounterChipClock, HIGH);
delay(50);
digitalWrite(CounterChipClock, LOW);
//delay(50)
digitalWrite(ShiftRegisterReset, HIGH);
delay(50);
digitalWrite(ShiftRegisterReset, LOW);
}
void useFuel(){
digitalWrite(ShiftRegisterClock, HIGH);
// delay(50);
digitalWrite(ShiftRegisterClock, LOW);
//delay(500)
}
//void FuelTimer(){
// fuelDelay = 200;
//}
void FuelTimer() { //loop for fuel gauge speed
if (KioskButtonCounter <= 5) {
fuelDelay = (200 - 10*KioskButtonCounter);
}
if (KioskButtonCounter >= 6 && KioskButtonCounter <= 20) {
fuelDelay = (150 - 5*KioskButtonCounter);
}
if (KioskButtonCounter >=21 && KioskButtonCounter <=35) {
fuelDelay = (100 - 2.5*KioskButtonCounter);
}
if (KioskButtonCounter > 35) {
fuelDelay = 30;
}
}
void pickNextTarget(){
digitalWrite(CurrentLED, LOW);
delay(100);
CurrentLED = 53 - 2*random(0,3);
digitalWrite(CurrentLED, HIGH);
}