GreyGnome / EnableInterrupt

New Arduino interrupt library, designed for Arduino Uno/Mega 2560/Leonardo/Due
332 stars 73 forks source link

Sleep mode issues #32

Closed damonvp closed 8 years ago

damonvp commented 8 years ago

I realize this has been reported prior, however Im not getting how to resolve it. I have a Rotary Encoder/Switch I want the switch to enter and exit sleep mode. I have the switch on pin4 of my uno.

void setup() { ... enableInterrupt(2, intrEncChange1, CHANGE); //encoder interrupts enableInterrupt(3, intrEncChange2, CHANGE); enableInterrupt(4, intrEncChangeS, CHANGE); ... }

void intrEncChangeS() { if (sw) delay(1); if (digitalRead(SencPin) == encSW) return; encSW = !encSW; if (encSW) sleepNow(); sw = false; } void intrEncChange1() //Read on interrupt Right turn - Fast +5 { if (moving) delay(1); if (digitalRead(encPin1) == enc1) return; enc1 = !enc1; if (enc1 && !enc2) encVal += 5; moving = false; update(); } void intrEncChange2() //Read on interrupt Left turn - Slow -1 { if (moving) delay(1); if (digitalRead(encPin2) == enc2) return; enc2 = !enc2; if (enc2 && !enc1) encVal -= 1; moving = false; update(); } void sleepNow() { disableInterrupt(4); enableInterrupt(4, wakeUpNow, LOW); set_sleep_mode(SLEEP_MODE_STANDBY);// Choose our preferred sleep mode sleep_enable();// Set sleep enable (SE) bit sleep_mode();// Put the device to sleep sleep_disable();// Upon waking up, sketch continues from this point. disableInterrupt(4); enableInterrupt(4, intrEncChangeS, CHANGE); }

How do I code this so that the switch works the way I need it to in order to resume from sleep mode?

markappa commented 8 years ago

You should not put the device to sleep from an interrupt routine. Try just setting some boolean flag in the ISR and then managing the sleep mode in the main loop.

GreyGnome commented 8 years ago

Thanks, Mark. Also note damonvp that you cannot use delay() in an interrupt because it depends on a timer which raises interrupts, and inside the interrupt, interrupts are off.

damonvp commented 8 years ago

Thanks!

On Fri, May 6, 2016 at 11:03 PM, Mike Schwager notifications@github.com wrote:

Thanks, Mark. Also note damonvp that you cannot use delay() in an interrupt because it depends on a timer which raises interrupts, and inside the interrupt, interrupts are off.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/GreyGnome/EnableInterrupt/issues/32#issuecomment-217601793

GreyGnome commented 8 years ago

Closing due to it looks closeable. :-)