-
Notifications
You must be signed in to change notification settings - Fork 2
/
button.ino
39 lines (33 loc) · 874 Bytes
/
button.ino
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
37
38
39
void button_init(){
pinMode(BUTTON_PIN,INPUT_PULLUP);
}
int readButtonDebounce(int pin) {
const int DEBOUNCE_DELAY = 50;
static int buttonState, lastButtonState;
static long lastDebounceTime;
int out = 0;
int reading = digitalRead(pin);
if(reading != lastButtonState) lastDebounceTime = millis();
if((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
if (reading != buttonState) {
buttonState = reading;
// ACTIVE LOW
if(!buttonState) {
out = 1;
}
}
}
lastButtonState = reading;
return out;
}
void button_read(){
if(readButtonDebounce(BUTTON_PIN)){
Serial.println("[IO] Button pressed, toggling lock state");
if(iot_kernel.device_state == "locked") {
servo_unlock_request = true;
}
else if(iot_kernel.device_state == "unlocked") {
servo_lock_request = true;
}
}
}