Motion sensor withput the interrupt
-
Hello,
I'm planning on building a node with motion sensor, light detector and a dimmer.
For it to work, I guess I can't have the node to sleep, so it will be able to receive command from the gateway.
So my question is how to adapt the motion sensor sketch to not use the interrupt.Would something like this work?
void loop() { boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) if(tripped== HIGH) sendTripped(); //send light level if changed //dimmer control code } function sendTripped() { Serial.println(tripped); gw.sendVariable(CHILD_ID, V_TRIPPED, "1"); }
-
The above should work, but i would also set an extra variable to check if there has been no movement at first. Otherwise it could continuously send movement.
Just as example:boolean tripped = false; boolean prevTripped = false; void loop() { tripped = (digitalRead(DIGITAL_INPUT_SENSOR)==HIGH)?true:false; //// Only send when there has been no movement first. if(tripped==true and prevTripped==false) { sendTripped(); } prevTripped = tripped; //send light level if changed //dimmer control code } function sendTripped() { Serial.println(tripped); gw.sendVariable(CHILD_ID, V_TRIPPED, "1"); }
-
@ferpando You can just leave the interrupt in. It will no longer wake the Arduino from sleep (as it won't be sleeping) but the interrupt will still be handled on motion!
-
Yes I thought so, but also thought the interrupt could mess with the rest of the sketch?
If the node receives a message from gateway and an interrupt happens, how would it behave?
-
@John
It was just a quick proof of concept.. I guess I should set tripped to false once the message is sent
-
If the node receives a message from gateway and an interrupt happens, how would it behave?
I asked almost the same question in an other thread, and i got the answer it is not interrupt save... I can not exactly say how it would behave.
-
Out of curiosity...how did you solve this?
-
I think you are on the right track. There' s no need for the interrupt in your case. So you don't have to worry about what process is being interrupted.
@ferpando said:
It was just a quick proof of concept.. I guess I should set tripped to false once the message is sent
John had the right logic. You don't want to set tripped false until the input (ie: motion dectector output) goes false, not when a message is sent.
His suggested logic sends one message whenever the motion detector goes active, and not again until it goes inactive and active again.
(Which happens after no motion for some time period adjustable via a pot on the motion detector).
Suggested Topics
-
Over the air updates
General Discussion • 23 Mar 2014, 21:38 • ToSa 1 Mar 2015, 11:21 -
Does MySensors require Arduino Timer 2?
Hardware • 6 Jan 2024, 19:39 • OldSurferDude 11 Jan 2024, 04:39 -
Arduino Nano Every problem
Hardware • 1 Feb 2020, 09:18 • hakha4 5 Dec 2023, 02:21 -
Which one should i choose? PIR or microwave sensor
Hardware • 2 May 2023, 10:58 • h01 11 May 2023, 13:56 -
hlk-pm01 are to noisy for rfm69?
Hardware • 5 Aug 2023, 21:16 • Tmaster 9 Aug 2023, 16:28 -
Sensors and more
Hardware • 19 Jun 2023, 00:41 • Robert Leverett 19 Jun 2023, 00:41