2020-01-18 16:32:46 +01:00
|
|
|
// door status on arduino
|
|
|
|
// arduino is Duemilanove
|
|
|
|
// sensor is a reed sensor
|
2019-11-30 17:42:36 +01:00
|
|
|
|
2020-02-02 20:26:50 +01:00
|
|
|
const int REED_SWITCH_INPUT_PIN = 13;
|
2020-01-18 16:32:46 +01:00
|
|
|
|
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;
|
2019-11-30 17:42:36 +01:00
|
|
|
|
|
|
|
void setup(){
|
2020-02-02 20:17:00 +01:00
|
|
|
Serial.begin(9600);
|
2020-02-02 20:23:05 +01:00
|
|
|
|
2020-02-02 20:18:45 +01:00
|
|
|
pinMode(REED_SWITCH_INPUT_PIN, INPUT);
|
2019-11-30 17:42:36 +01:00
|
|
|
|
2020-02-02 20:22:16 +01:00
|
|
|
pinMode(RED_LED_OUTPUT_PIN, OUTPUT);
|
|
|
|
pinMode(YELLOW_LED_OUTPUT_PIN, OUTPUT);
|
|
|
|
pinMode(GREEN_LED_OUTPUT_PIN, OUTPUT);
|
2019-11-30 17:42:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 20:30:23 +01:00
|
|
|
const int DELAY_TIME = 1000;
|
2020-02-02 20:29:20 +01:00
|
|
|
const int THRESHOLD = 20;
|
2020-02-02 20:54:38 +01:00
|
|
|
const int CLOSED_DOOR = 1;
|
|
|
|
const int OPEN_DOOR = 0;
|
2020-02-02 20:29:20 +01:00
|
|
|
|
|
|
|
int space_status = THRESHOLD / 2;
|
2020-02-02 20:56:31 +01:00
|
|
|
int published_state = OPEN_DOOR;
|
2019-11-30 17:42:36 +01:00
|
|
|
|
2020-02-02 20:46:19 +01:00
|
|
|
void print_status() {
|
2020-01-18 16:32:46 +01:00
|
|
|
Serial.print(" ");
|
2020-02-02 20:56:31 +01:00
|
|
|
Serial.print(published_state);
|
2019-11-30 17:42:36 +01:00
|
|
|
Serial.print(" ");
|
2020-01-18 16:32:46 +01:00
|
|
|
Serial.println(space_status);
|
2020-02-02 20:46:19 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 20:48:18 +01:00
|
|
|
void update_space_status() {
|
2020-02-02 20:51:07 +01:00
|
|
|
if(LOW == digitalRead(REED_SWITCH_INPUT_PIN)) {
|
|
|
|
space_status = max(0, space_status - 1);
|
|
|
|
} else {
|
|
|
|
space_status = min(THRESHOLD, space_status + 1);
|
2020-01-18 16:32:46 +01:00
|
|
|
}
|
2020-02-02 20:48:18 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 20:56:31 +01:00
|
|
|
void update_published_state() {
|
2020-01-18 16:32:46 +01:00
|
|
|
// 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) {
|
2020-02-02 20:56:31 +01:00
|
|
|
published_state = CLOSED_DOOR;
|
2020-01-18 16:32:46 +01:00
|
|
|
} else if (space_status <= 3) {
|
2020-02-02 20:56:31 +01:00
|
|
|
published_state = OPEN_DOOR;
|
2019-11-30 17:42:36 +01:00
|
|
|
}
|
2020-02-02 20:53:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop(){
|
|
|
|
print_status();
|
|
|
|
|
|
|
|
update_space_status();
|
|
|
|
|
2020-02-02 20:56:31 +01:00
|
|
|
update_published_state();
|
2019-11-30 17:42:36 +01:00
|
|
|
|
2020-02-02 20:37:12 +01:00
|
|
|
delay(DELAY_TIME);
|
|
|
|
|
2020-01-18 16:32:46 +01:00
|
|
|
// ampel / traffic light signals
|
2020-02-02 20:56:31 +01:00
|
|
|
if (published_state == CLOSED_DOOR) {
|
2020-02-02 20:22:16 +01:00
|
|
|
digitalWrite(RED_LED_OUTPUT_PIN, HIGH);
|
|
|
|
digitalWrite(GREEN_LED_OUTPUT_PIN, LOW);
|
2020-02-02 20:56:31 +01:00
|
|
|
} else if (published_state == OPEN_DOOR) {
|
2020-02-02 20:22:16 +01:00
|
|
|
digitalWrite(RED_LED_OUTPUT_PIN, LOW);
|
|
|
|
digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH);
|
2019-11-30 17:42:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 20:29:20 +01:00
|
|
|
if (space_status > 3 && space_status < THRESHOLD - 3) {
|
2020-02-02 20:22:16 +01:00
|
|
|
digitalWrite(YELLOW_LED_OUTPUT_PIN, HIGH);
|
2020-01-18 16:32:46 +01:00
|
|
|
} else {
|
2020-02-02 20:22:16 +01:00
|
|
|
digitalWrite(YELLOW_LED_OUTPUT_PIN, LOW);
|
2020-01-18 16:32:46 +01:00
|
|
|
}
|
2020-02-02 20:17:08 +01:00
|
|
|
}
|