Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SPS Arduino Code
//©UCLA-SPS 2016
START
{
Boolean rand1;
Boolean rand2;
Boolean rand3;
Boolean rand4;
Boolean rand5;
Boolean ip1==false;
Boolean ip2==false;
Boolean ip3==false;
Boolean ip4==false;
Boolean ip5==false;
//here, boolean rand 1-5 are random boolean values which change every cycle
//here, boolean ip 1-5 hold the value of the button presses.
Integer n=(user input)
//n decides the number of cycles the game runs for
Integer counter;
//counter counts the total number of times the mole was successfully whacked
Integer life;
//life stores the number of lives the player has lost
Boolean garbage==true;
while(garbage==true)
{
//get 5 random states for the boolean variables rand 1-5
if(rand1==true)
{
Light1 is on;
}
if(rand2==true)
{
Light2 is on;
}
if(rand3==true)
{
Light3 is on;
}
if(rand4==true)
{
Light4 is on;
}
if(rand5==true)
{
Light5 is on;
}
//take input from ip 1-5 (store as true if button is pressed)
Delay(500 cycles);
if(ip1==rand1)
{
counter++;
}
else if(ip1!=rand1)
{
life++;
}
if(ip2==rand2)
{
counter++;
}
else if(ip2!=rand2)
{
life++;
}
if(ip3==rand3)
{
counter++;
}
else if(ip3!=rand3)
{
life++;
}
if(ip4==rand4)
{
counter++;
}
else if(ip4!=rand4)
{
life++;
}
if(ip5==rand5)
{
counter++;
}
else if(ip5!=rand5)
{
life++;
}
if(life>=20)
{
TERMINATE();
}
}
Display(counter);
Arduino code-
int D4 = 4; // the number of the pin, so 4 for D4
int D5 = 5;
int D6 = 6;
int D7 = 7;
int D8 = 8;
/*
int A1 = 1; // A1 is also
int A2 = 2;
int A3 = 3;
int A4 = 4;
int A5 = 5;
*/
void setup ()
{
pinMode(D4,OUTPUT);
pinMode(D5,OUTPUT);
pinMode(D6,OUTPUT);
pinMode(D7,OUTPUT);
pinMode(D8,OUTPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);
pinMode(A5,INPUT);
randomSeed(analogRead(0));
}
void loop()
{
boolean control=true;
int life=0;
int score=0;
while (control==true)
{
int ip_arr[5]={0,0,0,0,0}; // input array starts at all off
int rand_arr[5];
for (int i=0; i<5; i++)
{
rand_arr[i]=random(1);
}
if(rand_arr[0]==1)
{
digitalWrite(D4,HIGH);
}
if(rand_arr[1]==1)
{
digitalWrite(D5,HIGH);
}
if(rand_arr[2]==1)
{
digitalWrite(D6,HIGH);
}
if(rand_arr[3]==1)
{
digitalWrite(D7,HIGH);
}
if(rand_arr[4]==1)
{
digitalWrite(D8,HIGH);
}
for (int i=0; i<500; i++)
{
int v1=digitalRead(A5);
int v2=digitalRead(A4);
int v3=digitalRead(A3);
int v4=digitalRead(A2);
int v5=digitalRead(A1);
if (v1==1)
{
ip_arr[0]=v1;
}
if (v2==1)
{
ip_arr[1]=v2;
}
if (v3==1)
{
ip_arr[2]=v3;
}
if (v4==1)
{
ip_arr[3]=v4;
}
if (v5==1)
{
ip_arr[4]=v5;
}
delay(1);
}
for(int i=0; i<5; i++)
{
if (ip_arr[i]==rand_arr[i])
{
score++;
}
else if(ip_arr[i]!=rand_arr[i])
{
life++;
}
}
if (life==20)
{
control=false;
}
}
control=true;
while (control==true)
{
digitalWrite(D4,HIGH);
digitalWrite(D5,HIGH);
digitalWrite(D6,HIGH);
digitalWrite(D7,HIGH);
digitalWrite(D8,HIGH);
delay(10);
digitalWrite(D4,LOW);
digitalWrite(D5,LOW);
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
digitalWrite(D8,LOW);
delay(10);
}
}
Arduino Code v2
// Global Constants
const int D1 = 1; // the number of the pin, so 4 for D1
const int D2 = 2;
const int D3 = 3;
const int D4 = 4;
const int D5 = 5;
// Global Variable
int digpin_arr[5] = {D1, D2, D3, D4, D5}; // Although variable, this should not change. Listed from 8 to 4 to correspond to order on breadboard.
// This runs one time only, during startup or after resets.
void setup ()
{
Serial.begin(9600);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
randomSeed(analogRead(A0)); // A0 is unconnected and should be random.
Serial.println("#########################");
Serial.println("Welcome to Whack-A-Mole!!");
Serial.println("#########################");
}
// This is supposed to loop until it is turned off or reset.
void loop()
{
int life = 20;
int score = 0;
Serial.print(" Starting score = "); // Display starting score and life.
Serial.println(score);
Serial.print("Starting life = ");
Serial.println(life);
boolean control = true;
while (control == true)
{
int ip_arr[5] = {0,0,0,0,0}; // input array starts at all off
int rand_arr[5];
// For loop for randomizing the LEDs.
for (int i = 0; i < 5; i++)
{
rand_arr[i] = (int) random(0,2);
if(rand_arr[i] == 1)
{
digitalWrite(digpin_arr[i], HIGH);
}
else if (rand_arr[i] == 0)
{
digitalWrite(digpin_arr[i], LOW);
}
}
// Block for debugging only
Serial.print("rand_arr = ");
for(int i=0;i<5;i++)
{
Serial.print(rand_arr[i]);
}
Serial.println();
delay(1);
for (int i = 0; i<3000; i++) // 3 second window to hit the buttons
{
int v1 = digitalRead(A1);
int v2 = digitalRead(A2);
int v3 = digitalRead(A3);
int v4 = digitalRead(A4);
int v5 = digitalRead(A5);
int vread[5]={v1, v2, v3, v4, v5};
for (int i = 0; i < 5; i++)
{
if (vread[i] == 1)
{
ip_arr[i] = vread[i];
}
}
delay(1); // Okay if a button press is longer than 1 ms.
}
// Block for debugging only
Serial.print("ip_arr = ");
for(int i=0;i<5;i++)
{
Serial.print(ip_arr[i]);
}
Serial.println();
delay(1);
for (int i = 0; i < 5; i++) // Checks how correct the input during the window was
{
if ((ip_arr[i] == rand_arr[i]) && (ip_arr[i] == 1))
{
score++;
}
else if(ip_arr[i] != rand_arr[i])
{
life--;
}
// resets ip_arr
ip_arr[i] = 0;
}
Serial.println("#########################");
Serial.print("score = "); // Displays score and life after (not during) the cycle, they can still gain score w/ negative life.
Serial.println(score);
Serial.print("life = ");
Serial.println(life);
if (life <= 0)
{
control = false;
Serial.println("#########################");
Serial.println("####### GAME OVER #######");
Serial.println("#########################");
Serial.print("Your final score is ");
Serial.print(score);
Serial.println(".");
}
}
while(!control)
{
for(int i = 0; i < 5; i++)
{
digitalWrite(digpin_arr[i], HIGH);
delay(500);
digitalWrite(digpin_arr[i], LOW);
delay(500);
}
}
}
Button Test
// Global Constants
const int D1 = 1; // the number of the pin, so 4 for D1
const int D2 = 2;
const int D3 = 3;
const int D4 = 4;
const int D5 = 5;
// Global Variable
int digpin_arr[5] = {D1, D2, D3, D4, D5}; // Although variable, this should not change. Listed from 8 to 4 to correspond to order on breadboard.
// This runs one time only, during startup or after resets.
void setup ()
{
Serial.begin(9600);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
randomSeed(analogRead(A0)); // A0 is unconnected and should be random.
Serial.println("#########################");
Serial.println("Welcome to Whack-A-Mole!!");
Serial.println("#########################");
}
// This is supposed to loop until it is turned off or reset.
void loop()
{
int vread[] = {0, 0, 0, 0, 0};
int v1 = digitalRead(A1);
int v2 = digitalRead(A2);
int v3 = digitalRead(A3);
int v4 = digitalRead(A4);
int v5 = digitalRead(A5);
int temp[]={v1, v2, v3, v4, v5};
for (int i = 0; i < 5; i++)
{
vread[i] = temp[i];
Serial.print(vread[i]);
vread[i] = 0;
}
Serial.println();
delay(500); // Okay if a button press is longer than 1 ms.
}
On Off Test
void setup() {
// put your setup code here, to run once:
pinMode(1, OUTPUT);
/* pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);*/
}
void loop() {
digitalWrite (1,HIGH);
delay(1000);
digitalWrite (1,LOW);
delay(1000);
}
Code that is on 123D Circuits as of 5/17/2016:
// Global Constants
const int D4 = 4; // the number of the pin, so 4 for D4
const int D5 = 5;
const int D6 = 6;
const int D7 = 7;
const int D8 = 8;
// Global Variable
int digpin_arr[5] = {D8, D7, D6, D5, D4}; // Although variable, this should not change. Listed from 8 to 4 to correspond to order on breadboard.
// This runs one time only, during startup or after resets.
void setup ()
{
Serial.begin(9600);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
randomSeed(analogRead(A0)); // A0 is unconnected and should be random.
Serial.println("#########################");
Serial.println("Welcome to Whack-A-Mole!!");
Serial.println("#########################");
}
// This is supposed to loop until it is turned off or reset.
void loop()
{
int life = 20;
int score = 0;
Serial.print("score = "); // Display starting score and life.
Serial.println(score);
Serial.print("life = ");
Serial.println(life);
boolean control = true;
while (control == true)
{
int ip_arr[5] = {0, 0, 0, 0, 0}; // input array starts at all off
int rand_arr[5];
// For loop for randomizing the LEDs.
for (int i = 0; i < 5; i++)
{
rand_arr[i] = (int) random(0,2);
if(rand_arr[i] == 1)
{
digitalWrite(digpin_arr[i], HIGH);
}
else if (rand_arr[i] == 0)
{
digitalWrite(digpin_arr[i], LOW);
}
}
/*
// Block for debugging only
Serial.print("rand_arr = ");
for(int i=0;i<5;i++)
{
Serial.print(rand_arr[i]);
}
Serial.println();
delay(500);
*/
for (int i = 0; i<3000; i++) // 3 second window to hit the buttons
{
int v1 = digitalRead(A1);
int v2 = digitalRead(A2);
int v3 = digitalRead(A3);
int v4 = digitalRead(A4);
int v5 = digitalRead(A5);
int vread[5]={v1, v2, v3, v4, v5};
for (int i = 0; i < 5; i++)
{
if (vread[i] == 1)
{
ip_arr[i] = vread[i];
}
}
delay(1); // Okay if a button press is longer than 1 ms.
}
for (int i = 0; i < 5; i++) // Checks how correct the input during the window was
{
if (ip_arr[i] == rand_arr[i] && ip_arr[i] == 1)
{
score++;
}
else if(ip_arr[i] != rand_arr[i])
{
life--;
}
}
Serial.println("#########################");
Serial.print("score = "); // Displays score and life after (not during) the cycle, they can still gain score w/ negative life.
Serial.println(score);
Serial.print("life = ");
Serial.println(life);
if (life <= 0)
{
control = false;
}
}
Serial.println("#########################");
Serial.println("####### GAME OVER #######");
Serial.println("#########################");
Serial.print("Your final score is ");
Serial.print(score);
Serial.println(".");
boolean startover = false;
// You can ask to start over and make "startover=true".
// For example, set 1st button status to off and ask to hit 1st button to start over.
while (startover != true)
{
digitalWrite(D4,HIGH);
digitalWrite(D5,HIGH);
digitalWrite(D6,HIGH);
digitalWrite(D7,HIGH);
digitalWrite(D8,HIGH);
delay(300);
digitalWrite(D4,LOW);
digitalWrite(D5,LOW);
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
digitalWrite(D8,LOW);
delay(300);
}
}
123D Circuits Code (pins differ)
// Global Constants
const int D4 = 5; // the number of the pin, so 4 for D4
const int D5 = 4;
const int D6 = 3;
const int D7 = 2;
const int D8 = 1;
// Global Variable
int digpin_arr[5] = {D8, D7, D6, D5, D4}; // Although variable, this should not change. Listed from 8 to 4 to correspond to order on breadboard.
// This runs one time only, during startup or after resets.
void setup ()
{
Serial.begin(9600);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
randomSeed(analogRead(A0)); // A0 is unconnected and should be random.
Serial.println("#########################");
Serial.println("Welcome to Whack-A-Mole!!");
Serial.println("#########################");
}
// This is supposed to loop until it is turned off or reset.
void loop()
{
int life = 20;
int score = 0;
Serial.print("score = "); // Display starting score and life.
Serial.println(score);
Serial.print("life = ");
Serial.println(life);
boolean control = true;
while (control == true)
{
int ip_arr[5] = {0, 0, 0, 0, 0}; // input array starts at all off
int rand_arr[5];
// For loop for randomizing the LEDs.
for (int i = 0; i < 5; i++)
{
rand_arr[i] = (int) random(0,2);
if(rand_arr[i] == 1)
{
digitalWrite(digpin_arr[i], HIGH);
}
else if (rand_arr[i] == 0)
{
digitalWrite(digpin_arr[i], LOW);
}
}
/*
// Block for debugging only
Serial.print("rand_arr = ");
for(int i=0;i<5;i++)
{
Serial.print(rand_arr[i]);
}
Serial.println();
delay(500);
*/
for (int i = 0; i<3000; i++) // 3 second window to hit the buttons
{
int v1 = digitalRead(A1);
int v2 = digitalRead(A2);
int v3 = digitalRead(A3);
int v4 = digitalRead(A4);
int v5 = digitalRead(A5);
int vread[5]={v1, v2, v3, v4, v5};
for (int i = 0; i < 5; i++)
{
if (vread[i] == 1)
{
ip_arr[i] = vread[i];
}
}
delay(1); // Okay if a button press is longer than 1 ms.
}
for (int i = 0; i < 5; i++) // Checks how correct the input during the window was
{
if (ip_arr[i] == rand_arr[i] && ip_arr[i] == 1)
{
score++;
}
else if(ip_arr[i] != rand_arr[i])
{
life--;
}
}
Serial.println("#########################");
Serial.print("score = "); // Displays score and life after (not during) the cycle, they can still gain score w/ negative life.
Serial.println(score);
Serial.print("life = ");
Serial.println(life);
if (life <= 0)
{
control = false;
}
}
Serial.println("#########################");
Serial.println("####### GAME OVER #######");
Serial.println("#########################");
Serial.print("Your final score is ");
Serial.print(score);
Serial.println(".");
boolean startover = false;
// You can ask to start over and make "startover=true".
// For example, set 1st button status to off and ask to hit 1st button to start over.
while (startover != true)
{
digitalWrite(D4,HIGH);
digitalWrite(D5,HIGH);
digitalWrite(D6,HIGH);
digitalWrite(D7,HIGH);
digitalWrite(D8,HIGH);
delay(300);
digitalWrite(D4,LOW);
digitalWrite(D5,LOW);
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
digitalWrite(D8,LOW);
delay(300);
}
}
Final Code on Teensy:
// Global Constants
const int D4 = 5; // the number of the pin, so 4 for D4
const int D5 = 4;
const int D6 = 3;
const int D7 = 2;
const int D8 = 1;
// Global Variable
int digpin_arr[5] = {D8, D7, D6, D5, D4}; // Although variable, this should not change. Listed from 8 to 4 to correspond to order on breadboard.
// This runs one time only, during startup or after resets.
void setup ()
{
Serial.begin(9600);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
randomSeed(analogRead(A0)); // A0 is unconnected and should be random.
delay(1000);
Serial.println("#########################");
Serial.println("Welcome to Whack-A-Mole!!");
Serial.println("#########################");
}
// This is supposed to loop until it is turned off or reset.
void loop()
{
int life = 20;
int score = 0;
Serial.print("score = "); // Display starting score and life.
Serial.println(score);
Serial.print("life = ");
Serial.println(life);
boolean control = true;
while (control == true)
{
int ip_arr[5] = {0, 0, 0, 0, 0}; // input array starts at all off
Serial.print("starting ip_arr is ");
for (int i = 0; i< 5; i++)
{
Serial.print(ip_arr[i]);
}
Serial.println();
int rand_arr[5];
// For loop for randomizing the LEDs.
for (int i = 0; i < 5; i++)
{
rand_arr[i] = (int) random(0,2);
if(rand_arr[i] == 1)
{
digitalWrite(digpin_arr[i], HIGH);
}
else if (rand_arr[i] == 0)
{
digitalWrite(digpin_arr[i], LOW);
}
}
// Block for debugging only
Serial.print("rand_arr = ");
for(int i=0;i<5;i++)
{
Serial.print(rand_arr[i]);
}
Serial.println();
//delay(500);
for (int i = 0; i<1000; i++) // 3 second window to hit the buttons
{
int vread[5] = {0, 0, 0, 0, 0};
int v1 = digitalRead(A1);
int v2 = digitalRead(A2);
int v3 = digitalRead(A3);
int v4 = digitalRead(A4);
int v5 = digitalRead(A5);
int varr[5]={v1, v2, v3, v4, v5};
for (int i = 0; i < 5; i++)
{
vread[i] = varr[i];
if (vread[i]==1)
{
ip_arr[i] = vread[i];
}
}
delay(1); // Okay if a button press is longer than 1 ms.
}
// Block for debugging only
Serial.print("ip_arr = ");
for(int i=0;i<5;i++)
{
Serial.print(ip_arr[i]);
Serial.print(" ");
}
Serial.println();
delay(1);
for (int i = 0; i < 5; i++) // Checks how correct the input during the window was
{
if (ip_arr[i] == rand_arr[i] && ip_arr[i] == 1)
{
score++;
}
else if(ip_arr[i] != rand_arr[i])
{
life--;
}
}
Serial.println("#########################");
Serial.print("score = "); // Displays score and life after (not during) the cycle, they can still gain score w/ negative life.
Serial.println(score);
Serial.print("life = ");
Serial.println(life);
if (life <= 0)
{
control = false;
}
}
Serial.println("#########################");
Serial.println("####### GAME OVER #######");
Serial.println("#########################");
Serial.print("Your final score is ");
Serial.print(score);
Serial.println(".");
boolean startover = false;
// You can ask to start over and make "startover=true".
// For example, set 1st button status to off and ask to hit 1st button to start over.
while (startover != true)
{
digitalWrite(D4,HIGH);
digitalWrite(D5,HIGH);
digitalWrite(D6,HIGH);
digitalWrite(D7,HIGH);
digitalWrite(D8,HIGH);
delay(300);
digitalWrite(D4,LOW);
digitalWrite(D5,LOW);
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
digitalWrite(D8,LOW);
delay(300);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.