const byte inPin = 3;
unsigned long timeDifference = 0;
unsigned long lastHigh = 0;
unsigned long actHigh;
byte lastState = HIGH;
byte state;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(inPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
state = debounced();
if (lastState != state) {
if (state) {
actHigh = millis();
if (lastHigh > 0) {
timeDifference = actHigh - lastHigh;
Serial.println(timeDifference);
}
lastHigh = actHigh;
}
lastState = state;
}
}
byte debounced(){
static unsigned long lastTime = 0;
static byte lastS = HIGH;
static byte ReturnState = HIGH;
byte actState = digitalRead(inPin);
if (actState != lastS){
lastS = actState;
lastTime = millis();
}
if (millis()-lastTime > 30) {
ReturnState = actState;
}
return ReturnState;
}