Hi guys,
I was wondering how to put the Arduino to sleep and what happens in the sleep mode. I'm using the SLEEP_MODE_PWR_DOWN, I believe this is the complete power down? If I'm wrong please correct me.
So I was making a project in which I want to put the Arduino to complete power down mode, where I don't want it to do anything. So to experiment how this sleep mode works with the Arduino I tried to make a sketch in which I trigger the sleep mode 5 seconds after getting into the loop. Also I have the built-in led on pin 13 turned on. I wanted to see if the led would turn off automatically as the Arduino jumps into the sleep mode. Unfortunately, the led doesn't turn off by itself. It seemed as if I had to write the code to turn off the led before going into the sleep mode and turn it back on after waking up. I forgot to mention that I'm using Interrupt on pin 2 to wake up the Arduino from sleep.
So does that mean I'll have to turn off all devices I wanted to stop working during the sleep mode manually (I mean with extra lines of code obviously, like I did with the led in this sketch below)? Is there no way for the Arduino to automatically turn off everything in power down mode?
This is my experiment sketch.
#include <avr/sleep.h>
#define interruptPin 2
unsigned long curtime1 = 0;
unsigned long prevtime1 = 0;
unsigned long curtime2 = 0;
unsigned long prevtime2 = 0;
int interval1 = 5000;
int interval2 = 500;
void setup()
{
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop()
{
curtime1 = millis();
curtime2 = millis();
if ((curtime1 - prevtime1) >= interval1)
{
Serial.println("Going to sleep now!!");
prevtime1 = curtime1;
going_to_sleep();
}
if ((curtime2 - prevtime2) >= interval2)
{
Serial.println("im still running"); //To check via serial monitor if the loop is still running while on sleep.
prevtime2 = curtime2;
}
}
void going_to_sleep()
{
sleep_enable();
attachInterrupt(0, wakeUp, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
//digitalWrite(LED_BUILTIN, LOW); //To turn off led before going to sleep
delay(1000);
sleep_cpu();
Serial.println("just woke up!!");
//digitalWrite(LED_BUILTIN, HIGH); //To turn on led after waking up
}
void wakeUp()
{
Serial.println("Interrupt Fired");
sleep_disable();
detachInterrupt(0);
}