How to turn off Arduino to save battery?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How to turn off Arduino to save battery?

Post by adafruit_support_rick »

Is the code working for you, or is there a problem with it?

zajebiscie
 
Posts: 13
Joined: Thu Nov 15, 2012 8:04 pm

Re: How to turn off Arduino to save battery?

Post by zajebiscie »

driverblock wrote:Is the code working for you, or is there a problem with it?
No, is not working for me. After pressing FSR sensor, it will wakeup arduino but after 1or2 seconds while continue holding it, it will go back to sleep. So, i was wondering if it has something to do with the last code section that forces it to go back to sleep while still holding the sensor which suppose to opposite to that.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How to turn off Arduino to save battery?

Post by adafruit_support_rick »

Your loop function is doing that.

Code: Select all

void loop(void)
{
// Stay awake for 0.1 second, then sleep.
// LED turns off when sleeping, then back on upon wake.
delay(100);
sleepNow();
}
When you call sleepNow, it will sleep until you touch the FSR. After you touch the FSR, the sleepNow function will return. The loop function will wait 100 milliseconds, and call sleepNow() again. And then the arduino will go back to sleep whether you're holding the FSR or not.

zajebiscie
 
Posts: 13
Joined: Thu Nov 15, 2012 8:04 pm

Re: How to turn off Arduino to save battery?

Post by zajebiscie »

driverblock wrote:Your loop function is doing that.

Code: Select all

void loop(void)
{
// Stay awake for 0.1 second, then sleep.
// LED turns off when sleeping, then back on upon wake.
delay(100);
sleepNow();
}
When you call sleepNow, it will sleep until you touch the FSR. After you touch the FSR, the sleepNow function will return. The loop function will wait 100 milliseconds, and call sleepNow() again. And then the arduino will go back to sleep whether you're holding the FSR or not.
Thanks very much. I m not really good in programming. Do i have to remove both delay and sleepNow, for arduino to stay awake when holding FSR, and then go back to sleep when i am not holding it.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How to turn off Arduino to save battery?

Post by adafruit_support_rick »

What you want to do is put a test into sleepNow. Check the state of the FSR. If it is pressed, then don't go to sleep and simply return.

You can leave the delay in, I suppose. That way you'll be checking your FSR 10 times per second.

zajebiscie
 
Posts: 13
Joined: Thu Nov 15, 2012 8:04 pm

Re: How to turn off Arduino to save battery?

Post by zajebiscie »

driverblock wrote:Your loop function is doing that.

Code: Select all

void loop(void)
{
// Stay awake for 0.1 second, then sleep.
// LED turns off when sleeping, then back on upon wake.
delay(100);
sleepNow();
}
When you call sleepNow, it will sleep until you touch the FSR. After you touch the FSR, the sleepNow function will return. The loop function will wait 100 milliseconds, and call sleepNow() again. And then the arduino will go back to sleep whether you're holding the FSR or not.
Thanks very much. I m not really good in programming. Do i have to remove both delay and sleepNow, for arduino to stay awake when holding FSR, and then go back to sleep when i am not holding it.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How to turn off Arduino to save battery?

Post by adafruit_support_rick »

OK. Try this:

Code: Select all

void loop(void)
{
  delay(100);					//execute loop 10 times/second
  if (HIGH == digitalRead(2))		// if the FSR is not pressed
  {
    sleepNow();					// then go to sleep.  Stays asleep until FSR is pressed.
  }
}

zajebiscie
 
Posts: 13
Joined: Thu Nov 15, 2012 8:04 pm

Re: How to turn off Arduino to save battery?

Post by zajebiscie »

driverblock wrote:OK. Try this:

Code: Select all

void loop(void)
{
  delay(100);					//execute loop 10 times/second
  if (HIGH == digitalRead(2))		// if the FSR is not pressed
  {
    sleepNow();					// then go to sleep.  Stays asleep until FSR is pressed.
  }
}

Thanks for your reply. I tried it but it did not work . Any other suggestions?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How to turn off Arduino to save battery?

Post by adafruit_support_rick »

I don't understand how it could not work. Your FSR is connected to digital 2, correct? It reads low when you press it, and high when you release it?

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: How to turn off Arduino to save battery?

Post by tldr »

try this. note that sleep is enabled before the interrupt is enabled, and is disabled within the interrupt routine.

Code: Select all

void sleepNow(void) {
  sleep_enable();
  attachInterrupt(0, pinInterrupt, LOW);
  delay(100);
  set_sleep_mode(SLEEP_MODE_IDLE);
  power_adc_disable();
  power_spi_disable();
  power_timer0_disable();
  power_timer1_disable();
  power_timer2_disable();
  power_twi_disable();
  digitalWrite(13,LOW); // turn LED off to indicate sleep
  sleep_mode();
  digitalWrite(13,HIGH); // turn LED on to indicate awake
}

void pinInterrupt(void) {
  sleep_disable();
  detachInterrupt(0);
}
the reasons for doing it this way are detailed here, but essentially we are trying to avoid a situation where the interrupt is disabled before the device is put to sleep.

sarahline
 
Posts: 3
Joined: Thu Dec 13, 2012 2:15 am

Re: How to turn off Arduino to save battery?

Post by sarahline »

How would I put the Arduino to sleep if I'm using the adafruit Wave shield and can't use digital pin 2?

Thanks for your help.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How to turn off Arduino to save battery?

Post by adafruit_support_rick »

There are a few different ways, but the easiest is to use either digital 2 or digital 3.

The Wave Shield doesn't make you use digital 2 for the LCS signal - that's just the way the tutorials are written. You can remove the jumper wire you soldered from LCS to digital 2, and replace it with a jumper from LCS to some other digital pin - digital 6, for example.

After that, all you need to do is modify the wave shield library to specify digital 6 for LCS. If you're usingWaveHC, there is a smallfile in the library called WavePinDefs.h. Open that file with any text editor program, and look for this section:

Code: Select all

// use arduino pins 2, 3, 4, 5 for DAC

// pin 2 is DAC chip select

/** Data direction register for DAC chip select. */
#define MCP_DAC_CS_DDR  PIN2_DDRREG
/** Port register for DAC chip select. */
#define MCP_DAC_CS_PORT PIN2_PORTREG
/** Port bit number for DAC chip select. */
#define MCP_DAC_CS_BIT  PIN2_BITNUM
Change five lines to reference pin 6 instead of pin 2, and save the file.

Code: Select all

// use arduino pins 3, 4, 5, 6 for DAC

// pin 6 is DAC chip select

/** Data direction register for DAC chip select. */
#define MCP_DAC_CS_DDR  PIN6_DDRREG
/** Port register for DAC chip select. */
#define MCP_DAC_CS_PORT PIN6_PORTREG
/** Port bit number for DAC chip select. */
#define MCP_DAC_CS_BIT  PIN6_BITNUM
Also, make sure you check your sketch for references to digital 2, and change them to 6 as well. Some of the older example sketches have setup code like this:

Code: Select all

  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
You would change that to:

Code: Select all

  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(6, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”