Re: How to turn off Arduino to save battery?

by adafruit_support_rick on Fri Dec 14, 2012 10:33 am

Is the code working for you, or is there a problem with it?
User avatar
adafruit_support_rick
 
Posts: 10937
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: How to turn off Arduino to save battery?

by zajebiscie on Thu Dec 20, 2012 11:47 am

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.
zajebiscie
 
Posts: 13
Joined: Thu Nov 15, 2012 7:04 pm

Re: How to turn off Arduino to save battery?

by adafruit_support_rick on Thu Dec 20, 2012 11:54 am

Your loop function is doing that.
Code: Select all | TOGGLE FULL SIZE
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.
User avatar
adafruit_support_rick
 
Posts: 10937
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: How to turn off Arduino to save battery?

by zajebiscie on Thu Dec 20, 2012 12:50 pm

driverblock wrote:Your loop function is doing that.
Code: Select all | TOGGLE FULL SIZE
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.
zajebiscie
 
Posts: 13
Joined: Thu Nov 15, 2012 7:04 pm

Re: How to turn off Arduino to save battery?

by adafruit_support_rick on Thu Dec 20, 2012 12:54 pm

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.
User avatar
adafruit_support_rick
 
Posts: 10937
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: How to turn off Arduino to save battery?

by zajebiscie on Thu Dec 20, 2012 1:42 pm

driverblock wrote:Your loop function is doing that.
Code: Select all | TOGGLE FULL SIZE
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.
zajebiscie
 
Posts: 13
Joined: Thu Nov 15, 2012 7:04 pm

Re: How to turn off Arduino to save battery?

by adafruit_support_rick on Thu Dec 20, 2012 1:57 pm

OK. Try this:
Code: Select all | TOGGLE FULL SIZE
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.
  }
}
User avatar
adafruit_support_rick
 
Posts: 10937
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: How to turn off Arduino to save battery?

by zajebiscie on Thu Dec 20, 2012 6:42 pm

driverblock wrote:OK. Try this:
Code: Select all | TOGGLE FULL SIZE
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?
zajebiscie
 
Posts: 13
Joined: Thu Nov 15, 2012 7:04 pm

Re: How to turn off Arduino to save battery?

by adafruit_support_rick on Fri Dec 21, 2012 9:44 am

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?
User avatar
adafruit_support_rick
 
Posts: 10937
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: How to turn off Arduino to save battery?

by tldr on Sat Dec 22, 2012 8:35 am

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

Code: Select all | TOGGLE FULL SIZE
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.
"If I had known it was harmless, I would have killed it myself." - Phillip K. Dick, A Scanner Darkly
User avatar
tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 12:34 am

Re: How to turn off Arduino to save battery?

by sarahline on Sun Mar 03, 2013 1:15 pm

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.
sarahline
 
Posts: 3
Joined: Thu Dec 13, 2012 1:15 am

Re: How to turn off Arduino to save battery?

by adafruit_support_rick on Sun Mar 03, 2013 3:05 pm

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 | TOGGLE FULL SIZE
// 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 | TOGGLE FULL SIZE
// 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 | TOGGLE FULL SIZE
  // 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 | TOGGLE FULL SIZE
  // 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);
User avatar
adafruit_support_rick
 
Posts: 10937
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY