Good night guys,
Im having a weird problem ( for me) in my code. So, the construction of it its a bunch of ifs that test the state of the program through a bunch of variables.( for example if LED_ON_RED == true means that the led is on and does some stuff etc).
It all works very well, except the IF - ELSE, where it executes both IF and ELSE lines of code. Here is the full code, and next to it the part that fails: ( there are a lot of variables that are not being used yet)
#define INTERVALO_SUPERIOR 10000
#define INTERVALO_INFERIOR 200
const byte LEDpin_RED = 4 ;
const byte LEDpin_YELLOW = 8;
const byte LEDpin_BLUE = 2;
const byte BUTTONpin_RED = 15;
const byte BUTTONpin_YELLOW = 14;
const byte BUTTONpin_BLUE = 16;
unsigned long prevMillis_YELLOW = 0;
unsigned long prevMillis_RED = 0;
unsigned long prevMillis_BLUE = 0;
unsigned long currentMillis_YELLOW;
unsigned long currentMillis_RED;
unsigned long currentMillis_BLUE;
unsigned long interval_YELLOW;
unsigned long interval_RED;
unsigned long interval_BLUE;
unsigned long Start_Time_YELLOW;
unsigned long Start_Time_RED;
unsigned long Start_Time_BLUE;
unsigned long End_Time_YELLOW;
unsigned long End_Time_RED;
unsigned long End_Time_BLUE;
unsigned long startTimer;
unsigned long endTimer;
bool need_random_RED = true;
bool turn_ON_RED = false;
bool LED_ON_RED = false;
bool perder = false;
bool reset = true;
float duration;
float total_time;
unsigned int j ;
unsigned int n;
void setup() {
pinMode(LEDpin_RED, OUTPUT);
pinMode(LEDpin_YELLOW, OUTPUT);
pinMode(LEDpin_BLUE, OUTPUT);
pinMode(BUTTONpin_RED, INPUT);
pinMode(BUTTONpin_YELLOW, INPUT);
pinMode(BUTTONpin_BLUE, INPUT);
randomSeed(analogRead(5));
Serial.begin(9600);
}
void loop() {
if(reset == true)
{
need_random_RED = true;
LED_ON_RED = false;
turn_ON_RED = false;
perder = false;
reset = false;
total_time = 0;
j=0;
duration=0;
}
Serial.println("Quanto tempo em segundos vai querer jogar o jogo?");
while(Serial.available()==0){}
n = Serial.parseInt()*1000;
Serial.println("JOGO INICIADO");
Serial.println("");
startTimer = millis();
endTimer = millis();
while(endTimer - startTimer <=n)
{
if(need_random_RED == true)
{
interval_RED = random(200,1000);
prevMillis_RED = millis();
turn_ON_RED = true;
need_random_RED = false;
}
currentMillis_RED = millis();
if(turn_ON_RED == true && (currentMillis_RED - prevMillis_RED >= interval_RED))
{
digitalWrite(LEDpin_RED,HIGH);
Start_Time_RED = millis();
turn_ON_RED = false;
LED_ON_RED = true;
}
if(digitalRead(BUTTONpin_RED) == HIGH)
{
if(LED_ON_RED == true)
{
End_Time_RED = millis();
duration = End_Time_RED - Start_Time_RED;
Serial.print(duration/1000);
Serial.println(" Segundos!!" );
total_time = total_time + duration;
digitalWrite(LEDpin_RED, LOW);
LED_ON_RED = false;
need_random_RED = true;
j++;
}
else
{
Serial.println("BOTAO ERRADO. PERDEU O JOGO!");
Serial.println("");
Serial.println("");
Serial.println("A REINICIAR...");
perder = true;
delay(4000);
}
}
if (perder==true)
{
reset=true;
break;
}
endTimer = millis();
}
if(perder == false)
{
Serial.print("O tempo total que demorou a pressionar os botoes foi ");
Serial.print(total_time/1000 );
Serial.print(" e a sua média foi: ");
Serial.println( (total_time/j)/1000);
Serial.println("");
Serial.print("Ao todo realizou ");
Serial.print(j);
Serial.println(" testes");
for(int i=0;i<5;i++)
{Serial.println("");}
Serial.println("JOGO ACABADO. A REINICIAR...");
delay(4000);
reset= true;
}
}
WRONG PART:
if(digitalRead(BUTTONpin_RED) == HIGH)
{
if(LED_ON_RED == true)
{
End_Time_RED = millis();
duration = End_Time_RED - Start_Time_RED;
Serial.print(duration/1000);
Serial.println(" Segundos!!" );
total_time = total_time + duration;
digitalWrite(LEDpin_RED, LOW);
LED_ON_RED = false;
need_random_RED = true;
j++;
}
else
{
Serial.println("BOTAO ERRADO. PERDEU O JOGO!"); // YOU LOST THE GAME
Serial.println("");
Serial.println("");
Serial.println("A REINICIAR..."); // REBOOTING
perder = true; // LOSE == TRUE
delay(4000);
}
}
It detects perfectly that the button is being pressed, and it detects that the LED is on, but also prints game lose and activates the " lose" statement, making it reboot the entire game.
Any help on why this is happening is very appreciated
Thank you very much! ![]()
Have a nice night