Waking up from power down - which pin?

In my sketch I have set 3 interrupts on 3 different pins in order to wake up my arduino.

One pin is attached to a push button, one to a 555 5 minute timer and one to the the Serial1 Rx pin.

These interrupts are waking up the arduino as expected however I can’t seem to find a way to determine which pin woke it up.

Serial.println statements don’t seem to work - probably not surprising in an ISR routine.

I can’t save a specific value to my SD card because SD library is not initialised.

And presumably I will have the same problem with saving a value to EEPROM…apart from the issue of limited writes.

And any global variables are apparently reset when the sketch restarts.

So is there any way I can store a value some where to tell me, in setup() and loop(), which pin/interrupt was responsible for waking up the arduino?

My issue is that I want to turn an LCD screen off immediately unless the arduino was woken manually via the push button.

You're missing a lot of information.

How are you powering down? By physically disconnecting power with a MOSFET? That's the worst way to do it. Your Arduino should NEVER have to restart during normal operation.

The devil is in the details when it comes to programming, so post them all.

Jiggy-Ninja: You're missing a lot of information.

How are you powering down? By physically disconnecting power with a MOSFET? That's the worst way to do it. Your Arduino should NEVER have to restart during normal operation.

The devil is in the details when it comes to programming, so post them all.

No I am not, i am powering down via Nick Gamon's method. The only device I am turning off with a relay is a TFTLCD screen.

There is no way I can tell if the code in my 3 ISRs is even being executed, when it comes out of power down, because the following does not even work inside any of them (with an LED connected)

 pinMode(A15, OUTPUT);
  digitalWrite(A15, HIGH);

I am starting to come to the conclusion that puting an arduino to sleep is more trouble than it is worth other than for trivial applications where the arduino only does one single function.

Just stick a bloody cheap solar panel on it to power it and trickle charge a battery and just forget trying to power it down!

When the Arduino comes out of power down are the ISR routines you attach, when you call attachInterrupt(…) prior to entering power down, irrelevant???

I.E. The interrupt simply wakes up the arduino and it jumps straight to setup()?

If that is the case then power down is useless for this particular application.

And since the other sleep modes don’t make all that significant reductions in power consumption then I needn’t waste my time with them given i can just attach a solar panel.

I said to post ALL of the details, not some of them. Waking up from sleep should not reset your sketch. If it is resetting you're doing something wrong.

When the Arduino comes out of power down are the ISR routines you attach, when you call attachInterrupt(...) prior to entering power down, irrelevant??????

I.E. The interrupt simply wakes up the arduino and it jumps straight to setup()?

There are mistakes that can cause that to happen.

If that is the case then power down is useless for this particular application.

The problem is most likely that you don't know how to use it. Post a complete sketch, don't waste my time and good will with a useless two line snippet.

And since the other sleep modes don't make all that significant reductions in power consumption then I needn't waste my time with them given i can just attach a solar panel.

If you are using a full Mega dev board, then power saving is pretty much useless since the regulator will be crap and you can't turn off the USB-Serial chip. That alone counts for about half of the normal power usage.

And what do you mean they don't save much? IDLE alone cuts the current draw by about 75%, and all the others drop the current far down into the microamp range. If you aren't seeing benefits from sleeping, you're doing something wrong. (there's a pattern here)

I don't know why you would think a solar panel+battery is easier than sleeping. Do you know how large a solar panel and battery you would need to sustain the average operating current of your project? Do you know how to connect the two so that they can hand off between each other seamlessly?

Jiggy-Ninja: I said to post ALL of the details, not some of them. Waking up from sleep should not reset your sketch. If it is resetting you're doing something wrong.There are mistakes that can cause that to happen.The problem is most likely that you don't know how to use it. Post a complete sketch, don't waste my time and good will with a useless two line snippet.If you are using a full Mega dev board, then power saving is pretty much useless since the regulator will be crap and you can't turn off the USB-Serial chip. That alone counts for about half of the normal power usage.

And what do you mean they don't save much? IDLE alone cuts the current draw by about 75%, and all the others drop the current far down into the microamp range. If you aren't seeing benefits from sleeping, you're doing something wrong. (there's a pattern here)

I don't know why you would think a solar panel+battery is easier than sleeping. Do you know how large a solar panel and battery you would need to sustain the average operating current of your project? Do you know how to connect the two so that they can hand off between each other seamlessly?

An incorrect pin assignment was resetting my arduino - I thought I was resetting my ESP8266 shield but I was in fact resetting the arduino. Was calling this from my ISR.

So I now know how it is supposed to behave - loop() stops and restarts.

It would be nice to have had some complete examples of sleep modes among the built in Arduino IDE examples.

I would have then seen immediately what is supposed to happen and realised that something was wrong in my code and causing it to reset.

Why do you have to re-initialise Serial? As in Serial.begin(115200)?

So I now know how it is supposed to behave - loop() stops and restarts.

Close enough. SLEEP actually halts the processor where it currently is, then it continues from that same spot after it wakes up. It doesn't start from the top of loop().

It would be nice to have had some complete examples of sleep modes among the built in Arduino IDE examples.

The creators have neglected to make sleeping part of the Arduino core. There's quite a few other features of the AVR chips that they didn't bother supporting in the Arduino core such as:

  • Analog comparator
  • Watchdog timer
  • Power reduction register
  • Free-running ADC
  • The 3 internal ADC inputs (GND, bandgap reference, and temperature sensor)
  • Changing PWM frequency
  • Input capture
  • Pin change interrupts
  • Timer2 Asynchronous operation

Many of these are supported by user-created libraries, but because they are not part of the official core or library set, examples are not included with the IDE.

There is a LowPower library. Search for it if you think it will help.

Why do you have to re-initialise Serial? As in Serial.begin(115200)?

You don't. The problem is that on the Arduino boards, there is a USB->Serial converter chip that draws its own power and you can't make it stop. Same with the voltage regulator. Even if you get your main processor down to sub-microamp current consumption, those two will still be merrily chewing up almost 40 mA of current.

That's why you need to get off an Arduino Board. One of the users here (CrossRoads) makes and sells minimalist breakout boards for the Mega's processor that have only the bare essentials needed for operation. PM him if you want to know more about that.

40mA would be perfectly good enough with a solar panel I would have though! 1W to 5W solar panels don't seem to cost all that much on ebay these days.

I have little doubt that these sleep modes are an integral part of a great many projects in here. So I see no good reason why they shouldn't provide some examples.

As it happens I have had to piece together how to do it from bits of code snippets from many sites.

But I have not found a site with a complete example showing you all the things you need to do and what is supposed to happen.

Buried in the Useful Links sticky here there is a Power Saving tutorial.

http://www.gammon.com.au/power

I was following one of Nick's tutorials, among others - it was probably that page.

But it still doesn't really entirely explain what is supposed to happen, i.e. at what point in the code does the sketch stop and at what point in the code does it start up again.

Another individual had comments in his code snippets stating this but I could not seem to get his bits of code snippets into a sketch that worked.

Then I found one of Nick's pages and his seemed to work differently from the previous one I was reading.

By that time I was rather confused as to what precisely sleep is supposed to do.

What ended up clinching it was your mentioning of the fact that the arduino should not be re-entering setup() and puting a Serial.println statement in loop. Then I could immediately see what was supposed to happen.

So an official example with a println statement in loop() would be really nice for new comers to sleep. Once you mentioned that

It stops at the sleep_cpu(); statement, and continues from that same spot when it wakes up. All information in the registers and SRAM will be retained.

Basically, it just pauses everything.