File size: 700 Bytes
8e53a3e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /**
Determine how long a switch is pressed
6 APR 2025 by Blind Author
*/
#include <Arduino.h>
const int inPin = 4;
bool prevButtonState = HIGH;
unsigned long pressStartTime = 0;
unsigned long pressDuration = 0;
void setup() {
pinMode(inPin, INPUT);
Serial.begin(9600);
}
void loop() {
bool buttonState = digitalRead(inPin);
if (prevButtonState == HIGH && buttonState == LOW) {
pressStartTime = millis();
}
if (prevButtonState == LOW && buttonState == HIGH) {
pressDuration = (millis() - pressStartTime) / 1000;
Serial.print("Button pressed duration: ");
Serial.print(pressDuration);
Serial.println("s");
}
prevButtonState = buttonState;
delay(10);
}
|