Read arduino supply voltage, giving garbage on sleep?


hi,

have tried afternoon cannot find out why, if add sleep function, supply voltage reads garbage, ok if don't use sleep function ?  maybe adc being turned off ?

any appreciated.  :)

regards

gary

code: [select]


/*
  blink
  read arduino battery
  print serial port voltage
  sleep 4 seconds
*/


  #include <avr/sleep.h>
  #include <avr/wdt.h>
  //#include <avr/power.h>
  int led = 13;

 
  isr(wdt_vect) // watchdog interrupt
  {
  wdt_disable();  // disable watchdog
  }
 
void setup()
  {               
  pinmode(led, output);     
  serial.begin(9600);
  }


void loop()
  {
  serial.println(" led on");
  digitalwrite(led, high);   
  delay(200);             
  digitalwrite(led, low);   
  serial.println(" led off");
  delay(1000);             
  readvcc() ; // read arduino  battery voltage
  serial.print(" ");
  serial.println( readvcc(), dec );
  mywatchdogenable (b0100000);  // sleep 4 seconds //   *** goes wrong when add line  ***
  }


long readvcc()
  {
  long result;
  // read 1.1v reference against avcc
  admux = _bv(refs0) | _bv(mux3) | _bv(mux2) | _bv(mux1);
  delay(2); // wait vref settle
  adcsra |= _bv(adsc); // convert
  while (bit_is_set(adcsra,adsc));
  result = adcl;
  result |= adch<<8;
  result = 1126400l / result; // back-calculate avcc in mv
  return result;
  }


void mywatchdogenable(const byte interval)
  {
  mcusr = 0;                          // reset various flags
  wdtcsr |= 0b00011000;               // see docs, set wdce, wde
  wdtcsr =  0b01000000 | interval;    // set wdie, , appropriate delay

  wdt_reset();
  set_sleep_mode (sleep_mode_pwr_down);
  sleep_mode();            // goes sleep , waits interrupt
  }


have tried doing few analogread()s in succession , discarding last one?

it may worth trying short wait between reads.

...r


Arduino Forum > Using Arduino > Programming Questions > Read arduino supply voltage, giving garbage on sleep?


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support