Notifications
Clear all

Sleep and push button interrupt wake up question

5 Posts
3 Users
1 Likes
1,980 Views
(@garnold)
Member
Joined: 5 years ago
Posts: 81
Topic starter  

I'd like to be able to put my Arduino into deep sleep only to wake up when a button is pressed. Once the button is pressed I need it to do something for 1 sec (honk a horn) and then go back to sleep. My question is about the task of making something happen for a specific amount of time. From my reading I've learned that delays should not be used with ISR. Can I get some advice on how I would go about coding something like this? Again, work flow is this....

  • Power Arduino on
  • Some settings get set
  • Arduino goes to deep sleep
  • Button is pressed
  • Arduino wakes up
  • ISR is fired which should trigger horn for 1 sec
  • Arduino goes back to sleep waiting for next trigger

Much thanks!

Gene


   
Quote
(@jbeazy)
Member
Joined: 5 years ago
Posts: 18
 

You could use a global volatile variable initially set to zero or LOW.

When ISR is triggered set variable to 1 or HIGH in ISR. In main loop if variable=HIGH, disable interrupts, set horn high, delay x msecs, enable interrupts, set variable=LOW, put back to sleep.

Since the interrupts were disabled you can use delay() functions.  See attachInterrupts() ,detachInterrupts(). Note that if you have a long delay interrupts won't work during that time which may or may not be a problem depending on the application.


   
garnold reacted
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1121
 
Posted by: @garnold

Can I get some advice on how I would go about coding something like this? Again, work flow is this....

  • Power Arduino on
  • Some settings get set
  • Arduino goes to deep sleep
  • Button is pressed
  • Arduino wakes up
  • ISR is fired which should trigger horn for 1 sec
  • Arduino goes back to sleep waiting for next trigger

Power Ardunio on
- plug it in
Some settings are set
- either your program code sets up settings like the frequency of the output to your horn upon start up, or your program awaits some communication via serial or I2C etc that triggers the program to set something, and you communicate to the arduino from another computer.

Arduino goes into deep sleep
Button is pressed
Arduino wakes up

- The web is awash with tutorials on doing just this so I wont repeat, but here is a link to a typical tutorial.

https://thekurks.net/blog/2018/1/24/guide-to-arduino-sleep-mode

ISR is fired which should trigger horn for 1 sec
I'm not sure why you would use an ISR for this, just program it in the Arduino Loop - edit - were you thinking of triggering the horn because of a separate button push, or sounding the horn just because the computer is awakened by the aforementioned arduino awakening button press?

Arduino goes back to sleep waiting for next trigger
- refer back to the tutorial for putting the Arduino into a deep sleep. Call the 'deep sleep' code again.

Have fun


   
ReplyQuote
(@garnold)
Member
Joined: 5 years ago
Posts: 81
Topic starter  

@byron

Yes I want the horn to fire for 1 sec once the Arduino comes out of sleep. Never really thought about just putting that in the loop. Pretty silly on my part. So the settings will put it to sleep from the start. The button wakes it up. The loop fires and blows the horn like a normal loop code and after it just puts the Arduino back to sleep. Right? Duh! On my part. 


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1121
 

@garnold

You could try the following logic which nicely follows the tutorial I gave a link to.

main loop calls the 'put to sleep' function.

The 'put to sleep function'  1) enables the sleep mode and attaches an interrupt that will call an awake function, 2) put the computer to sleep, and 3) Sounds the horn.

So the arduino takes 40 winks in step 2 of the 'put to sleep' function.  When the interrupt is triggered the awake function cancels the sleep and you should then be right back into the 'put to sleep' function' at step 3 which then sounds the horn.  The function is then finished so it returns to the main loop which then calls the 'put to sleep function' again.

Of course you could also think of a logic sequence that calls a horn sound function from the main loop.  Lots of ways to achieve the same goal, so don't think I was suggesting that sounding the horn from the main loop was necessarily the best way to go but I was just wondering why you were thinking of calling an IRS to sound the horn.   Putting the computer to sleep immediately in the Settings section is probably not the most elegant way to program this, but why not try several different ways to loop your code as this will help you in your learning and often there is no absolute best way, just your preferred way. 😀 

 


   
ReplyQuote