Go Down

Topic: saving energy (Read 931 times) previous topic - next topic


i try to make a code for saving energy in arduino by using IDLE MODE...
i used timer 2 for give an intteruption to my processeur every 6s...
but my problem is,how i can make a mesure to verify that my code is correct???
can someone help me please....


#include <Timer.h>
#include <avr/sleep.h>

const int ledPin=13 ;    //declarer le port 13 comme output
int iEtat ;

TIMER TIM = TIMER();  //instancier la class de la TEMPS

void setup() {
  pinMode (ledPin,OUTPUT);
  iEtat = 0 ;    //initialise à 0
  digitalWrite (ledPin,iEtat);     //ecrire la valeur iEtat à broche 13
  TIM.Init() ;            //appelle la fonction donc la biblio
  TIM.Stop() ;            //appelle la fonction donc la biblio
  TIM.Set(6000) ;        //appelle la fonction donc la biblio
  TIM.Start() ; 
  set_sleep_mode(SLEEP_MODE_IDLE);   // sleep mode is set here
  sleep_enable();  //appelle la fonction donc la biblio

void loop(){
  enum TIMERSTATE EtatDuTimer ;    
    while(1) {   
     digitalWrite (ledPin,iEtat);
      EtatDuTimer = TIM.GetState() ;
      if(EtatDuTimer == FINI) {
        iEtat = ! iEtat ;
        digitalWrite (ledPin,iEtat);
        TIM.Start() ;


You could measure the current draw with and without your sleep code.  If the current draw is lower during sleep then you have done it correctly.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


placing an ampmeter reading the current taken by the arduino will let you know if your code is doing something.

Regarding the code, I have no idea if it'll work since I haven't seen how the TIMER class is built. But I suspect that it won't work since it should need an interrupt function to be called every 6s (or when the timer expires). But again, I have no idea if this is the case.
This... is a hobby.


which pin must i make a mesure to read a current taken by the arduino???


Usually you would be using energy saving features when your project has to run off batteries.  Put the current meter between the + side of the battery and here the battery connects to the Arduino, typically the Vin pin, +5 pin, or the power jack.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


There have been other recent threads regarding this topic, eg:

Search for "sleep_cpu" and you will find a good deal of material.

You will find that SLEEP_MODE_IDLE will be woken up by any interrupt, including the timer0 one, which will fire every 1,000 microseconds.  Even so, sleeping the CPU will make your main loop run at a thousand loops/sec rather than a million plus.  The energy savings might well be worth it.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131