Fixed Combo Code

This commit is contained in:
Mike S 2014-01-31 11:00:55 -07:00
parent 80d1535f02
commit d6fffc3e57
1 changed files with 73 additions and 26 deletions

View File

@ -1,5 +1,5 @@
/* /*
Make the Simon Says Game a 4-hole Ocarina (Like in the legend of zelda) Code to
Prof Mike Soltys Prof Mike Soltys
University of Colorado University of Colorado
@ -22,6 +22,13 @@ const int but_LR = 6;
const int BUZZER1 = 4; const int BUZZER1 = 4;
const int BUZZER2 = 7; const int BUZZER2 = 7;
// We'll set up variables for each button, so Upper Left is Button 1,
// Upper right is 2, etc.
const int UL = 1;
const int UR = 2;
const int LL = 3;
const int LR = 4;
void setup() { void setup() {
//Setup hardware inputs/outputs. //Setup hardware inputs/outputs.
//Enable pull ups on inputs //Enable pull ups on inputs
@ -39,21 +46,42 @@ void setup() {
pinMode(BUZZER2, OUTPUT); pinMode(BUZZER2, OUTPUT);
} }
// keep doing this over and over again.
void loop() { void loop() {
// keep doing this over and over again. // this calls the bit of code ButtonWait() which is found below
// I will start with the most buttons pressed, and move on to the least buttons pressed // the code waits untill a button is pressed, and then tells you which button was pressed
// for any combination, i'll play a matching note and light up the LEDs that aren't being pressed. // (1, 2, 3, 4)
int Button = ButtonWait();
// Start with all buttons pressed - C4 - 262 // check to see if the button was the first one in our sequence (Upper Left)
int Button=ButtonWait(); if (Button == UL){
delay(100); // if so, turn on the upper left button
if (Button == 1){
digitalWrite(led_UL, HIGH); digitalWrite(led_UL, HIGH);
Button=ButtonWait(); // and wait for another button to be pressed
if (Button==2){ Button = ButtonWait();
// now check to see if the second button (UR) in our sequence was pressed
if (Button == UR){
// if so, turn that button on and continue
digitalWrite(led_UR, HIGH); digitalWrite(led_UR, HIGH);
// play success tone!
tone(BUZZER1,399,1000);
// interesting fact: 399 Hz is the "most plesant" frequency
// http://www.ncbi.nlm.nih.gov/pubmed/503755
// if at any point a wrong button is pressed, the code will jump down
// to delay 1000, turn off the LEDs, and restart at the top of void loop()
}
// if the wrong button is pressed, play an unplesant sound
else {
tone(BUZZER1,2000,500);
} }
} }
// if the wrong button is pressed, play an unplesant sound
else {
tone(BUZZER1,2000,500);
}
// wait a second and turn off all LEDS
delay(1000); delay(1000);
digitalWrite(led_UR, LOW); digitalWrite(led_UR, LOW);
digitalWrite(led_UL, LOW); digitalWrite(led_UL, LOW);
@ -61,27 +89,46 @@ void loop() {
digitalWrite(led_LR, LOW); digitalWrite(led_LR, LOW);
} }
// Waits for a button to be pressed, then returns 1-4 to tell what button it was.
int ButtonWait(void){ int ButtonWait(void){
while(1 == 1){ // this is always true, so this loop will keep going until it reaches a "return" command
while(1 == 1){
// Check if the UL button is pressed
if(digitalRead(but_UL) == 0){ if(digitalRead(but_UL) == 0){
while (digitalRead(but_UL) != 0) ; //wait till the user releases the button
while (digitalRead(but_UL) == 0){
delay(10); delay(10);
return 1;
} }
// return what button was pressed
return(UL);
}
// Check if the UR button is pressed
else if(digitalRead(but_UR) == 0){ else if(digitalRead(but_UR) == 0){
while (digitalRead(but_UR) != 0) ; //wait till the user releases the button
while (digitalRead(but_UR) == 0){
delay(10); delay(10);
return 2;
} }
// return what button was pressed
return(UR);
}
// and so on.
else if(digitalRead(but_LL) == 0){ else if(digitalRead(but_LL) == 0){
while (digitalRead(but_LL) != 0) ; while (digitalRead(but_LL) == 0){
delay(10); delay(10);
return 3; }
return(LL);
} }
else if(digitalRead(but_LR) == 0){ else if(digitalRead(but_LR) == 0){
while (digitalRead(but_LR) != 0) ; while (digitalRead(but_LR) == 0){
delay(10); delay(10);
return 4;
} }
return(LR);
}
// if we get here, nothing has been pressed, and so we'll restart at the top of
// the loop while (1==1).
} }
} }