diff --git a/firmware/core/peripheral.cpp b/firmware/core/peripheral.cpp index e01ef80..6812807 100644 --- a/firmware/core/peripheral.cpp +++ b/firmware/core/peripheral.cpp @@ -100,7 +100,6 @@ pinMode(buttonPin, INPUT); digitalWrite(buttonPin, HIGH);//enable pull-up lastState = digitalRead(buttonPin); - lastChanged = millis(); latestAction = ActionNone; } @@ -108,20 +107,24 @@ { unsigned long now = millis(); uint8_t val = digitalRead(buttonPin); - if((val^lastState) && now > lastChanged){ - lastChanged = now; + if((val^lastState)){ lastState = val; if(val == LOW){ //key down pressTime = now; Serial.println("Button down"); }else{ //key up - if(now - pressTime > LongPressThreshold){ - latestAction = ActionLongPressed; - }else{ + if(now - pressTime > ValidPressThreshold + && now - pressTime < LongPressThreshold){ + latestAction = ActionPressed; } Serial.println("Button up"); } + }else{ + if(val == LOW && now - pressTime > LongPressThreshold){ + latestAction = ActionLongPressed; + pressTime = now; + } } } diff --git a/firmware/core/peripheral.h b/firmware/core/peripheral.h index 5d29c6b..0be44f8 100644 --- a/firmware/core/peripheral.h +++ b/firmware/core/peripheral.h @@ -8,6 +8,7 @@ #define alarmPin 6 #define DetectorSwitchDelay 80 +#define ValidPressThreshold 10 #define LongPressThreshold 3000 #define DoorPreparedOpenTimeOut 5000 #define DoorOpenedTimeOut 20000 @@ -56,7 +57,7 @@ ActionNone, ActionPressed, ActionLongPressed }; private: - unsigned long lastChanged, pressTime; + unsigned long pressTime; unsigned char lastState; Action latestAction; public: