adds reed sensor to arduino

adds reed sensor to arduino
removes RPR220 sensor code
adds a low pass of status to prevent toggling.
This commit is contained in:
bernd2k 2020-01-18 16:32:46 +01:00
parent 6ae6dad3dd
commit 3fafd8e722

View file

@ -1,66 +1,80 @@
# arduino Duemilanove // door status on arduino
#define IR_INPUT_PIN A0 // arduino is Duemilanove
#define IR_LED_PIN 13 // sensor is a reed sensor
#define status_red_PIN 12 #define IR_INPUT_PIN 13
#define status_yellow_PIN 11
#define status_green_PIN 10 #define space_status_red_PIN 12
#define space_status_yellow_PIN 11
#define space_status_green_PIN 10
void setup(){ void setup(){
Serial.begin(9600); Serial.begin(9600);
pinMode(IR_INPUT_PIN, INPUT); pinMode(IR_INPUT_PIN, INPUT);
pinMode(IR_LED_PIN, OUTPUT);
pinMode(status_red_PIN, OUTPUT); pinMode(space_status_red_PIN, OUTPUT);
pinMode(status_yellow_PIN, OUTPUT); pinMode(space_status_yellow_PIN, OUTPUT);
pinMode(status_green_PIN, OUTPUT); pinMode(space_status_green_PIN, OUTPUT);
} }
int threshold = 900; int threshold = 20;
int value_b4 = 900; int space_status = threshold / 2;
int space_status_b4 = 0;
int delay_time = 1000;
int value = 0;
void loop(){ void loop(){
int ambient = 0; int pin_status = 0;
int lit = 0;
digitalWrite(IR_LED_PIN, LOW); pin_status = digitalRead(IR_INPUT_PIN);
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);
Serial.print(" "); Serial.print(" ");
Serial.println(ambient); Serial.print(space_status_b4);
delay(500); Serial.print(" ");
Serial.println(space_status);
delay(delay_time);
if (value >= threshold) {
digitalWrite(status_red_PIN, HIGH); // pin check of the reed sensor and low pass filter
digitalWrite(status_yellow_PIN, LOW); if (pin_status == 0){
digitalWrite(status_green_PIN, LOW); 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) { // status check if we can switch the status.
digitalWrite(status_red_PIN, LOW); // low pass prevents waggling a bit
digitalWrite(status_yellow_PIN, LOW); if (space_status >= threshold-3) {
digitalWrite(status_green_PIN, HIGH); // 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);
}
} }