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

85 lines
2 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;
2020-02-02 20:30:23 +01:00
const int DELAY_TIME = 1000;
const int MAX_COUNTER = 20;
2020-02-02 21:06:03 +01:00
const int LOWER_THRESHOLD = 4;
const int UPPER_THRESHOLD = MAX_COUNTER - LOWER_THRESHOLD;
const int CLOSED_DOOR = 1;
const int OPEN_DOOR = 0;
2020-02-02 20:29:20 +01:00
int measured_state_counter = MAX_COUNTER / 2;
2020-02-02 21:26:33 +01:00
int reported_state = OPEN_DOOR;
void setup(){
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);
}
void loop(){
2020-02-02 21:11:52 +01:00
print_state();
update_measured_state_counter();
2020-02-02 21:26:33 +01:00
update_reported_state();
delay(DELAY_TIME);
update_led_pins();
}
2020-02-02 21:11:52 +01:00
void print_state() {
Serial.print(" ");
2020-02-02 21:26:33 +01:00
Serial.print(reported_state);
Serial.print(" ");
2020-02-02 20:58:19 +01:00
Serial.println(measured_state_counter);
2020-02-02 20:46:19 +01:00
}
2020-02-02 20:58:19 +01:00
void update_measured_state_counter() {
2020-02-02 20:51:07 +01:00
if(LOW == digitalRead(REED_SWITCH_INPUT_PIN)) {
2020-02-02 20:58:19 +01:00
measured_state_counter = max(0, measured_state_counter - 1);
2020-02-02 20:51:07 +01:00
} else {
2020-02-02 20:58:19 +01:00
measured_state_counter = min(THRESHOLD, measured_state_counter + 1);
}
}
// status check if we can switch the status.
// low pass prevents waggling a bit
2020-02-02 21:26:33 +01:00
void update_reported_state() {
2020-02-02 21:06:03 +01:00
if (measured_state_counter > UPPER_THRESHOLD) {
2020-02-02 21:26:33 +01:00
reported_state = CLOSED_DOOR;
2020-02-02 21:06:03 +01:00
} else if (measured_state_counter < LOWER_THRESHOLD) {
2020-02-02 21:26:33 +01:00
reported_state = OPEN_DOOR;
}
}
2020-02-02 21:00:07 +01:00
void update_led_pins() {
2020-02-02 21:26:33 +01:00
if (reported_state == CLOSED_DOOR) {
digitalWrite(RED_LED_OUTPUT_PIN, HIGH);
digitalWrite(GREEN_LED_OUTPUT_PIN, LOW);
2020-02-02 21:26:33 +01:00
} else if (reported_state == OPEN_DOOR) {
digitalWrite(RED_LED_OUTPUT_PIN, LOW);
digitalWrite(GREEN_LED_OUTPUT_PIN, HIGH);
}
2020-02-02 21:06:03 +01:00
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);
}
2020-02-02 20:17:08 +01:00
}