I am writing a program to run a project I am doing for my Electronics A-Level. It involves telling the user to press a certain button (via and LCD screen, working!), then timing between the command and the button press. This needs to then be shown on the LCD screen.
I can work out how to time between the command and button press. But I am confused as to how I would loop it back to just timing if the button is not pressed at a certain point.
Here's the code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7, 8);unsigned long timeBegin; // A variable for the start time of a timer
long randomTimer; // A variable for the length of a timer
long timerLength; // The length of time the timer ran
int minTimer = 500; // The minimum timer length
int maxTimer = 3000; // The maximum timer length
#define redB 1
#define blueB 2
#define yellowB 3
#define greenB 4void setup(){
}void loop(){
}unsigned int testTime(){
lcd.print("Press Button X, "); // Print a textual command
timeBegin = millis(); // Set the start time of the timer
randomTimer = random(3minTimer, maxTimer); // Set a random timer length
if(digitalRead(redB) == 1) { // If the button is pressed
timerLength = (millis() - timeBegin); // Work out the time it took
}
else { // If the button is not pressed
timeBegin = millis(); // Set the start time of the timer
}
}
As you can see it's not finished, but I don't know where to go!