From 5c2938ad4fd4a0f56f791c68e10c88ca4c7b0f45 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 7 Nov 2018 19:09:34 -0700 Subject: [PATCH] Add prearm state and abstract buttons --- firmware/firmware.ino | 44 +++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 758c1a6..59a4ad6 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -36,10 +36,10 @@ enum wifiStates enum lockStates { LOCK_OFF, + LOCK_PREARM, LOCK_ARMED, LOCK_ON_PRESSED, LOCK_ON, - LOCK_OFF_PRESSED, } lockState = LOCK_OFF; enum commStates @@ -112,6 +112,14 @@ void processWifiState() } } +bool greenButton() { return digitalRead(GREEN_BUTTON_PIN) == BUTTON_CLOSED; } +bool redButton() { return digitalRead(RED_BUTTON_PIN) == BUTTON_CLOSED; } + +void greenLEDOn() { digitalWrite(GREEN_LED_PIN, LED_ON); } +void greenLEDOff() { digitalWrite(GREEN_LED_PIN, LED_OFF); } +void redLEDOn() { digitalWrite(RED_LED_PIN, LED_ON); } +void redLEDOff() { digitalWrite(RED_LED_PIN, LED_OFF); } + void processLockState() { switch (lockState) { @@ -121,25 +129,33 @@ void processLockState() digitalWrite(RELAY_PIN, RELAY_OPEN); break; + case LOCK_PREARM: + if (!greenButton() && !redButton()) { + if (LOGGING) Serial.println("[INFO] Arming interlock."); + lockState = LOCK_ARMED; + } else { + lockState = LOCK_OFF; + } + break; case LOCK_ARMED: digitalWrite(GREEN_LED_PIN, LED_OFF); digitalWrite(RED_LED_PIN, LED_ON); digitalWrite(RELAY_PIN, RELAY_OPEN); - if (digitalRead(RED_BUTTON_PIN) == BUTTON_CLOSED) { + if (redButton()) { if (LOGGING) Serial.println("[INFO] Unarming interlock."); lockState = LOCK_OFF_PRESSED; - } else if (digitalRead(GREEN_BUTTON_PIN) == BUTTON_CLOSED) { + } else if (greenButton()) { if (LOGGING) Serial.println("[INFO] On button pressed."); lockState = LOCK_ON_PRESSED; } break; case LOCK_ON_PRESSED: - if (digitalRead(RED_BUTTON_PIN) == BUTTON_CLOSED) { - if (LOGGING) Serial.println("[ERROR] Both buttons pressed, aborting."); + if (redButton()) { + if (LOGGING) Serial.println("[ERROR] Off button pressed, aborting."); lockState = LOCK_OFF_PRESSED; - } else if (digitalRead(GREEN_BUTTON_PIN) == BUTTON_OPEN) { + } else if (!greenButton()) { if (LOGGING) Serial.println("[INFO] Turning machine on."); lockState = LOCK_ON; } @@ -150,14 +166,8 @@ void processLockState() digitalWrite(RELAY_PIN, RELAY_CLOSED); - if (digitalRead(RED_BUTTON_PIN) == BUTTON_CLOSED) { + if (redButton()) { if (LOGGING) Serial.println("[INFO] Off button pressed."); - lockState = LOCK_OFF_PRESSED; - } - break; - case LOCK_OFF_PRESSED: - if (digitalRead(RED_BUTTON_PIN) == BUTTON_OPEN) { - if (LOGGING) Serial.println("[INFO] Turning machine off."); lockState = LOCK_OFF; } break; @@ -229,11 +239,9 @@ void processCommState() if (LOGGING) Serial.println(payload); String action = deserializeJson(payload); - if (action == "arm" && lockState == LOCK_OFF && digitalRead(GREEN_BUTTON_PIN) == BUTTON_OPEN) { - lockState = LOCK_ARMED; - } else if (action == "disarm" && lockState == LOCK_ARMED) { - lockState = LOCK_OFF; - } else if (action == "disarm" && lockState == LOCK_ON) { + if (action == "arm" && lockState == LOCK_OFF) { + lockState = LOCK_PREARMED; + } else if (action == "disarm") { lockState = LOCK_OFF; }