I am trying to make a device to test reaction tester. The device is supposed to turn on a randomly selected LED and start a timer and the user must press the corresponding button to stop the timer. The results of 5 tests are averaged for a total score.
The code compiles but after the start function runs the blue LED blinks and nothing else happens. I believe there is a problem with the test function but I am not sure.
//initialize LCD sreen
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
//initialize buzzer
const int buzzer = 13;
//initialize buttons and leds
//using analog pins for buttons because I dont have enough pins
const int blueLED = 6;
const int blueButton = A0;
const int redLED = 7;
const int redButton = A1;
const int greenLED = 8;
const int greenButton = A2;
const int yellowLED = 9;
const int yellowButton = A3;
//initialize timer
unsigned long startMillis;
unsigned long currentMillis;
//variables
long test_1;
long test_2;
long test_3;
long test_4;
long test_5;
long reaction_average;
void setup() {
//set up LCD
lcd.begin(16,2);
lcd.setCursor(0,1);
lcd.print("Ready reaction test!");
//set up buzzer
pinMode(buzzer, OUTPUT);
//set up buttons and leds
pinMode(blueLED, OUTPUT);
pinMode(blueButton, INPUT_PULLUP);
pinMode(redLED, OUTPUT);
pinMode(redButton, INPUT_PULLUP);
pinMode(greenLED, OUTPUT);
pinMode(greenButton, INPUT_PULLUP);
pinMode(yellowLED, OUTPUT);
pinMode(yellowButton, INPUT_PULLUP);
}
void loop() {
Start(); //run start function
test_1 = Test(); //run test function 5 times and
delay(1000); //save results
test_2 = Test();
delay(1000);
test_3 = Test();
delay(1000);
test_4 = Test();
delay(1000);
test_5 = Test();
delay(1000);
reaction_average = ((test_1 + test_2 + test_3 + test_4 + test_5)/5); //calculate average reaction time
lcd.setCursor(0,1); //and print results for user
lcd.clear();
lcd.print("Your average reaction time is");
lcd.setCursor(1, 1);
lcd.print(reaction_average);
}
void Start(){
lcd.setCursor(0,1); //plays welcome message on startup
lcd.clear();
lcd.print("Ready test in:");
delay(2000);
lcd.setCursor(0,1);
lcd.clear();
lcd.print("3");
delay(1000);
lcd.setCursor(0,1);
lcd.clear();
lcd.print("2");
delay(1000);
lcd.setCursor(0,1);
lcd.clear();
lcd.print("1");
delay(1000);
lcd.setCursor(0,1);
lcd.clear();
lcd.print("GO!");
}
long Test(){
int rando = random(1, 5); //save random number
long reaction;
if(rando = 1){
digitalWrite(blueLED, HIGH); //turn on LED
startMillis = millis(); //timer start time
if(blueButton == HIGH){ //user presses button
currentMillis = millis(); //timer stop time
}
digitalWrite(blueLED, LOW);
reaction = startMillis - currentMillis; //calculates reaction time
return reaction; //return result in milliseconds
}
else if(rando = 2){
digitalWrite(redLED, HIGH);
startMillis = millis(); //timer start time
if(redButton == HIGH){
currentMillis = millis();
}
digitalWrite(redLED, LOW);
reaction = startMillis - currentMillis;
return reaction; //in milliseconds
}
else if(rando = 3){
digitalWrite(greenLED, HIGH);
startMillis = millis(); //timer start time
if(greenButton == HIGH){
currentMillis = millis();
}
digitalWrite(greenLED, LOW);
reaction = startMillis - currentMillis;
return reaction; //in milliseconds
}
else if(rando = 4){
digitalWrite(greenLED, HIGH);
startMillis = millis(); //timer start time
if(greenButton == HIGH){
currentMillis = millis();
}
digitalWrite(greenLED, LOW);
reaction = startMillis - currentMillis;
return reaction; //in milliseconds
}
}