Hey Guys and Gals
Newbie here, first Ardunio project, first C++ application so any guidance would greatly be appreciated.
I'm building an application, where I'm generating a number 1 thru 5. Then light up a light that corresponds to that number. Once you press the button that corresponds to the light, then repeat the cycle.
The issue that i'm having is... when the random generator generates a 5, I get two lights that come on (not the same two. This only happens with 5. When there is a 1, just light one is lit. When there is a 2 only light 2 is lit. When there is a 3, only light 3 is lit and when there is a 4 only light 4 is lit. I was able to verify that it is only the 5 that creates this problem by printing out the random number generator.
If I change my random generator to use just 1 thru 4, everything works perfectly. if I set the generator to use a range of 2-6, then 5 keeps screwing up.
This is still a work in progress, not everything is complete..
Help!
/*11/28/2019
- -Start a timer
- -Generate a number between 1 and 5,
- -Light up a light that corasponds to the random generated number.
- -Wait until the button that corresponds to the light.
- -Repeat new generated number/light/button push "X" times.
- -Stop timer
- -Display time
*/
int ButtonBtn1 = 2;
int ButtonBtn2 = 4;
int ButtonBtn3= 7;
int ButtonBtn4 = 8;
int ButtonBtn5 = 12;
int Light1 = 3;
int Light2 = 5;
int Light3 = 6;
int Light4 = 9;
int Light5 = 10;
int randNumber;
int timer1 = 250;
int timer2 = 50;
int ctr=0;
void setup() {
Serial.begin(9600);
pinMode(Light1, OUTPUT);
pinMode(Light2, OUTPUT);
pinMode(Light3, OUTPUT);
pinMode(Light4, OUTPUT);
pinMode(Light5, OUTPUT);
pinMode(ButtonBtn1, INPUT_PULLUP);
pinMode(ButtonBtn2, INPUT_PULLUP);
pinMode(ButtonBtn3, INPUT_PULLUP);
pinMode(ButtonBtn4, INPUT_PULLUP);
pinMode(ButtonBtn5, INPUT_PULLUP);
randomSeed(analogRead(0));
}
void pressBtn(){ // press the button that matches the light that is on
if (digitalRead(Light1)==HIGH &&
digitalRead(ButtonBtn1)==LOW){
Clear();}
else if (digitalRead(Light2)==HIGH &&
digitalRead(ButtonBtn2)==LOW){
Clear();}
else if (digitalRead(Light3)==HIGH &&
digitalRead(ButtonBtn3)==LOW){
Clear();}
else if (digitalRead(Light4)==HIGH &&
digitalRead(ButtonBtn4)==LOW){
Clear();}
else if (digitalRead(Light5)==HIGH &&
digitalRead(ButtonBtn5)==LOW){
Clear();}
else{
pressBtn();
}
}
void Clear(){ // clear all the lights, to start a new random generated number
delay (timer1);
digitalWrite(Light1, LOW);
digitalWrite(Light2, LOW);
digitalWrite(Light3, LOW);
digitalWrite(Light4, LOW);
digitalWrite(Light5, LOW);
delay (timer2);
loop();
}
void seqLight() { // light the lights in order to indicate end of game
digitalWrite(Light1, HIGH);
delay (timer1);
digitalWrite(Light1, LOW);
digitalWrite(Light2, HIGH);
delay (timer1);
digitalWrite(Light2, LOW);
digitalWrite(Light3, HIGH);
delay (timer1);
digitalWrite(Light3, LOW);
digitalWrite(Light4, HIGH);
delay (timer1);
digitalWrite(Light4, LOW);
digitalWrite(Light5, HIGH);
delay (timer1);
digitalWrite(Light5, LOW);
}
void lightBtn(){
if ((randNumber) == 1) { // light up the light that corresponds to the random generated number
digitalWrite(Light1, HIGH);
}
if ((randNumber) == 2) {
digitalWrite(Light2, HIGH);
}
if ((randNumber) == 3) {
digitalWrite(Light3, HIGH);
}
if ((randNumber) == 4) {
digitalWrite(Light4, HIGH);
}
if ((randNumber) == 5) {
digitalWrite(Light5, HIGH);
}
else{
pressBtn();
}
}
void loop() {
randNumber= random(1,6); //generate a number between 1 thru 5
Serial.print(randNumber);
Serial.print(" ");
lightBtn();
}
sketch_nov28b.ino (2.95 KB)