Hey, i am trying to make a device that you activate by pressing a button, then it creates a random delay, and you have to wait for this random amount of time, and when it has passed, either a led or a buzzer will go off (also randomly chosen), then you have to click again, and then it will tell you your reaction time based on two millis() timers. But i seem to have a problem on the very very last step, everything is working, but when the led or buzzer goes off, it just jumps straight past my while loop, doesent wait for a button click, it just jumps back to the beginning.
So i am either trying to make it so that it will get stuck in my while loop, or so that it will wait for a button press, before telling me my reaction time and going back to the beginning.
Any advice please??
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int buzzerPin = 6;
const int buttonPin = 7;
const int ledPin = 8;
unsigned long int counter = 0;
unsigned long int reaction = 0;
int randomDelay = 0;
int randomVal1 = 0;
unsigned long int realReaction = 0;
int buttonState = 0;
int looper = 0;
void setup() {
randomSeed(analogRead(0));
lcd.begin(16, 2);
pinMode(6, OUTPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
}
void loop() {
introduction();
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
randomVal1 = random(0, 100);
randomDelay = random(3000, 10000);
lcd.setCursor(0, 0);
lcd.print("Tap when you ");
lcd.setCursor(0, 1);
lcd.print("notice sound/led");
delay(randomDelay);
reaction = millis();
buzzerOrLed();
buttonState = digitalRead(buttonPin);
while (looper == 0) {
lcd.setCursor(0, 0);
lcd.print("Tap when you ");
lcd.setCursor(0, 1);
lcd.print("notice sound/led");
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
looper++;
counter = millis();
realReaction = (reaction - counter);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(realReaction);
lcd.setCursor(0, 1);
lcd.print(" ");
}
}
}
}
void introduction() {
lcd.setCursor(0, 0);
lcd.print("Press the button");
lcd.setCursor(0, 1);
lcd.print("to start test ");
}
void buzzerOrLed() {
if (randomVal1 > 53) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
} else {
tone(buzzerPin, 800);
delay(200);
noTone(buzzerPin);
}
}