// Using interrupts - detect FALLING
volatile bool buttonPressed2, buttonPressed3; // "volatile" inside interrupt service routeine (ISR)
int buttonPin2 = 2; // INT0
int buttonPin3 = 3; // INT1
int ledPin4 = 4; // GRN LED
int ledPin5 = 5; // RED LED
unsigned long startTime, stopTime;
bool firstRead2 = 1, firstRead3 = 1;
void setup() {
Serial.begin(115200); // start serial monitor
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin2), buttonISR2, FALLING); // PIN#, ISR, TRIGGER
attachInterrupt(digitalPinToInterrupt(buttonPin3), buttonISR3, FALLING);
}
void loop() {
checkButton2();
checkButton3();
}
int buttonISR2() {
noInterrupts(); // disable interrupts in ISR
buttonPressed2 = true; // set flag
interrupts(); // enable interrupts
}
int buttonISR3() {
noInterrupts();
buttonPressed3 = true;
interrupts();
}
void checkButton2() {
if (buttonPressed2 && firstRead2) {
startTime = micros();
firstRead2 = 0;
digitalWrite(ledPin4, HIGH);
}
}
void checkButton3() {
if (buttonPressed3 && firstRead3) {
stopTime = micros();
firstRead3 = 0;
digitalWrite(ledPin5, HIGH);
Serial.print("Start: ");
Serial.println(startTime);
Serial.print(" Stop: ");
Serial.println(stopTime);
Serial.print("Split: ");
Serial.println(stopTime - startTime);
delay(500);
Serial.println("Press RESET button.");
}
}
START
STOP