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 the ground rail. I also have a piezo buzzer plugged into digital pin 8.
My code is as follows:
//This program has 8 LEDs and 8 buttons paired together in 8 Stations. At the start of the program there is 5 seconds for buttons to be pressed in a specific order, these are added to an array in that order. After those 5 seconds setup is doneand one of the selected stations is illuminated. When the current illuminated station's button is pressed, the next station is illuminated and this continues until the arduino is powered off.
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; //Button and LED pin definition
int AllButton[] = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8};
int AllLED[] = {LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8}; //Arrays of all possible LEDs and Buttons
int AllowedButton[] = {0, 0, 0, 0, 0, 0, 0, 0};
int AllowedLED[] = {0, 0, 0, 0, 0, 0, 0, 0}; //Arrays to be modified to contain all used LEDs and Buttons in correct order
int CB, CL; //variables to track current button and LED
int count, prepcount; //variables to move through arrays
long seed; //seeding for random number generation
int RandomStart; //variable for determining random starting station from the allowed lists
bool prep = false; // variable for spliting between program setup and program execution in void loop
void setup() {
long seed = analogRead(0) + 1024 * analogRead(1); //generate seed from 2 different analog pins to increase range of possible seeds
randomSeed(seed); //use generated seed
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT); //define outputs
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(Button5, INPUT_PULLUP);
pinMode(Button6, INPUT_PULLUP);
pinMode(Button7, INPUT_PULLUP);
pinMode(Button8, INPUT_PULLUP); //define inputs
prepcount = 0; //intilizes array counter for setup
tone(8, 50, 1000); //play tone to indicate end of void setup and start of my program's setup
}
void loop() {
if (prep == false) {
if (digitalRead(AllButton[0]) == LOW) {
AllowedButton[prepcount] = Button1;
AllowedLED[prepcount] = LED1;
prepcount++; //test if Button 1 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
}
if (digitalRead(AllButton[1]) == LOW) {
AllowedButton[prepcount] = Button2;
AllowedLED[prepcount] = LED2;
prepcount++; //test if Button 2 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
}
if (digitalRead(AllButton[2]) == LOW) {
AllowedButton[prepcount] = Button3;
AllowedLED[prepcount] = LED3;
prepcount++; //test if Button 3 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
}
if (digitalRead(AllButton[3]) == LOW) {
AllowedButton[prepcount] = Button4;
AllowedLED[prepcount] = LED4;
prepcount++; //test if Button 4 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
}
if (digitalRead(AllButton[4]) == LOW) {
AllowedButton[prepcount] = Button5;
AllowedLED[prepcount] = LED5;
prepcount++; //test if Button 5 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
}
if (digitalRead(AllButton[5]) == LOW) {
AllowedButton[prepcount] = Button6;
AllowedLED[prepcount] = LED6;
prepcount++; //test if Button 6 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
}
if (digitalRead(AllButton[6]) == LOW) {
AllowedButton[prepcount] = Button7;
AllowedLED[prepcount] = LED7;
prepcount++; //test if Button 7 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
}
if (digitalRead(AllButton[7]) == LOW) {
AllowedButton[prepcount] = Button8;
AllowedLED[prepcount] = LED8;
prepcount++; //test if Button 8 has been pressed and adds it to next available spot in Allowed Button and LED arrays.
}
RandomStart = random(0, (sizeof(AllowedButton) / sizeof(AllowedButton[0]))); //pick random starting station from allowed stations
CB = AllowedButton[RandomStart]; //set current button variable to decided starting point
CL = AllowedLED[RandomStart]; //set current LED variable to decided starting point
count = RandomStart; //intilizes array counter for program
}
if (millis() >= 5000 && prep == false) {//ends timer for setup of allowing specific stations for use in program
digitalWrite(CL, HIGH); //illuminates the current station
prep = true; //ends setup
tone(8, 50, 1000); //plays tone to indicate setup is done
}
if (digitalRead(CB) == LOW && prep == true) {//actual program loop
digitalWrite(CL, LOW);//when a button is pressed, turn that station's LED off
count++; //increase array counter
if ((count >= sizeof(AllowedButton) / sizeof(AllowedButton[0])) || (AllowedButton[count] == 0)) count = 0; //if the array counter has gone further than allowed stations reset to first station
CB = AllowedButton[count]; //set current button to the next station
CL = AllowedLED[count]; //set current LED to the next station
digitalWrite(CL, HIGH); //set new Station's LED on
}
}
The program correctly beeps at the start and end of setup, but it doesn't seem to correctly pick a random LED or illuminate any of the stations.
This is functional code, but it only works with all 8 buttons.
const int LED1 = 39, LED2 = 41, LED3 = 43, LED4 = 45, LED5 = 47, LED6 = 49, LED7 = 51, LED8 = 53, Button1 = 38, Button2 = 40, Button3 = 42, Button4 = 44, Button5 = 46, Button6 = 48, Button7 = 50, Button8 = 52, Speaker = 36;
int CurrentButton[] = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8}, CurrentLED[] = {LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8};
int CB;
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_PULLUP), pinMode(Button2, INPUT_PULLUP), pinMode(Button3, INPUT_PULLUP), pinMode(Button4, INPUT_PULLUP), pinMode(Button5, INPUT_PULLUP), pinMode(Button6, INPUT_PULLUP), pinMode(Button7, INPUT_PULLUP), pinMode(Button8, INPUT_PULLUP);
CB = CurrentButton[RandomStart];
CL = CurrentLED[RandomStart];
count = RandomStart;
digitalWrite(CL, HIGH);
tone(8, 50, 100);
}
void loop() {
if (digitalRead(CB) == LOW) {
digitalWrite(CL, LOW);
count++;
if (count >= sizeof(CurrentButton) / sizeof(CurrentButton[0])) count = 0;
CB = CurrentButton[count];
CL = CurrentLED[count];
digitalWrite(CL, HIGH);
}
}