diff --git a/doorbell/klingel_sender_nano_serial/klingel_sender_nano_serial.ino b/doorbell/klingel_sender_nano_serial/klingel_sender_nano_serial.ino index d03c3b5..113f4c6 100644 --- a/doorbell/klingel_sender_nano_serial/klingel_sender_nano_serial.ino +++ b/doorbell/klingel_sender_nano_serial/klingel_sender_nano_serial.ino @@ -1,12 +1,15 @@ -/* Klingel-Sensor auf Arduino Nano ATmega168 - * +/* Klingel-Sensor auf Arduino Nano ATmega168 plus zweiter Button für das Garagentor + * Die Buttons sind über Optokoppler mit dem Arduino verbunden, + * um den Spannungsabfall über die lange Leitung auszugleichen. + * * https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button * https://docs.arduino.cc/tutorials/generic/digital-input-pullup/ */ const int ledPin = LED_BUILTIN; -const int buttonPin = 2; // Button connected to D2 +const int doorbellButtonPin = 2; // Button connected to D2 +const int buttonPin = 4; // Button connected to D4 const int flare_threshold = 200; const int ring_delay = 250; @@ -16,6 +19,7 @@ bool active = false; void setup() { pinMode(ledPin, OUTPUT); + pinMode(doorbellButtonPin, INPUT_PULLUP); pinMode(buttonPin, INPUT_PULLUP); Serial.begin(9600); Serial.println("Doorbell sensor"); @@ -23,28 +27,50 @@ void setup() { void loop() { - int val = digitalRead(buttonPin); - - + int val = digitalRead(doorbellButtonPin); + + if (val == LOW) { + delay(flare_threshold); // catch flares + val = digitalRead(doorbellButtonPin); + + if (val == LOW && active == false) { + Serial.println("RING"); + active = true; + digitalWrite(ledPin, HIGH); + delay(ring_delay); + } + else if (val == HIGH) { + Serial.print("flare (< "); + Serial.print(flare_threshold); + Serial.println(" ms)"); + } + } + else { + active = false; + digitalWrite(ledPin, LOW); + } + + + val = digitalRead(buttonPin); + if (val == LOW) { delay(flare_threshold); // catch flares val = digitalRead(buttonPin); if (val == LOW && active == false) { - Serial.println("RING"); - digitalWrite(ledPin, HIGH); + Serial.println("CLICK"); active = true; + digitalWrite(ledPin, HIGH); delay(ring_delay); } else if (val == HIGH) { - Serial.print("flare (<"); + Serial.print("flare (< "); Serial.print(flare_threshold); - Serial.println(")"); + Serial.println(" ms)"); } } else { - digitalWrite(ledPin, LOW); active = false; + digitalWrite(ledPin, LOW); } -} - +} \ No newline at end of file