Looks like you need a "state machine" ![]()
Maybe something like this:
// Dual stop watch with state machine
// output on serial monitor
// blinking led while running
const byte startPin = 8;
const byte stopLeftPin = 9;
const byte stopRightPin = 10;
const byte ledPin = 13;
boolean buttonStartState = HIGH;
boolean buttonStopLeftState = HIGH;
boolean buttonStopRightState = HIGH;
boolean leftIsIn = false;
boolean rightIsIn = false;
byte state = 0; // state machine states: 0 ... 3
unsigned long startTime;
unsigned long stopLeft;
unsigned long stopRight;
unsigned long raceTimeLeft;
unsigned long raceTimeRight;
void setup() {
Serial.begin(9600);
pinMode(startPin, INPUT_PULLUP);
pinMode(stopLeftPin, INPUT_PULLUP);
pinMode(stopRightPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
// read input pins:
buttonStartState = digitalRead(startPin);
buttonStopLeftState = digitalRead(stopLeftPin);
buttonStopRightState = digitalRead(stopRightPin);
switch (state) {
case 0: // make ready for start
Serial.println("press start button");
state++;
break;
case 1: // waiting for start button press
if (buttonStartState == LOW) {
startTime = millis();
Serial.println("START");
state++;
}
break;
case 2: // running - waiting for left and/or right stop button press
digitalWrite(ledPin, millis() / 128 % 2); // blink the led
if ( (buttonStopLeftState == LOW) && (leftIsIn == false) ) {
stopLeft = millis();
raceTimeLeft = stopLeft - startTime;
Serial.print("Left is in: ");
Serial.println(raceTimeLeft);
leftIsIn = true;
}
if ( (buttonStopRightState == LOW) && (rightIsIn == false) ) {
stopRight = millis();
raceTimeRight = stopRight - startTime;
Serial.print("Right is in: ");
Serial.println(raceTimeRight);
rightIsIn = true;
}
if ( (leftIsIn == true) && (rightIsIn == true) ) {
digitalWrite(ledPin, LOW);
leftIsIn = false;
rightIsIn = false;
state++;
}
break;
case 3: // print the winner
if (raceTimeLeft < raceTimeRight) {
Serial.println("LEFT was first!");
}
else {
Serial.println("RIGHT was first!");
}
state = 0;
break;
}
}
No LCD, just serial output, so you need to adapt it for your needs.