Binary Switch Timer
-
I am trying to use the Binary switch example to monitor the state of a Switch .I want to be able to measure the length of time the switch is not in its normal state e.g.. "how long was the door/window open" I have found numerous timer sketch's but they seem to be for timing when something should happen . Anyone have suggestions or example sketch's
-
I am trying to use the Binary switch example to monitor the state of a Switch .I want to be able to measure the length of time the switch is not in its normal state e.g.. "how long was the door/window open" I have found numerous timer sketch's but they seem to be for timing when something should happen . Anyone have suggestions or example sketch's
@mikeg291 You can store the epoch time on state open (now();) and again on state close and subtract those values.
But you have to init the time module with the GW time first. something like this sketch should work
#include <MySensor.h> #include <SPI.h> #include <Bounce2.h> #include <Time.h> #define CHILD_ID 3 #define BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch MySensor gw; Bounce debouncer = Bounce(); int oldValue=-1; time_t lastSwitchTime = 0; void receiveTime(unsigned long controllerTime); bool timeReceived = false; // Change to V_LIGHT if you use S_LIGHT in presentation below MyMessage msg(CHILD_ID,V_TRIPPED); void setup() { gw.begin(); // Setup the button pinMode(BUTTON_PIN,INPUT); // Activate internal pull-up digitalWrite(BUTTON_PIN,HIGH); // After setting up the button, setup debouncer debouncer.attach(BUTTON_PIN); debouncer.interval(5); // Register binary input sensor to gw (they will be created as child devices) // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. // If S_LIGHT is used, remember to update variable type you send in. See "msg" above. gw.present(CHILD_ID, S_DOOR); // Make sure we get the system time, repeat request every 5 seconds until we have a valid time gw.requestTime(receiveTime); unsigned long lastRequest = millis(); if ((millis() - lastRequest) > 5000) { gw.wait(100); gw.requestTime(receiveTime); lastRequest = millis(); } lastSwitchTime = now(); } // Check if digital input has changed and send in new value void loop() { debouncer.update(); // Get the update value int value = debouncer.read(); if (value != oldValue) { Serial.print("Switch was "); Serial.print(oldValue ? "ON" : "OFF"); Serial.print(" for: "); Serial.print(now() - lastSwitchTime); Serial.println(" seconds"); lastSwitchTime = now(); // Send in the new value gw.send(msg.set(value==HIGH ? 1 : 0)); oldValue = value; } } // This is called when a new time value was received void receiveTime(unsigned long controllerTime) { // OK, set incoming time Serial.print(F("Time value received: ")); Serial.println(controllerTime); setTime(controllerTime); timeReceived = true; } -
BartE I hope this isn't a test because I just failed . I get the following error when compiling .
Arduino: 1.6.7 (Windows 7), Board: "Arduino Nano, ATmega328"C:\Users\repair\AppData\Local\Temp\arduino_7e89e922452d95ce255568999142a32b\sketch_mar10a.ino: In function 'void setup()':
sketch_mar10a:43: error: expected primary-expression before ')' token
if (( millis()- ) { ^sketch_mar10a:43: error: expected ')' before '{' token
if (( millis()- ) { ^sketch_mar10a:47: error: expected primary-expression before '}' token
} ^sketch_mar10a:47: error: expected ';' before '}' token
exit status 1
expected primary-expression before ')' tokenThis report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences. -
BartE I hope this isn't a test because I just failed . I get the following error when compiling .
Arduino: 1.6.7 (Windows 7), Board: "Arduino Nano, ATmega328"C:\Users\repair\AppData\Local\Temp\arduino_7e89e922452d95ce255568999142a32b\sketch_mar10a.ino: In function 'void setup()':
sketch_mar10a:43: error: expected primary-expression before ')' token
if (( millis()- ) { ^sketch_mar10a:43: error: expected ')' before '{' token
if (( millis()- ) { ^sketch_mar10a:47: error: expected primary-expression before '}' token
} ^sketch_mar10a:47: error: expected ';' before '}' token
exit status 1
expected primary-expression before ')' tokenThis report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences. -
@mikeg291 Mm sorry must have been a copy/past error when posting the sketch i 've made an update you can try again.
That fixed the issue. It works great. To further enhance the sketch I added an alarm ,however I get an error as shown . Is there a way ; once the sent value of the alarm is set high that it is not changed based on future reading . requiring an acknowledgement or reset .
#include <MySensor.h> #include <SPI.h> #include <Bounce2.h> #include <Time.h> #define CHILD_ID 3 #define BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch const int alarmtime = 15; MySensor gw; Bounce debouncer = Bounce(); int oldValue=-1; time_t lastSwitchTime = 0; void receiveTime(unsigned long controllerTime); bool timeReceived = false; // Change to V_LIGHT if you use S_LIGHT in presentation below MyMessage msg(CHILD_ID,V_TRIPPED); void setup() { gw.begin(); // Serial.begin(9600); // Setup the button pinMode(BUTTON_PIN,INPUT); // Activate internal pull-up digitalWrite(BUTTON_PIN,HIGH); // After setting up the button, setup debouncer debouncer.attach(BUTTON_PIN); debouncer.interval(5); // Register binary input sensor to gw (they will be created as child devices) // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. // If S_LIGHT is used, remember to update variable type you send in. See "msg" above. gw.present(CHILD_ID, S_DOOR); // Make sure we get the system time, repeat request every 5 seconds until we have a valid time gw.requestTime(receiveTime); unsigned long lastRequest = millis(); if ((millis() - lastRequest) > 5000) { gw.wait(100); gw.requestTime(receiveTime); lastRequest = millis(); } lastSwitchTime = now(); } // Check if digital input has changed and send in new value void loop() { debouncer.update(); // Get the update value int value = debouncer.read(); if (value != oldValue) { Serial.print("Switch was "); Serial.print(oldValue ? "ON" : "OFF"); Serial.print(" for: "); Serial.print(now() - lastSwitchTime); Serial.println(" seconds"); lastSwitchTime = now(); // Send in the new value if (value < == alarmtime ); gw.send(msg.set(value==HIGH 1)); else gw.send(msg.set(value=LOW 0)); oldValue = value; } // This is called when a new time value was received void receiveTime(unsigned long controllerTime) { // OK, set incoming time Serial.print(F("Time value received: ")); Serial.println(controllerTime); setTime(controllerTime); timeReceived = true; } -
That fixed the issue. It works great. To further enhance the sketch I added an alarm ,however I get an error as shown . Is there a way ; once the sent value of the alarm is set high that it is not changed based on future reading . requiring an acknowledgement or reset .
#include <MySensor.h> #include <SPI.h> #include <Bounce2.h> #include <Time.h> #define CHILD_ID 3 #define BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch const int alarmtime = 15; MySensor gw; Bounce debouncer = Bounce(); int oldValue=-1; time_t lastSwitchTime = 0; void receiveTime(unsigned long controllerTime); bool timeReceived = false; // Change to V_LIGHT if you use S_LIGHT in presentation below MyMessage msg(CHILD_ID,V_TRIPPED); void setup() { gw.begin(); // Serial.begin(9600); // Setup the button pinMode(BUTTON_PIN,INPUT); // Activate internal pull-up digitalWrite(BUTTON_PIN,HIGH); // After setting up the button, setup debouncer debouncer.attach(BUTTON_PIN); debouncer.interval(5); // Register binary input sensor to gw (they will be created as child devices) // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. // If S_LIGHT is used, remember to update variable type you send in. See "msg" above. gw.present(CHILD_ID, S_DOOR); // Make sure we get the system time, repeat request every 5 seconds until we have a valid time gw.requestTime(receiveTime); unsigned long lastRequest = millis(); if ((millis() - lastRequest) > 5000) { gw.wait(100); gw.requestTime(receiveTime); lastRequest = millis(); } lastSwitchTime = now(); } // Check if digital input has changed and send in new value void loop() { debouncer.update(); // Get the update value int value = debouncer.read(); if (value != oldValue) { Serial.print("Switch was "); Serial.print(oldValue ? "ON" : "OFF"); Serial.print(" for: "); Serial.print(now() - lastSwitchTime); Serial.println(" seconds"); lastSwitchTime = now(); // Send in the new value if (value < == alarmtime ); gw.send(msg.set(value==HIGH 1)); else gw.send(msg.set(value=LOW 0)); oldValue = value; } // This is called when a new time value was received void receiveTime(unsigned long controllerTime) { // OK, set incoming time Serial.print(F("Time value received: ")); Serial.println(controllerTime); setTime(controllerTime); timeReceived = true; }@mikeg291 Arduino: 1.6.7 (Windows 7), Board: "Arduino Nano, ATmega328"
C:\Users\repair\AppData\Local\Temp\arduino_9fa690567122ca1856b95eff67fbf9b8\pump_cycle_time_2.5.ino: In function 'void loop()':
pump_cycle_time_2.5:66: error: expected primary-expression before '==' token
if (value < == alarmtime ); ^pump_cycle_time_2.5:67: error: expected ')' before numeric constant
gw.send(msg.set(value==HIGH 1)); ^pump_cycle_time_2.5:68: error: expected '}' before 'else'
else ^pump_cycle_time_2.5:69: error: expected ')' before numeric constant
gw.send(msg.set(value=LOW 0)); ^C:\Users\repair\AppData\Local\Temp\arduino_9fa690567122ca1856b95eff67fbf9b8\pump_cycle_time_2.5.ino: At global scope:
pump_cycle_time_2.5:73: error: expected declaration before '}' token
}
^
exit status 1
expected primary-expression before '==' tokenThis report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login