From 3fafd8e72207c0c45a4ada10ec759fdad88aa059 Mon Sep 17 00:00:00 2001 From: bernd2k <36308246+bernd2k@users.noreply.github.com> Date: Sat, 18 Jan 2020 16:32:46 +0100 Subject: [PATCH] adds reed sensor to arduino adds reed sensor to arduino removes RPR220 sensor code adds a low pass of status to prevent toggling. --- arduino/door_status.ino | 102 +++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 44 deletions(-) diff --git a/arduino/door_status.ino b/arduino/door_status.ino index 805043f..a6c00cb 100644 --- a/arduino/door_status.ino +++ b/arduino/door_status.ino @@ -1,66 +1,80 @@ -# arduino Duemilanove -#define IR_INPUT_PIN A0 -#define IR_LED_PIN 13 +// door status on arduino +// arduino is Duemilanove +// sensor is a reed sensor -#define status_red_PIN 12 -#define status_yellow_PIN 11 -#define status_green_PIN 10 +#define IR_INPUT_PIN 13 + +#define space_status_red_PIN 12 +#define space_status_yellow_PIN 11 +#define space_status_green_PIN 10 void setup(){ Serial.begin(9600); pinMode(IR_INPUT_PIN, INPUT); - pinMode(IR_LED_PIN, OUTPUT); - pinMode(status_red_PIN, OUTPUT); - pinMode(status_yellow_PIN, OUTPUT); - pinMode(status_green_PIN, OUTPUT); + pinMode(space_status_red_PIN, OUTPUT); + pinMode(space_status_yellow_PIN, OUTPUT); + pinMode(space_status_green_PIN, OUTPUT); } -int threshold = 900; -int value_b4 = 900; +int threshold = 20; +int space_status = threshold / 2; +int space_status_b4 = 0; +int delay_time = 1000; -int value = 0; void loop(){ - int ambient = 0; - int lit = 0; + int pin_status = 0; - digitalWrite(IR_LED_PIN, LOW); - delay(5); //To give ADC and LED transition time - ambient = analogRead(IR_INPUT_PIN); - - digitalWrite(IR_LED_PIN, HIGH); - delay(5); - lit = analogRead(IR_INPUT_PIN); - - value = ((lit - ambient) + value_b4) / 2; // small filter - - Serial.print(value); + pin_status = digitalRead(IR_INPUT_PIN); Serial.print(" "); - Serial.println(ambient); - delay(500); + Serial.print(space_status_b4); + Serial.print(" "); + Serial.println(space_status); + delay(delay_time); + - if (value >= threshold) { - digitalWrite(status_red_PIN, HIGH); - digitalWrite(status_yellow_PIN, LOW); - digitalWrite(status_green_PIN, LOW); + // 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 (value < 977 && value >= 964) { - // digitalWrite(status_red_PIN, LOW); - // digitalWrite(status_yellow_PIN, HIGH); - // digitalWrite(status_green_PIN, LOW); - //} - - if (value < threshold) { - digitalWrite(status_red_PIN, LOW); - digitalWrite(status_yellow_PIN, LOW); - digitalWrite(status_green_PIN, HIGH); + + // status check if we can switch the status. + // low pass prevents waggling a bit + if (space_status >= threshold-3) { + // closed + space_status_b4 = 1; + } else if (space_status <= 3) { + // open + space_status_b4 = 0; } - value_b4 = value; -} + + // ampel / traffic light signals + if (space_status_b4 == 1) { + // closed + 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); + } + + if (space_status > 3 && space_status < threshold - 3) { + digitalWrite(space_status_yellow_PIN, HIGH); + } else { + digitalWrite(space_status_yellow_PIN, LOW); + } +} \ No newline at end of file