IF and ELSE statements being executed

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! :slight_smile:

Have a nice night

If LED_ON_RED is true then you set it to false so next time through loop() the else code is executed. This will happen very quickly, so both sets of code will be executed in quick succession

Are you trying to keep the LED on for a period ?

UKHeliBob:
Are you trying to keep the LED on for a period ?

I will explain what this is supposed to do.

The overall is a program that turns on a LED at a random time defined for interval, and stays ON until the button is pressed. When it pressed it calculates the time that the user took to press it, and creates a new random interval for the LED to turn on. I only click on the button once, so the second time it goes through the loop it shouldnt even enter the " digitalRead == true" IF statement. Are you saying that arduino is so fast that between my clicking and unclicking , it runs the program twice and it detects the button still pressed? Should I put some small delay to fix that? Thnx!

Should I put some small delay to fix that?

No. Do it properly.

UKHeliBob:
No. Do it properly.

Im sorry, what do you mean?

It sounds to me as though you need to detect when the button becomes pressed rather than when if is pressed.

Look at the StateChangeDetection example in the IDE to see how to do it.

After the button is pressed, it has to wait it is released! Only if it has been released a new pressure can be taken in account.
It's also important a debouncing: you can achieve it simply by a 100nF (100.000pF) capacitor between input and ground, or paralleled to the button.

Thank you all, I dont have capacitors yet but the State idea worked. Thank you all