hi guys:
I am trying to use interrupt and sleep mode to turn on/off Arduino. I use the example from the some web sites. When I start doing on my own way. It won't work. Is there anyone could possibly help me to deal with this?
When I start doing on my own way. It won't work. Is there anyone could possibly help me to deal with this?
Given the (pathetically little) information in your post, no.
No code. No description of the problem. No hope of help.
Show us what you have done so far.
Nick gammon has some great reviews here:
USVnut:
When I start doing on my own way. It won't work.
Your own way being what?
It won't work. Is there anyone could possibly help me to deal with this?
Is there any possibility you'll post your code?
This the programming section after all.
Yes!!
Sorry guys I was way to sleepy yesterday. I forgot to post my code. Here is my purpose for doing this project. I am suppose to let Arduino stay in PowerDown Sleep mode, When I plugging the battery. When I press the power button(pin2, RISING), it will bring Arduino to normal mode and start working. When I want to shutdown the system, I hold the button for couple seconds and it will go back to sleep in PowerDown Sleep mode. So, If I want to wake it up again, I will hit button(pin2) and it will wake up.
#include <avr/sleep.h>
#include <avr/power.h>
int pin2 = 2;
int led = 13;
int holdButtonCounter = 0;
void pin2Interrupt(void)
{
detachInterrupt(0);
}
void powerDown(void)
{
//digitalWrite(led,LOW);
attachInterrupt(0, pin2Interrupt, RISING);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
sleep_disable();
}
void setup()
{
pinMode(pin2, INPUT);
pinMode(led, OUTPUT);
attachInterrupt(0, pin2Interrupt, RISING);
powerDown();
}
void holdButton()
{
holdButtonCounter++;
if (holdButtonCounter > 100)
{
powerDown();
}
}
void loop()
{
digitalWrite(led,HIGH);
if (digitalRead(pin2) == LOW)
{
holdButton();
}
else
{
holdButtonCounter = 0;
}
}
And thank you guys for reminding me to post the code.
How is the switch wired?
You probably (almost certainly) need to allow for switch bounces.
The only external interrupt that can be detected from power-down sleep is LOW (not RISING).
More info on sleep modes:
here is the way how I wiring my interrupt button.
OK, but you haven't addressed my other comments.
I wire the button with the:
I also use hardware denounce on it by using parallel connection with with a 1uF capacitor
like this one:
I use the powerDown to keep the system in PowerDown mode when I plug in the battery(make sure it is not consume that much power).
When I press the button, it will wake up the Arduino and light up led on pin8. If I want to turn it off, I hold the button of keep it for couple second, it should be shutdown to PowerDown mode. However it doesn't. One thing I don't quiet understand is that I have PowerDown function in setup function to let the Arduino start with power_down mode. Wake it up with external interrupt, but program won't be able to shutdown the system when I hold the button to trigger the powerDown(). I am not sure where my blind spot is.
BTW, my board is Arduino MEGA 2560 R3.
#include <avr/sleep.h>
#include <avr/power.h>
int pin2 = 2;
int led = 8;
int holdButtonCounter = 0;
void wake(void)
{
detachInterrupt(0);
}
void powerDown(void)
{
attachInterrupt(0, wake, LOW);
set_sleep_mode(SLEEP_MODE_STANDBY);
sleep_enable();
sleep_mode();
sleep_disable();
}
void setup()
{
pinMode(pin2, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
powerDown();
}
void holdButton()
{
holdButtonCounter++;
if (holdButtonCounter > 100)
{
powerDown();
}
}
void loop()
{
digitalWrite(led,HIGH);
if (digitalRead(pin2) == LOW)
{
holdButton();
}
else
{
holdButtonCounter = 0;
}
}
Moderator edit: Inlined the images. (Nick Gammon)
I've modified your sketch a bit and this seems to work for me.
#include <avr/sleep.h>
#include <avr/power.h>
const unsigned long powerDownInterval = 2000; // milliseconds
const byte pin2 = 2;
const byte led = 8;
void wake (void)
{
detachInterrupt (0);
}
void powerDown(void)
{
noInterrupts ();
attachInterrupt (0, wake, LOW);
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable ();
interrupts ();
sleep_cpu ();
sleep_disable ();
}
void setup()
{
pinMode (pin2, INPUT_PULLUP);
pinMode (led, OUTPUT);
Serial.begin (115200);
powerDown ();
}
void holdButton()
{
delay (20); // debounce
unsigned long startTime = millis ();
// wait for switch to be released
while (digitalRead (pin2) == LOW)
{ }
delay (20); // debounce
// was it held down more than the required time?
if (millis () - startTime >= powerDownInterval)
powerDown ();
}
void loop()
{
digitalWrite (led, HIGH);
delay (200);
digitalWrite (led, LOW);
delay (200);
if (digitalRead (pin2) == LOW)
holdButton();
}
The main loop flashes the LED so you know it is awake and doing something. If you push the switch the LED stops flashing (as it waits for you to let go) however you have to hold it for two seconds or it keeps flashing. Otherwise it goes back to sleep.
I used INPUT_PULLUP to save having to use a resistor on the switch, and I used a software debounce to save having to wire up a capacitor.
Thank you so much Nick. I still have few questions. What is different between attachInterrupt() and Interrupt(), also what is the difference between noInterrupts() and detachInterrupt()?
interrupts() enables all interrupt handling.
noInterrupts() disables all interrupts.
attachInterrupt() attaches a particular interrupt (an external interrupt).
detachInterrupt() removes a particular interrupt.
Read the documentation for each of those.
hi Nick:
There is one more question about using sleep mode. I realized (may be is not correct) that if I don't set the LED to LOW, the LED won't go off when the Arduino get into sleep mode. I am correct with this?
Yes, I believe the output ports latch to whatever they were when it went to sleep.