Hello,
I ve searched some posts by other people that seemed to have the same problem has me, but didnt really find what I wanted.
Im doing a game that has 3 LEDS that turn on at random times, and I have one button for each LED, to be activated as quickly as possible as the LED turn on. The program counts the time the button took to be activated, and works for an amount of time specified by the user in the start of the game. Im trying to make a if statement to check if the user is clicking in the wrong button , and if it does, loses the game. After he loses the game I want the program to skip everything and just go to the point in void loop() where the function was called. The following piece of code has my while loop to wait for the button to be pressed. The complete code will be displayed in the end of this post.
void LED(unsigned int random_BUTTON, unsigned int random_LED)
{
startTime = millis();
while(digitalRead(random_BUTTON) != HIGH) // WHILE THE CORRECT IS NOT BEING PRESSED, CHECK IF THE OTHER BUTTONS ARE BEING PRESSED
{
switch(random_BUTTON)
{
case BUTTONpin_RED:
if(digitalRead(BUTTONpin_YELLOW)== HIGH || digitalRead(BUTTONpin_BLUE)== HIGH)
{
Serial.println("");
Serial.println("BOTAO ERRADO. PERDEU O JOGO!!"); // WRITE "YOU LOST THE GAME"
Serial.println("");
digitalWrite(random_LED, LOW);
}
break;
case BUTTONpin_YELLOW:
if(digitalRead(BUTTONpin_RED)== HIGH || digitalRead(BUTTONpin_BLUE)== HIGH)
{
Serial.println("");
Serial.println("BOTAO ERRADO. PERDEU O JOGO!!");
Serial.println("");
digitalWrite(random_LED, LOW);
}
break;
case BUTTONpin_BLUE:
if(digitalRead(BUTTONpin_YELLOW)== HIGH || digitalRead(BUTTONpin_RED)== HIGH)
{
Serial.println("");
Serial.println("BOTAO ERRADO. PERDEU O JOGO!!");
Serial.println("");
digitalWrite(random_LED, LOW);
}
break;
}
return; // I EXPECTED THIS TO RETURN TO THE MAIN LOOP
}
endTime = millis();
digitalWrite(random_LED, LOW);
duration = endTime - startTime;
Serial.print(duration/1000);
Serial.println(" Segundos!!");
Serial.println("////////////////////////////////////");
Serial.println("");
j++;
total_Time = duration + total_Time;
delay(200);
}
There are 2 problems I dont understand in this outcome. First it doesnt print the last prints in void loop(), which means it didnt exit the function and continued the normal procedure. Second, it prints six times ("YOU LOST THE GAME") after I click the wrong button and turns off the LED ( as it should). But the function continues running, which means if I click the button, it continues the game normally. Here you have an example of the Serial monitor display: ( translated)
How much time you want to play the game?
GAME STARTED
1.04 Seconds!!
////////////////////////////////////
0.53 Seconds!!
////////////////////////////////////
WRONG BUTTON. YOU LOST THE GAME!!
WRONG BUTTON. YOU LOST THE GAME!!
WRONG BUTTON. YOU LOST THE GAME!!
WRONG BUTTON. YOU LOST THE GAME!!
WRONG BUTTON. YOU LOST THE GAME!!
WRONG BUTTON. YOU LOST THE GAME!!
2.98 Seconds!!
////////////////////////////////////
0.82 Seconds!!
////////////////////////////////////
0.35 Seconds!!
////////////////////////////////////
You took total 5.72 seconds to press all buttons and your average was 1.14.
You made 5 tests in total
Basically after the WRONG BUTTON appeared I clicked in the correct one and continued to work normally whichs means totally ignored my return.
FULL CODE:
const byte LEDpin_RED = 4 ;
const byte LEDpin_YELLOW = 7;
const byte LEDpin_BLUE = 2;
const byte BUTTONpin_RED = 15;
const byte BUTTONpin_YELLOW = 14;
const byte BUTTONpin_BLUE = 16;
const byte array_LED[] = {LEDpin_YELLOW, LEDpin_RED, LEDpin_BLUE};
unsigned int n,j=0;
float startTime ;
float endTime;
float startTimer;
float endTimer;
float duration;
unsigned int random_LED;
float total_Time;
void LED(unsigned int random_BUTTON,unsigned int random_LED);
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);
Serial.begin(9600);
}
void loop() {
randomSeed(analogRead(5));
Serial.println("Quanto tempo em segundos vai querer jogar o jogo?");
while(Serial.available()==0){}
n = Serial.parseInt()*1000;
startTimer = millis();
endTimer = millis();
Serial.println("JOGO INICIADO");
Serial.println("");
while(endTimer - startTimer <=n){
random_LED = array_LED[random(0,3)];
delay(random(0,2000));
digitalWrite(random_LED, HIGH);
switch(random_LED){
case LEDpin_RED:
LED(BUTTONpin_RED,LEDpin_RED);
break;
case LEDpin_YELLOW:
LED(BUTTONpin_YELLOW,LEDpin_YELLOW);
break;
case LEDpin_BLUE:
LED(BUTTONpin_BLUE,LEDpin_BLUE);
break;
}
endTimer = millis();
}
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);
j=0;
total_Time =0;
}
void LED(unsigned int random_BUTTON, unsigned int random_LED)
{
startTime = millis();
while(digitalRead(random_BUTTON) != HIGH)
{
switch(random_BUTTON)
{
case BUTTONpin_RED:
if(digitalRead(BUTTONpin_YELLOW)== HIGH || digitalRead(BUTTONpin_BLUE)== HIGH)
{
Serial.println("");
Serial.println("BOTAO ERRADO. PERDEU O JOGO!!");
Serial.println("");
digitalWrite(random_LED, LOW);
}
break;
case BUTTONpin_YELLOW:
if(digitalRead(BUTTONpin_RED)== HIGH || digitalRead(BUTTONpin_BLUE)== HIGH)
{
Serial.println("");
Serial.println("BOTAO ERRADO. PERDEU O JOGO!!");
Serial.println("");
digitalWrite(random_LED, LOW);
}
break;
case BUTTONpin_BLUE:
if(digitalRead(BUTTONpin_YELLOW)== HIGH || digitalRead(BUTTONpin_RED)== HIGH)
{
Serial.println("");
Serial.println("BOTAO ERRADO. PERDEU O JOGO!!");
Serial.println("");
digitalWrite(random_LED, LOW);
}
break;
}
}
endTime = millis();
digitalWrite(random_LED, LOW);
duration = endTime - startTime;
Serial.print(duration/1000);
Serial.println(" Segundos!!");
Serial.println("////////////////////////////////////");
Serial.println("");
j++;
total_Time = duration + total_Time;
delay(200);
}
Im sorry if my explanation got a little confusing, thank you very much in advance!!