World of Electronics

Minuterie – Full Tutorial

For this tutorial, you’ll need:

  1. Arduino UNO
  2. 1 x Resistor 330E
  3. BreadBoard
  4. Jumper Wires
  5. 1 x Led 5MM
  6. 1x TCRT500 Module

Watch this full video:

This video is sponsored by C.B.Electronics & Produced by Lebanese Park

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;  
}