attachInterrupt and sleep()
-
Hi,
Can anyone tell me if mcu wakes up if IRQ is changing in the following setup? OR do I have to use sleep(digitalPinToInterrupt(IRQ), CHANGE, SLEEP_TIME);
void setup() { pinMode(IRQ, INPUT); attachInterrupt(digitalPinToInterrupt(IRQ), wakeup, RISING); .... } void loop { ... sleep(SLEEP_TIME); } void wakeup() { detected = true; }
-
@alexsh1 yes, you should preferably use the MySensors sleep() interface, unless you know exactly what you're doing.
Furthermore, using the MySensors API will assure compatibility with future versions of the library.
-
@yveaux Thank you|! But does attachInterrupt works with a simple sleep() from MySensors? I cannot call ISR from sleep(attachInterrupt()....)
-
@alexsh1 not sure what you are trying to achieve here... Is this the same question as in the initial post?
-
I am trying to understand if there a difference on how to put a node to sleep. Please see below and compare it to my initial post.
The node is working fine, but it seems that the sensor on IRQ is not working. This is why I am asking.It is sleep() + attachInterrupt vs sleep(attachInterrupt...) question
void setup() { pinMode(IRQ, INPUT); .... } void loop { ... if (detected) .... sleep(attachInterrupt(digitalPinToInterrupt(IRQ), CHANGE, SLEEP_TIME);
-
@Yveaux Let me tell you want I am doing and perhaps it would be easier for you to understand. I have a node with multiple sensors. Additionally, I have a i2c lightning sensor with IRQ connected to D2 of atmega328p. The node has to wake up every 10-15 mins for send regular information from sensors AND also has to wake up every time there is an interrupt on D2.
-
@alexsh1 said in attachInterrupt and sleep():
sleep(attachInterrupt(digitalPinToInterrupt(IRQ), CHANGE, SLEEP_TIME);
This is invalid, as attachInterrupt returns nothing; ref: https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
Just use
int8_t cause = sleep( digitalPinToInterrupt( IRQ ), CHANGE, SLEEP_TIME );
and have MySensors handle the wakeup by interrupt and sleep timeout for you.
The cause will tell you if it woke from interrupt or because of a timeout, as described by https://www.mysensors.org/apidocs-beta/group__MySensorsCoregrp.html#ga57a5217aff45276f40018165d2fb10a1
-
@yveaux said in attachInterrupt and sleep():
@alexsh1 said in attachInterrupt and sleep():
sleep(attachInterrupt(digitalPinToInterrupt(IRQ), CHANGE, SLEEP_TIME);
This is invalid, as attachInterrupt returns nothing; ref: https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
Thank you for spotting it - this was my mistake.
I meantsleep(digitalPinToInterrupt(IRQ), CHANGE, SLEEP_TIME);
-
@Yveaux this is what you mean, right?
I need to refactor my code a bit to try it.void loop() { int8_t cause = sleep( digitalPinToInterrupt( IRQ ), CHANGE, SLEEP_TIME ); switch (cause) { case MY_WAKE_UP_BY_TIMER: //Take measurements from sensor and msg to GW. break; case digitalPinToInterrupt(IRQ): // Lightning sensor interrupt break;
-
@alexsh1 yes, correct!