I have built a circuit on my Arduino Mega 2560, with 8 LEDS with built in resistors plugged into digital ports 39, 41, 43, 45, 47, 49, 51, and 53. They are controlled by 8 Momentary Push Buttons plugged into digital ports 38, 40, 42, 44, 46, 48, 50, and 52 and 220 Ω resistors to the ground rail on one side, and my positive rail on the other. I also have a piezo buzzer plugged into digital pin 8.
My code is as follows:
const int LED1 = 39;
const int LED2 = 41;
const int LED3 = 43;
const int LED4 = 45;
const int LED5 = 47;
const int LED6 = 49;
const int LED7 = 51;
const int LED8 = 53;
const int Button1 = 38;
const int Button2 = 40;
const int Button3 = 42;
const int Button4 = 44;
const int Button5 = 46;
const int Button6 = 48;
const int Button7 = 50;
const int Button8 = 52;
const int Speaker = 36;
int CurrentButton[]={Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8};
int CB;
int CurrentLED[]={LED1,LED2,LED3,LED4,LED5,LED6,LED7,LED8};
int CL;
int count;
int RandomStart;
long seed;
void setup(){
long seed = analogRead(0) + 1024 * analogRead(1);
randomSeed(seed);
RandomStart=random(0,8);
//RandomStart=5;
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(Button1, INPUT);
pinMode(Button2, INPUT);
pinMode(Button3, INPUT);
pinMode(Button4, INPUT);
pinMode(Button5, INPUT);
pinMode(Button6, INPUT);
pinMode(Button7, INPUT);
pinMode(Button8, INPUT);
CB=CurrentButton[RandomStart];
CL=CurrentLED[RandomStart];
count=RandomStart;
digitalWrite(CL, HIGH);
tone(8,50,100);
}
void loop() {
if (digitalRead(CB) == HIGH) {
digitalWrite(CL, LOW);
count++;
if(count>=sizeof(CurrentButton)/sizeof(CurrentButton[0])) count = 0;
CB = CurrentButton[count];
CL = CurrentLED[count];
digitalWrite(CL, HIGH);
}
}
As of now, my code works perfectly. Each LED is associated with one button, at the start of the program one is randomly selected and illuminated. After that you can press an LED's button to turn it off, and illuminate the next one in sequence. This all works perfectly. There is just one more piece of functionality that I would like to add but have no clue how to approach.
Currently I have an array with all 8 buttons in it, and a corresponding array with all 8 LEDs in it. I would like to make it so when the program starts up my buzzer plays a tone, and a certain number of buttons are pressed, and then 10 seconds later the buzzer plays a second tone and only the buttons pressed in between the two tones are the LEDs that get cycled through.
Any and all help would be greatly appreciated.