bugfix flare catch

This commit is contained in:
2020-09-18 08:49:46 +02:00
parent 48ae094e7c
commit 984031f95c
@@ -1,33 +1,37 @@
int ledPin = LED_BUILTIN;
int buttonPin = 2; // Button connected to D2 and GND
unsigned long lowTime = millis();
unsigned long highTime = millis();
unsigned long difference = 0;
int flare_threshold = 30;
bool active = false;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Starting button test");
Serial.println("Starting doorbell sensor");
}
void loop() {
int val = digitalRead(buttonPin);
if (val == LOW && active == false) {
lowTime = millis();
difference = lowTime - highTime;
Serial.print("LOW ");
Serial.println(difference);
active = true;
if (val == LOW) {
delay(flare_threshold); // catch flares
val = digitalRead(buttonPin);
if (val == LOW && active == false) {
Serial.println("RING");
digitalWrite(ledPin, HIGH);
active = true;
delay(250);
}
else if (val == HIGH) {
Serial.print("flare (<");
Serial.print(flare_threshold);
Serial.println(")");
}
}
else if (val == HIGH && active == true) {
highTime = millis();
difference = highTime - lowTime;
Serial.print("HIGH ");
Serial.println(difference);
else {
active = false;
digitalWrite(ledPin, LOW);
}
}