int ledPin = LED_BUILTIN; int buttonPin = 2; // Button connected to D2 and GND unsigned long lowTime = millis(); unsigned long highTime = millis(); unsigned long difference = 0; bool active = false; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); Serial.begin(9600); Serial.println("Starting button test"); } void loop() { int val = digitalRead(buttonPin); if (val == LOW && active == false) { lowTime = millis(); difference = lowTime - highTime; Serial.print("LOW "); Serial.println(difference); active = true; } else if (val == HIGH && active == true) { highTime = millis(); difference = highTime - lowTime; Serial.print("HIGH "); Serial.println(difference); active = false; } }