This repository has been archived on 2024-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
space_status/arduino/door_status.ino

75 lines
1.7 KiB
Arduino
Raw Normal View History

// door status on arduino
// arduino is Duemilanove
// sensor is a reed sensor
2020-02-02 20:26:50 +01:00
const int REED_SWITCH_INPUT_PIN = 13;
2020-02-02 20:26:50 +01:00
const int RED_LED_OUTPUT_PIN = 12;
const int YELLOW_LED_OUTPUT_PIN = 11;
const int GREEN_LED_OUTPUT_PIN = 10;
void setup(){
2020-02-02 20:17:00 +01:00
Serial.begin(9600);
pinMode(REED_SWITCH_INPUT_PIN, INPUT);
pinMode(RED_LED_OUTPUT_PIN, OUTPUT);
pinMode(YELLOW_LED_OUTPUT_PIN, OUTPUT);
pinMode(GREEN_LED_OUTPUT_PIN, OUTPUT);
}
2020-02-02 20:29:20 +01:00
const int THRESHOLD = 20;
int space_status = THRESHOLD / 2;
int space_status_b4 = 0;
int delay_time = 1000;
void loop(){
int pin_status = 0;
2020-02-02 20:17:00 +01:00
pin_status = digitalRead(REED_SWITCH_INPUT_PIN);
Serial.print(" ");
Serial.print(space_status_b4);
Serial.print(" ");
Serial.println(space_status);
delay(delay_time);
2020-02-02 20:17:00 +01:00
// 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){
2020-02-02 20:29:20 +01:00
if (space_status < THRESHOLD){
space_status += 1;
}
}
// status check if we can switch the status.
// low pass prevents waggling a bit
2020-02-02 20:29:20 +01:00
if (space_status >= THRESHOLD-3) {
// closed
space_status_b4 = 1;
} else if (space_status <= 3) {
// open
space_status_b4 = 0;
}
// ampel / traffic light signals
if (space_status_b4 == 1) {
// closed
digitalWrite(RED_LED_OUTPUT_PIN, HIGH);
digitalWrite(GREEN_LED_OUTPUT_PIN, LOW);
} else if (space_status_b4 == 0) {
// open
digitalWrite(RED_LED_OUTPUT_PIN, LOW);
digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH);
}
2020-02-02 20:29:20 +01:00
if (space_status > 3 && space_status < THRESHOLD - 3) {
digitalWrite(YELLOW_LED_OUTPUT_PIN, HIGH);
} else {
digitalWrite(YELLOW_LED_OUTPUT_PIN, LOW);
}
2020-02-02 20:17:08 +01:00
}