For this tutorial, you’ll need:
Watch this full video:
First Version of Code:
#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
#define interruptPin 2 //Pin we are going to use to wake up the Arduino
#define led 7
void setup() {
Serial.begin(9600); //Start Serial Comunication
pinMode(led,OUTPUT);
digitalWrite(led,LOW);//turning LED on
pinMode(interruptPin,INPUT_PULLUP);//Set pin d2 to input using the buildin pullup resistor
}
void loop() {
Going_To_Sleep();
//------
//sleeping
//----- byerja3 hone
Serial.println("just woke up!");//next line of code executed after the interrupt
digitalWrite(led, HIGH);
delay(10000); // light for 10 seconds
}
void Going_To_Sleep(){
Serial.println("Bonne Nuit");
sleep_enable();//Enabling sleep mode
attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2 digitalPinToInterrupt(2)
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
digitalWrite(led,LOW);//turning LED off
delay(1000); //wait a second to allow the led to be turned off before going to sleep
sleep_cpu();//activating sleep mode
}
void wakeUp(){
Serial.println("Interrrupt Fired");//Print message to serial monitor "Bonjour"
sleep_disable();//Disable sleep mode
detachInterrupt(0); //Removes the interrupt from pin 2;
}
Second Version of Code:
#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
#define interruptPin 2 //Pin we are going to use to wake up the Arduino
unsigned long timeNow;
unsigned long timeAfterTenSeconds;
int start = 0;
int count = 0;
void setup() {
Serial.begin(9600);//Start Serial Comunication
pinMode(7,OUTPUT);
pinMode(interruptPin,INPUT_PULLUP);//Set pin d2 to input using the buildin pullup resistor
digitalWrite(7,LOW);//turning LED on
Going_To_Sleep();
}
void loop() {
if(start==1)
{
digitalWrite(7, HIGH);
timeNow = millis();
count=1;
start=2;
}
if(count == 1)
{
timeAfterTenSeconds = millis();
if(timeAfterTenSeconds - 10000 == timeNow)
{
count = 0;
start = 0;
}
}
if(start ==0)
{
Going_To_Sleep();
}
}
void Going_To_Sleep(){
Serial.println("Bonne Nuit");
sleep_enable();//Enabling sleep mode
attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
digitalWrite(7,LOW);//turning LED off
delay(1000); //wait a second to allow the led to be turned off before going to sleep
sleep_cpu();//activating sleep mode
}
void wakeUp(){
start =1;
Serial.println("Sabaho");//next line of code executed after the interrupt
Serial.println("Interrrupt Fired");//Print message to serial monitor
sleep_disable();//Disable sleep mode
//detachInterrupt(0); //Removes the interrupt from pin 2;
}