button test
This commit is contained in:
@@ -1,35 +1,33 @@
|
|||||||
int ledPin = LED_BUILTIN;
|
int ledPin = LED_BUILTIN;
|
||||||
int buttonPin = 2; // Button connected to D2 and GND
|
int buttonPin = 2; // Button connected to D2 and GND
|
||||||
|
|
||||||
|
unsigned long lowTime = millis();
|
||||||
|
unsigned long highTime = millis();
|
||||||
|
unsigned long difference = 0;
|
||||||
bool active = false;
|
bool active = false;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(ledPin, OUTPUT);
|
pinMode(ledPin, OUTPUT);
|
||||||
pinMode(buttonPin, INPUT_PULLUP);
|
pinMode(buttonPin, INPUT_PULLUP);
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.println("Starting doorbell sensor");
|
Serial.println("Starting button test");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
int val = digitalRead(buttonPin);
|
int val = digitalRead(buttonPin);
|
||||||
|
|
||||||
if (val == LOW) {
|
if (val == LOW && active == false) {
|
||||||
delay(2); // catch flares
|
lowTime = millis();
|
||||||
if (val == LOW && active == false) {
|
difference = lowTime - highTime;
|
||||||
Serial.println("RING");
|
Serial.print("LOW ");
|
||||||
digitalWrite(ledPin, HIGH);
|
Serial.println(difference);
|
||||||
active = true;
|
active = true;
|
||||||
delay(250);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Serial.println("flare");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else if (val == HIGH && active == true) {
|
||||||
delay(2); // catch flares
|
highTime = millis();
|
||||||
if (val == HIGH) {
|
difference = highTime - lowTime;
|
||||||
active = false;
|
Serial.print("HIGH ");
|
||||||
digitalWrite(ledPin, LOW);
|
Serial.println(difference);
|
||||||
}
|
active = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user