Simon-Says/Firmware/Combo/Combo.ino

147 lines
4.0 KiB
Arduino
Raw Normal View History

2014-01-30 16:18:28 -07:00
/*
2014-01-31 11:03:56 -07:00
Code checks for combination in order. Must press upper left button, then upper right button,
then lower left for success. Any wrong button will result in an unplesant sound,
and the code will reset.
2014-01-30 16:18:28 -07:00
Prof Mike Soltys
University of Colorado
2014-01-31 11:03:56 -07:00
1/31/2014
2014-01-30 16:18:28 -07:00
*/
// Define the button, LED, and buzzer pins (this is standard for all Simon Says games
2014-01-31 11:00:55 -07:00
// I've labeled mine UL for Upper Left, LR for Lower Left and so on.
2014-01-30 16:18:28 -07:00
const int led_UL = 10; //Red
const int led_UR = 3; //Green
const int led_LL = 13; //Blue
const int led_LR = 5; //Yellow
const int but_UL = 9;
const int but_UR = 2;
const int but_LL = 12;
const int but_LR = 6;
const int BUZZER1 = 4;
const int BUZZER2 = 7;
2014-01-31 11:00:55 -07:00
// 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;
2014-01-30 16:18:28 -07:00
void setup() {
//Setup hardware inputs/outputs.
//Enable pull ups on inputs
pinMode(but_UL, INPUT_PULLUP);
pinMode(but_UR, INPUT_PULLUP);
pinMode(but_LL, INPUT_PULLUP);
pinMode(but_LR, INPUT_PULLUP);
pinMode(led_UL, OUTPUT);
pinMode(led_UR, OUTPUT);
pinMode(led_LL, OUTPUT);
pinMode(led_LR, OUTPUT);
pinMode(BUZZER1, OUTPUT);
pinMode(BUZZER2, OUTPUT);
}
2014-01-31 11:00:55 -07:00
// keep doing this over and over again.
2014-01-30 16:18:28 -07:00
void loop() {
2014-01-31 11:00:55 -07:00
// this calls the bit of code ButtonWait() which is found below
// the code waits untill a button is pressed, and then tells you which button was pressed
// (1, 2, 3, 4)
int Button = ButtonWait();
// check to see if the button was the first one in our sequence (Upper Left)
if (Button == UL){
// if so, turn on the upper left button
2014-01-30 16:18:28 -07:00
digitalWrite(led_UL, HIGH);
2014-01-31 11:00:55 -07:00
// and wait for another button to be pressed
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);
2014-01-31 11:03:56 -07:00
Button = ButtonWait();
if (Button == LL){
// if so, turn that button on and continue
digitalWrite(led_LL, 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);
}
2014-01-30 16:18:28 -07:00
}
2014-01-31 11:00:55 -07:00
// 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);
2014-01-30 16:18:28 -07:00
}
2014-01-31 11:03:56 -07:00
2014-01-31 11:00:55 -07:00
// wait a second and turn off all LEDS
2014-01-30 16:18:28 -07:00
delay(1000);
digitalWrite(led_UR, LOW);
digitalWrite(led_UL, LOW);
digitalWrite(led_LL, LOW);
digitalWrite(led_LR, LOW);
}
2014-01-31 11:00:55 -07:00
// Waits for a button to be pressed, then returns 1-4 to tell what button it was.
2014-01-30 16:18:28 -07:00
int ButtonWait(void){
2014-01-31 11:00:55 -07:00
// 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
2014-01-30 16:18:28 -07:00
if(digitalRead(but_UL) == 0){
2014-01-31 11:00:55 -07:00
//wait till the user releases the button
while (digitalRead(but_UL) == 0){
delay(10);
}
// return what button was pressed
return(UL);
2014-01-30 16:18:28 -07:00
}
2014-01-31 11:00:55 -07:00
// Check if the UR button is pressed
2014-01-30 16:18:28 -07:00
else if(digitalRead(but_UR) == 0){
2014-01-31 11:00:55 -07:00
//wait till the user releases the button
while (digitalRead(but_UR) == 0){
delay(10);
}
// return what button was pressed
return(UR);
}
// and so on.
2014-01-30 16:18:28 -07:00
else if(digitalRead(but_LL) == 0){
2014-01-31 11:00:55 -07:00
while (digitalRead(but_LL) == 0){
delay(10);
}
return(LL);
2014-01-30 16:18:28 -07:00
}
else if(digitalRead(but_LR) == 0){
2014-01-31 11:00:55 -07:00
while (digitalRead(but_LR) == 0){
delay(10);
}
return(LR);
2014-01-30 16:18:28 -07:00
}
2014-01-31 11:00:55 -07:00
// if we get here, nothing has been pressed, and so we'll restart at the top of
// the loop while (1==1).
2014-01-30 16:18:28 -07:00
}
}
2014-01-31 11:00:55 -07:00
2014-01-31 11:03:56 -07:00