From 760a9d1a4403310b737cc206ea66ecc088eb1878 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:17:00 +0100 Subject: [PATCH 01/28] Remove trailing whitespace --- arduino/door_status.ino | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index a6c00cb..b12e8fb 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -11,7 +11,7 @@ void setup(){ - Serial.begin(9600); + Serial.begin(9600); pinMode(IR_INPUT_PIN, INPUT); pinMode(space_status_red_PIN, OUTPUT); @@ -28,15 +28,15 @@ int delay_time = 1000; void loop(){ int pin_status = 0; - - + + pin_status = digitalRead(IR_INPUT_PIN); Serial.print(" "); Serial.print(space_status_b4); Serial.print(" "); Serial.println(space_status); delay(delay_time); - + // pin check of the reed sensor and low pass filter if (pin_status == 0){ @@ -49,7 +49,7 @@ void loop(){ } } - + // status check if we can switch the status. // low pass prevents waggling a bit if (space_status >= threshold-3) { -- 2.39.5 From 36169ba0cf322c953b61fad74a24b57dc35cd3e4 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:17:08 +0100 Subject: [PATCH 02/28] Add newline at end of file --- arduino/door_status.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index b12e8fb..e1b2897 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -77,4 +77,4 @@ void loop(){ } else { digitalWrite(space_status_yellow_PIN, LOW); } -} \ No newline at end of file +} -- 2.39.5 From 517d131689feeb2fc9af5a8fb918fdc645d103ad Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:17:53 +0100 Subject: [PATCH 03/28] Consistently use one empty line to separate thematic parts --- arduino/door_status.ino | 8 -------- 1 file changed, 8 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index e1b2897..22659a5 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -8,8 +8,6 @@ #define space_status_yellow_PIN 11 #define space_status_green_PIN 10 - - void setup(){ Serial.begin(9600); pinMode(IR_INPUT_PIN, INPUT); @@ -19,17 +17,14 @@ void setup(){ pinMode(space_status_green_PIN, OUTPUT); } - int threshold = 20; int space_status = threshold / 2; int space_status_b4 = 0; int delay_time = 1000; - void loop(){ int pin_status = 0; - pin_status = digitalRead(IR_INPUT_PIN); Serial.print(" "); Serial.print(space_status_b4); @@ -37,7 +32,6 @@ void loop(){ Serial.println(space_status); delay(delay_time); - // pin check of the reed sensor and low pass filter if (pin_status == 0){ if (space_status > 0){ @@ -49,7 +43,6 @@ void loop(){ } } - // status check if we can switch the status. // low pass prevents waggling a bit if (space_status >= threshold-3) { @@ -60,7 +53,6 @@ void loop(){ space_status_b4 = 0; } - // ampel / traffic light signals if (space_status_b4 == 1) { // closed -- 2.39.5 From ddde38ec7daeaa3ce171319f4b5d64f02cc6ee56 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:18:45 +0100 Subject: [PATCH 04/28] Rename constant to reflect new sensor type --- arduino/door_status.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 22659a5..5f7f36f 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -2,7 +2,7 @@ // arduino is Duemilanove // sensor is a reed sensor -#define IR_INPUT_PIN 13 +#define REED_SWITCH_INPUT_PIN 13 #define space_status_red_PIN 12 #define space_status_yellow_PIN 11 @@ -10,7 +10,7 @@ void setup(){ Serial.begin(9600); - pinMode(IR_INPUT_PIN, INPUT); + pinMode(REED_SWITCH_INPUT_PIN, INPUT); pinMode(space_status_red_PIN, OUTPUT); pinMode(space_status_yellow_PIN, OUTPUT); @@ -25,7 +25,7 @@ int delay_time = 1000; void loop(){ int pin_status = 0; - pin_status = digitalRead(IR_INPUT_PIN); + pin_status = digitalRead(REED_SWITCH_INPUT_PIN); Serial.print(" "); Serial.print(space_status_b4); Serial.print(" "); -- 2.39.5 From 7f3c4aba6b3d881b5fa81d899ef22939c30aac7f Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:20:41 +0100 Subject: [PATCH 05/28] Consistently use all caps for constants --- arduino/door_status.ino | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 5f7f36f..fe7869e 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -4,17 +4,17 @@ #define REED_SWITCH_INPUT_PIN 13 -#define space_status_red_PIN 12 -#define space_status_yellow_PIN 11 -#define space_status_green_PIN 10 +#define SPACE_STATUS_RED_PIN 12 +#define SPACE_STATUS_YELLOW_PIN 11 +#define SPACE_STATUS_GREEN_PIN 10 void setup(){ Serial.begin(9600); pinMode(REED_SWITCH_INPUT_PIN, INPUT); - pinMode(space_status_red_PIN, OUTPUT); - pinMode(space_status_yellow_PIN, OUTPUT); - pinMode(space_status_green_PIN, OUTPUT); + pinMode(SPACE_STATUS_RED_PIN, OUTPUT); + pinMode(SPACE_STATUS_YELLOW_PIN, OUTPUT); + pinMode(SPACE_STATUS_GREEN_PIN, OUTPUT); } int threshold = 20; @@ -56,17 +56,17 @@ void loop(){ // ampel / traffic light signals if (space_status_b4 == 1) { // closed - digitalWrite(space_status_red_PIN, HIGH); - digitalWrite(space_status_green_PIN, LOW); + digitalWrite(SPACE_STATUS_RED_PIN, HIGH); + digitalWrite(SPACE_STATUS_GREEN_PIN, LOW); } else if (space_status_b4 == 0) { // open - digitalWrite(space_status_red_PIN, LOW); - digitalWrite(space_status_green_PIN, HIGH); + digitalWrite(SPACE_STATUS_RED_PIN, LOW); + digitalWrite(SPACE_STATUS_GREEN_PIN, HIGH); } if (space_status > 3 && space_status < threshold - 3) { - digitalWrite(space_status_yellow_PIN, HIGH); + digitalWrite(SPACE_STATUS_YELLOW_PIN, HIGH); } else { - digitalWrite(space_status_yellow_PIN, LOW); + digitalWrite(SPACE_STATUS_YELLOW_PIN, LOW); } } -- 2.39.5 From 0871de8affa47e016d6c61bde4382756468f7fa7 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:22:16 +0100 Subject: [PATCH 06/28] Use common naming convention for PIN constants --- arduino/door_status.ino | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index fe7869e..4439d29 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -4,17 +4,17 @@ #define REED_SWITCH_INPUT_PIN 13 -#define SPACE_STATUS_RED_PIN 12 -#define SPACE_STATUS_YELLOW_PIN 11 -#define SPACE_STATUS_GREEN_PIN 10 +#define RED_LED_OUTPUT_PIN 12 +#define YELLOW_LED_OUTPUT_PIN 11 +#define GREEN_LED_OUTPUT_PIN 10 void setup(){ Serial.begin(9600); pinMode(REED_SWITCH_INPUT_PIN, INPUT); - pinMode(SPACE_STATUS_RED_PIN, OUTPUT); - pinMode(SPACE_STATUS_YELLOW_PIN, OUTPUT); - pinMode(SPACE_STATUS_GREEN_PIN, OUTPUT); + pinMode(RED_LED_OUTPUT_PIN, OUTPUT); + pinMode(YELLOW_LED_OUTPUT_PIN, OUTPUT); + pinMode(GREEN_LED_OUTPUT_PIN, OUTPUT); } int threshold = 20; @@ -56,17 +56,17 @@ void loop(){ // ampel / traffic light signals if (space_status_b4 == 1) { // closed - digitalWrite(SPACE_STATUS_RED_PIN, HIGH); - digitalWrite(SPACE_STATUS_GREEN_PIN, LOW); + digitalWrite(RED_LED_OUTPUT_PIN, HIGH); + digitalWrite(GREEN_LED_OUTPUT_PIN, LOW); } else if (space_status_b4 == 0) { // open - digitalWrite(SPACE_STATUS_RED_PIN, LOW); - digitalWrite(SPACE_STATUS_GREEN_PIN, HIGH); + digitalWrite(RED_LED_OUTPUT_PIN, LOW); + digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH); } if (space_status > 3 && space_status < threshold - 3) { - digitalWrite(SPACE_STATUS_YELLOW_PIN, HIGH); + digitalWrite(YELLOW_LED_OUTPUT_PIN, HIGH); } else { - digitalWrite(SPACE_STATUS_YELLOW_PIN, LOW); + digitalWrite(YELLOW_LED_OUTPUT_PIN, LOW); } } -- 2.39.5 From b9f9f55d2fa3ea0e8c80f2ec365ed25113797166 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:23:05 +0100 Subject: [PATCH 07/28] Separate serial setup from pin mode setup --- arduino/door_status.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 4439d29..a1910a4 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -10,6 +10,7 @@ void setup(){ Serial.begin(9600); + pinMode(REED_SWITCH_INPUT_PIN, INPUT); pinMode(RED_LED_OUTPUT_PIN, OUTPUT); -- 2.39.5 From af86687e432614ed5689b844f121538377be8276 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:26:50 +0100 Subject: [PATCH 08/28] Use const for constants --- arduino/door_status.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index a1910a4..e2f136f 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -2,11 +2,11 @@ // arduino is Duemilanove // sensor is a reed sensor -#define REED_SWITCH_INPUT_PIN 13 +const int REED_SWITCH_INPUT_PIN = 13; -#define RED_LED_OUTPUT_PIN 12 -#define YELLOW_LED_OUTPUT_PIN 11 -#define GREEN_LED_OUTPUT_PIN 10 +const int RED_LED_OUTPUT_PIN = 12; +const int YELLOW_LED_OUTPUT_PIN = 11; +const int GREEN_LED_OUTPUT_PIN = 10; void setup(){ Serial.begin(9600); -- 2.39.5 From 4db98d53cd2579d130a6fa6038302a5fc08eae65 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:29:20 +0100 Subject: [PATCH 09/28] Make threshold a constant --- arduino/door_status.ino | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index e2f136f..bfba537 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -18,8 +18,9 @@ void setup(){ pinMode(GREEN_LED_OUTPUT_PIN, OUTPUT); } -int threshold = 20; -int space_status = threshold / 2; +const int THRESHOLD = 20; + +int space_status = THRESHOLD / 2; int space_status_b4 = 0; int delay_time = 1000; @@ -39,14 +40,14 @@ void loop(){ space_status -= 1; } } else if (pin_status == 1){ - if (space_status < threshold){ + if (space_status < THRESHOLD){ space_status += 1; } } // status check if we can switch the status. // low pass prevents waggling a bit - if (space_status >= threshold-3) { + if (space_status >= THRESHOLD-3) { // closed space_status_b4 = 1; } else if (space_status <= 3) { @@ -65,7 +66,7 @@ void loop(){ digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH); } - if (space_status > 3 && space_status < threshold - 3) { + if (space_status > 3 && space_status < THRESHOLD - 3) { digitalWrite(YELLOW_LED_OUTPUT_PIN, HIGH); } else { digitalWrite(YELLOW_LED_OUTPUT_PIN, LOW); -- 2.39.5 From f6886a7a788a10cbca3ae4bf85e307133ffa0568 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:30:23 +0100 Subject: [PATCH 10/28] Make delay_time a constant --- arduino/door_status.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index bfba537..ff1a4f5 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -18,11 +18,11 @@ void setup(){ pinMode(GREEN_LED_OUTPUT_PIN, OUTPUT); } +const int DELAY_TIME = 1000; const int THRESHOLD = 20; int space_status = THRESHOLD / 2; int space_status_b4 = 0; -int delay_time = 1000; void loop(){ int pin_status = 0; @@ -32,7 +32,7 @@ void loop(){ Serial.print(space_status_b4); Serial.print(" "); Serial.println(space_status); - delay(delay_time); + delay(DELAY_TIME); // pin check of the reed sensor and low pass filter if (pin_status == 0){ -- 2.39.5 From 409ea1d05636cb2ad4d94cbfabd460ee0e67a580 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:37:12 +0100 Subject: [PATCH 11/28] Move delay back without changing the pin interaction timing --- arduino/door_status.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index ff1a4f5..6161ae3 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -32,7 +32,6 @@ void loop(){ Serial.print(space_status_b4); Serial.print(" "); Serial.println(space_status); - delay(DELAY_TIME); // pin check of the reed sensor and low pass filter if (pin_status == 0){ @@ -55,6 +54,8 @@ void loop(){ space_status_b4 = 0; } + delay(DELAY_TIME); + // ampel / traffic light signals if (space_status_b4 == 1) { // closed -- 2.39.5 From bf2b75404356f4c0d0580288ea5a1cbc856b8c9d Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:46:19 +0100 Subject: [PATCH 12/28] Move status printing to own function --- arduino/door_status.ino | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 6161ae3..0cfc237 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -24,14 +24,19 @@ const int THRESHOLD = 20; int space_status = THRESHOLD / 2; int space_status_b4 = 0; -void loop(){ - int pin_status = 0; - - pin_status = digitalRead(REED_SWITCH_INPUT_PIN); +void print_status() { Serial.print(" "); Serial.print(space_status_b4); Serial.print(" "); Serial.println(space_status); +} + +void loop(){ + print_status(); + + int pin_status = 0; + + pin_status = digitalRead(REED_SWITCH_INPUT_PIN); // pin check of the reed sensor and low pass filter if (pin_status == 0){ -- 2.39.5 From da152247ac936d3d1947c96018c9f0f08732720f Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:48:18 +0100 Subject: [PATCH 13/28] Move space_status update to own function --- arduino/door_status.ino | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 0cfc237..a3a0963 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -31,9 +31,7 @@ void print_status() { Serial.println(space_status); } -void loop(){ - print_status(); - +void update_space_status() { int pin_status = 0; pin_status = digitalRead(REED_SWITCH_INPUT_PIN); @@ -48,6 +46,12 @@ void loop(){ space_status += 1; } } +} + +void loop(){ + print_status(); + + update_space_status(); // status check if we can switch the status. // low pass prevents waggling a bit -- 2.39.5 From b14055bcfaec276bf80c34b83e70597295f6c33e Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:51:07 +0100 Subject: [PATCH 14/28] Simplify update_space_status --- arduino/door_status.ino | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index a3a0963..461ee11 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -32,19 +32,10 @@ void print_status() { } void update_space_status() { - int pin_status = 0; - - pin_status = digitalRead(REED_SWITCH_INPUT_PIN); - - // pin check of the reed sensor and low pass filter - if (pin_status == 0){ - if (space_status > 0){ - space_status -= 1; - } - } else if (pin_status == 1){ - if (space_status < THRESHOLD){ - space_status += 1; - } + if(LOW == digitalRead(REED_SWITCH_INPUT_PIN)) { + space_status = max(0, space_status - 1); + } else { + space_status = min(THRESHOLD, space_status + 1); } } -- 2.39.5 From 19a1c177c68db1afee2bfefd1da16961ba156c6b Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:53:07 +0100 Subject: [PATCH 15/28] Move update of space_status_b4 into own function --- arduino/door_status.ino | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 461ee11..9cbf3b8 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -39,11 +39,7 @@ void update_space_status() { } } -void loop(){ - print_status(); - - update_space_status(); - +void update_space_status_b4() { // status check if we can switch the status. // low pass prevents waggling a bit if (space_status >= THRESHOLD-3) { @@ -53,6 +49,14 @@ void loop(){ // open space_status_b4 = 0; } +} + +void loop(){ + print_status(); + + update_space_status(); + + update_space_status_b4(); delay(DELAY_TIME); -- 2.39.5 From 472d3995dabf6f2168928bc6173dfa769c2a0cd7 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:54:38 +0100 Subject: [PATCH 16/28] Move knowledge from comments into constant names --- arduino/door_status.ino | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 9cbf3b8..e91d5c5 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -20,9 +20,11 @@ void setup(){ const int DELAY_TIME = 1000; const int THRESHOLD = 20; +const int CLOSED_DOOR = 1; +const int OPEN_DOOR = 0; int space_status = THRESHOLD / 2; -int space_status_b4 = 0; +int published_door_state = OPEN_DOOR; void print_status() { Serial.print(" "); @@ -43,11 +45,9 @@ void update_space_status_b4() { // status check if we can switch the status. // low pass prevents waggling a bit if (space_status >= THRESHOLD-3) { - // closed - space_status_b4 = 1; + space_status_b4 = CLOSED_DOOR; } else if (space_status <= 3) { - // open - space_status_b4 = 0; + space_status_b4 = OPEN_DOOR; } } @@ -61,12 +61,10 @@ void loop(){ delay(DELAY_TIME); // ampel / traffic light signals - if (space_status_b4 == 1) { - // closed + if (space_status_b4 == CLOSED_DOOR) { digitalWrite(RED_LED_OUTPUT_PIN, HIGH); digitalWrite(GREEN_LED_OUTPUT_PIN, LOW); - } else if (space_status_b4 == 0) { - // open + } else if (space_status_b4 == OPEN_DOOR) { digitalWrite(RED_LED_OUTPUT_PIN, LOW); digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH); } -- 2.39.5 From 2bbd6c5f3b9cc89e05a8ec6b1654bb5d57236f61 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:56:31 +0100 Subject: [PATCH 17/28] Rename variable --- arduino/door_status.ino | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index e91d5c5..458eee0 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -24,11 +24,11 @@ const int CLOSED_DOOR = 1; const int OPEN_DOOR = 0; int space_status = THRESHOLD / 2; -int published_door_state = OPEN_DOOR; +int published_state = OPEN_DOOR; void print_status() { Serial.print(" "); - Serial.print(space_status_b4); + Serial.print(published_state); Serial.print(" "); Serial.println(space_status); } @@ -41,13 +41,13 @@ void update_space_status() { } } -void update_space_status_b4() { +void update_published_state() { // status check if we can switch the status. // low pass prevents waggling a bit if (space_status >= THRESHOLD-3) { - space_status_b4 = CLOSED_DOOR; + published_state = CLOSED_DOOR; } else if (space_status <= 3) { - space_status_b4 = OPEN_DOOR; + published_state = OPEN_DOOR; } } @@ -56,15 +56,15 @@ void loop(){ update_space_status(); - update_space_status_b4(); + update_published_state(); delay(DELAY_TIME); // ampel / traffic light signals - if (space_status_b4 == CLOSED_DOOR) { + if (published_state == CLOSED_DOOR) { digitalWrite(RED_LED_OUTPUT_PIN, HIGH); digitalWrite(GREEN_LED_OUTPUT_PIN, LOW); - } else if (space_status_b4 == OPEN_DOOR) { + } else if (published_state == OPEN_DOOR) { digitalWrite(RED_LED_OUTPUT_PIN, LOW); digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH); } -- 2.39.5 From 924d873aa97e51536e0627f224182e90e0dcdfd7 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 20:58:19 +0100 Subject: [PATCH 18/28] Rename variable --- arduino/door_status.ino | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 458eee0..7587235 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -23,30 +23,30 @@ const int THRESHOLD = 20; const int CLOSED_DOOR = 1; const int OPEN_DOOR = 0; -int space_status = THRESHOLD / 2; +int measured_state_counter = THRESHOLD / 2; int published_state = OPEN_DOOR; void print_status() { Serial.print(" "); Serial.print(published_state); Serial.print(" "); - Serial.println(space_status); + Serial.println(measured_state_counter); } -void update_space_status() { +void update_measured_state_counter() { if(LOW == digitalRead(REED_SWITCH_INPUT_PIN)) { - space_status = max(0, space_status - 1); + measured_state_counter = max(0, measured_state_counter - 1); } else { - space_status = min(THRESHOLD, space_status + 1); + measured_state_counter = min(THRESHOLD, measured_state_counter + 1); } } void update_published_state() { // status check if we can switch the status. // low pass prevents waggling a bit - if (space_status >= THRESHOLD-3) { + if (measured_state_counter >= THRESHOLD-3) { published_state = CLOSED_DOOR; - } else if (space_status <= 3) { + } else if (measured_state_counter <= 3) { published_state = OPEN_DOOR; } } @@ -54,7 +54,7 @@ void update_published_state() { void loop(){ print_status(); - update_space_status(); + update_measured_state_counter(); update_published_state(); @@ -69,7 +69,7 @@ void loop(){ digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH); } - if (space_status > 3 && space_status < THRESHOLD - 3) { + if (measured_state_counter > 3 && measured_state_counter < THRESHOLD - 3) { digitalWrite(YELLOW_LED_OUTPUT_PIN, HIGH); } else { digitalWrite(YELLOW_LED_OUTPUT_PIN, LOW); -- 2.39.5 From bd5dc49a9214970c5da6ffe0cd728ce80bc5d969 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 21:00:07 +0100 Subject: [PATCH 19/28] Move led update code to own function --- arduino/door_status.ino | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 7587235..0a2f58b 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -51,16 +51,7 @@ void update_published_state() { } } -void loop(){ - print_status(); - - update_measured_state_counter(); - - update_published_state(); - - delay(DELAY_TIME); - - // ampel / traffic light signals +void update_led_pins() { if (published_state == CLOSED_DOOR) { digitalWrite(RED_LED_OUTPUT_PIN, HIGH); digitalWrite(GREEN_LED_OUTPUT_PIN, LOW); @@ -75,3 +66,15 @@ void loop(){ digitalWrite(YELLOW_LED_OUTPUT_PIN, LOW); } } + +void loop(){ + print_status(); + + update_measured_state_counter(); + + update_published_state(); + + delay(DELAY_TIME); + + update_led_pins(); +} -- 2.39.5 From 4f29fcdb90448efd32c3ab5581fec3be4dd08299 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 21:02:21 +0100 Subject: [PATCH 20/28] Rename constant from THRESHOLD to MAX_COUNTER --- arduino/door_status.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 0a2f58b..44f1cab 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -19,11 +19,11 @@ void setup(){ } const int DELAY_TIME = 1000; -const int THRESHOLD = 20; +const int MAX_COUNTER = 20; const int CLOSED_DOOR = 1; const int OPEN_DOOR = 0; -int measured_state_counter = THRESHOLD / 2; +int measured_state_counter = MAX_COUNTER / 2; int published_state = OPEN_DOOR; void print_status() { @@ -44,7 +44,7 @@ void update_measured_state_counter() { void update_published_state() { // status check if we can switch the status. // low pass prevents waggling a bit - if (measured_state_counter >= THRESHOLD-3) { + if (measured_state_counter >= MAX_COUNTER-3) { published_state = CLOSED_DOOR; } else if (measured_state_counter <= 3) { published_state = OPEN_DOOR; @@ -60,7 +60,7 @@ void update_led_pins() { digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH); } - if (measured_state_counter > 3 && measured_state_counter < THRESHOLD - 3) { + if (measured_state_counter > 3 && measured_state_counter < MAX_COUNTER - 3) { digitalWrite(YELLOW_LED_OUTPUT_PIN, HIGH); } else { digitalWrite(YELLOW_LED_OUTPUT_PIN, LOW); -- 2.39.5 From aaab790f811541868cf7d17231eea12f449db561 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 21:05:03 +0100 Subject: [PATCH 21/28] Introduce constants LOWER_THRESHOLD and UPPER_THRESHOLD --- arduino/door_status.ino | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 44f1cab..6a99e41 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -20,6 +20,8 @@ void setup(){ const int DELAY_TIME = 1000; const int MAX_COUNTER = 20; +const int LOWER_THRESHOLD = 3; +const int UPPER_THRESHOLD = MAX_COUNTER - LOWER_THRESHOLD; const int CLOSED_DOOR = 1; const int OPEN_DOOR = 0; @@ -44,9 +46,9 @@ void update_measured_state_counter() { void update_published_state() { // status check if we can switch the status. // low pass prevents waggling a bit - if (measured_state_counter >= MAX_COUNTER-3) { + if (measured_state_counter >= UPPER_THRESHOLD) { published_state = CLOSED_DOOR; - } else if (measured_state_counter <= 3) { + } else if (measured_state_counter <= LOWER_THRESHOLD) { published_state = OPEN_DOOR; } } @@ -60,7 +62,7 @@ void update_led_pins() { digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH); } - if (measured_state_counter > 3 && measured_state_counter < MAX_COUNTER - 3) { + if (measured_state_counter > LOWER_THRESHOLD && measured_state_counter < UPPER_THRESHOLD) { digitalWrite(YELLOW_LED_OUTPUT_PIN, HIGH); } else { digitalWrite(YELLOW_LED_OUTPUT_PIN, LOW); -- 2.39.5 From ad97be12958f20c96a86fb223083f031e0a9764b Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 21:06:03 +0100 Subject: [PATCH 22/28] Express range check differently --- arduino/door_status.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 6a99e41..be4d001 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -20,7 +20,7 @@ void setup(){ const int DELAY_TIME = 1000; const int MAX_COUNTER = 20; -const int LOWER_THRESHOLD = 3; +const int LOWER_THRESHOLD = 4; const int UPPER_THRESHOLD = MAX_COUNTER - LOWER_THRESHOLD; const int CLOSED_DOOR = 1; const int OPEN_DOOR = 0; @@ -46,9 +46,9 @@ void update_measured_state_counter() { void update_published_state() { // status check if we can switch the status. // low pass prevents waggling a bit - if (measured_state_counter >= UPPER_THRESHOLD) { + if (measured_state_counter > UPPER_THRESHOLD) { published_state = CLOSED_DOOR; - } else if (measured_state_counter <= LOWER_THRESHOLD) { + } else if (measured_state_counter < LOWER_THRESHOLD) { published_state = OPEN_DOOR; } } @@ -62,7 +62,7 @@ void update_led_pins() { digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH); } - if (measured_state_counter > LOWER_THRESHOLD && measured_state_counter < UPPER_THRESHOLD) { + if (measured_state_counter == constrain(measured_state_counter, LOWER_THRESHOLD, UPPER_THRESHOLD)) { digitalWrite(YELLOW_LED_OUTPUT_PIN, HIGH); } else { digitalWrite(YELLOW_LED_OUTPUT_PIN, LOW); -- 2.39.5 From ae1a32d54910ad46f61797f6ad0da1ce3ffa93d5 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 21:10:22 +0100 Subject: [PATCH 23/28] Move constants and variables into one block --- arduino/door_status.ino | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index be4d001..7a21a3d 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -8,6 +8,18 @@ const int RED_LED_OUTPUT_PIN = 12; const int YELLOW_LED_OUTPUT_PIN = 11; const int GREEN_LED_OUTPUT_PIN = 10; +const int DELAY_TIME = 1000; + +const int MAX_COUNTER = 20; +const int LOWER_THRESHOLD = 4; +const int UPPER_THRESHOLD = MAX_COUNTER - LOWER_THRESHOLD; + +const int CLOSED_DOOR = 1; +const int OPEN_DOOR = 0; + +int measured_state_counter = MAX_COUNTER / 2; +int published_state = OPEN_DOOR; + void setup(){ Serial.begin(9600); @@ -18,16 +30,6 @@ void setup(){ pinMode(GREEN_LED_OUTPUT_PIN, OUTPUT); } -const int DELAY_TIME = 1000; -const int MAX_COUNTER = 20; -const int LOWER_THRESHOLD = 4; -const int UPPER_THRESHOLD = MAX_COUNTER - LOWER_THRESHOLD; -const int CLOSED_DOOR = 1; -const int OPEN_DOOR = 0; - -int measured_state_counter = MAX_COUNTER / 2; -int published_state = OPEN_DOOR; - void print_status() { Serial.print(" "); Serial.print(published_state); -- 2.39.5 From 7449b8700444bccae82eed6a07ab8cc74737bc29 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 21:11:06 +0100 Subject: [PATCH 24/28] Move loop before functions implementing the details --- arduino/door_status.ino | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 7a21a3d..0eedc1e 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -30,6 +30,18 @@ void setup(){ pinMode(GREEN_LED_OUTPUT_PIN, OUTPUT); } +void loop(){ + print_status(); + + update_measured_state_counter(); + + update_published_state(); + + delay(DELAY_TIME); + + update_led_pins(); +} + void print_status() { Serial.print(" "); Serial.print(published_state); @@ -70,15 +82,3 @@ void update_led_pins() { digitalWrite(YELLOW_LED_OUTPUT_PIN, LOW); } } - -void loop(){ - print_status(); - - update_measured_state_counter(); - - update_published_state(); - - delay(DELAY_TIME); - - update_led_pins(); -} -- 2.39.5 From 32cd4f2d35d02095cc565f7070a5955a8e91b349 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 21:11:52 +0100 Subject: [PATCH 25/28] Rename function --- arduino/door_status.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 0eedc1e..4409a56 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -31,7 +31,7 @@ void setup(){ } void loop(){ - print_status(); + print_state(); update_measured_state_counter(); @@ -42,7 +42,7 @@ void loop(){ update_led_pins(); } -void print_status() { +void print_state() { Serial.print(" "); Serial.print(published_state); Serial.print(" "); -- 2.39.5 From 8711a1783f6f273ba2517dab4e8eef4dcba94d32 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 21:26:33 +0100 Subject: [PATCH 26/28] Rename variable --- arduino/door_status.ino | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 4409a56..c50dbc2 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -18,7 +18,7 @@ const int CLOSED_DOOR = 1; const int OPEN_DOOR = 0; int measured_state_counter = MAX_COUNTER / 2; -int published_state = OPEN_DOOR; +int reported_state = OPEN_DOOR; void setup(){ Serial.begin(9600); @@ -35,7 +35,7 @@ void loop(){ update_measured_state_counter(); - update_published_state(); + update_reported_state(); delay(DELAY_TIME); @@ -44,7 +44,7 @@ void loop(){ void print_state() { Serial.print(" "); - Serial.print(published_state); + Serial.print(reported_state); Serial.print(" "); Serial.println(measured_state_counter); } @@ -57,21 +57,21 @@ void update_measured_state_counter() { } } -void update_published_state() { // status check if we can switch the status. // low pass prevents waggling a bit +void update_reported_state() { if (measured_state_counter > UPPER_THRESHOLD) { - published_state = CLOSED_DOOR; + reported_state = CLOSED_DOOR; } else if (measured_state_counter < LOWER_THRESHOLD) { - published_state = OPEN_DOOR; + reported_state = OPEN_DOOR; } } void update_led_pins() { - if (published_state == CLOSED_DOOR) { + if (reported_state == CLOSED_DOOR) { digitalWrite(RED_LED_OUTPUT_PIN, HIGH); digitalWrite(GREEN_LED_OUTPUT_PIN, LOW); - } else if (published_state == OPEN_DOOR) { + } else if (reported_state == OPEN_DOOR) { digitalWrite(RED_LED_OUTPUT_PIN, LOW); digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH); } -- 2.39.5 From 028911f9595b0b3afb4055431feb4d6759100c6b Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 21:27:17 +0100 Subject: [PATCH 27/28] Update documentation --- arduino/door_status.ino | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index c50dbc2..0c4d5ba 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -1,6 +1,21 @@ -// door status on arduino -// arduino is Duemilanove -// sensor is a reed sensor +/* Door state monitoring logic for Arduino Duemilanove + * + * This script periodically reads the state of a reed switch + * (REED_SWITCH_INPUT_PIN) to determine the locked state of door. + * + * The switch sometimes reports the wrong state for brief periods of time. + * This script filters out these quick changes. A counter + * (measured_state_counter) is decremented or incremented within a range + * from 0 to MAX_COUNTER depending on the state of the reed switch in each + * iteration of the loop. The reported state (reported_state via + * RED_LED_OUTPUT_PIN and GREEN_LED_OUTPUT_PIN) is only changed, if the counter + * reaches its lower (0 to LOWER_THRESHOLD) or upper end (UPPER_THRESHOLD to + * MAX_COUNTER). The scripts also reports when the state counter is between the + * lower and upper end (YELLOW_LED_OUTPUT_PIN). + * + * State counter and reported state are written to the serial port in each + * iteration for debugging purposes. + */ const int REED_SWITCH_INPUT_PIN = 13; @@ -57,8 +72,6 @@ void update_measured_state_counter() { } } - // status check if we can switch the status. - // low pass prevents waggling a bit void update_reported_state() { if (measured_state_counter > UPPER_THRESHOLD) { reported_state = CLOSED_DOOR; -- 2.39.5 From 2e90aed245b2a08852c94ef38e645b6f1ee8ec60 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sun, 2 Feb 2020 21:29:04 +0100 Subject: [PATCH 28/28] Revert split into separate functions --- arduino/door_status.ino | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 0c4d5ba..4095a8e 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -46,41 +46,29 @@ void setup(){ } void loop(){ - print_state(); - - update_measured_state_counter(); - - update_reported_state(); - - delay(DELAY_TIME); - - update_led_pins(); -} - -void print_state() { + // print state Serial.print(" "); Serial.print(reported_state); Serial.print(" "); Serial.println(measured_state_counter); -} -void update_measured_state_counter() { + // update measured state if(LOW == digitalRead(REED_SWITCH_INPUT_PIN)) { measured_state_counter = max(0, measured_state_counter - 1); } else { measured_state_counter = min(THRESHOLD, measured_state_counter + 1); } -} -void update_reported_state() { + // update reported state if (measured_state_counter > UPPER_THRESHOLD) { reported_state = CLOSED_DOOR; } else if (measured_state_counter < LOWER_THRESHOLD) { reported_state = OPEN_DOOR; } -} -void update_led_pins() { + delay(DELAY_TIME); + + // update led pins if (reported_state == CLOSED_DOOR) { digitalWrite(RED_LED_OUTPUT_PIN, HIGH); digitalWrite(GREEN_LED_OUTPUT_PIN, LOW); @@ -88,7 +76,6 @@ void update_led_pins() { digitalWrite(RED_LED_OUTPUT_PIN, LOW); digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH); } - if (measured_state_counter == constrain(measured_state_counter, LOWER_THRESHOLD, UPPER_THRESHOLD)) { digitalWrite(YELLOW_LED_OUTPUT_PIN, HIGH); } else { -- 2.39.5