Hello everyone
I'm trying to write a code where the Arduino goes to sleep 3 times then wakes up however the slide potentiometers stop working (the serial monitor always prints 0V whatever the positioning of the slider) whenever I use the low power libraries. Any suggestions?
#include <avr/sleep.h>
#include <avr/wdt.h>
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
const int SLIDEPOT1 = 4; // the number of first slide pot pin
int Position = 1; // which position the slide pot is in
int SleepCount; //The number of times the Arduino has slept for
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// set the digital pin as output:
pinMode(SLIDEPOT1, OUTPUT);
digitalWrite (SLIDEPOT1, HIGH);//enable volatge for slide potentiometer
}
// the loop routine runs over and over again forever:
void loop() {
while (SleepCount < 3)
{
WakeUp();
LowPowerMode(); //Uses Idle Mode instead of Power Down Mode to keep the Timers running for PMW
}
// read the input on analog pin 1:
int SlidePotValue = analogRead(A1);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = SlidePotValue;
// print out the value you read:
Serial.println(voltage);
Serial.println(Position);
if ((SlidePotValue >= 0) && (SlidePotValue <= 255))
{
Position = 1;
}
if ((SlidePotValue >= 256) && (SlidePotValue <= 511))
{
Position = 2;
}
if ((SlidePotValue >= 512) && (SlidePotValue <= 767))
{
Position = 3;
}
if ((SlidePotValue >= 768) && (SlidePotValue <= 1023))
{
Position = 4;
}
}
//***************************************************************************************************************** Low Power Mode ****************************************************************************************************************************************************
void LowPowerMode() { //Sleep (MOST POWER SAVING)
// disable ADC
ADCSRA = 0;
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset
WDTCSR = bit (WDCE) | bit (WDE);
// set interrupt mode and an interval
WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 8 seconds delay
wdt_reset(); // pat the dog
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
noInterrupts (); // timed sequence follows
sleep_enable();
// turn off brown-out enable in software
MCUCR = bit (BODS) | bit (BODSE);
MCUCR = bit (BODS);
interrupts (); // guarantees next instruction executed
sleep_cpu ();
// cancel sleep as a precaution
sleep_disable();
}
//********************************************************************************************************** Wake Upp ****************************************************************************************************************************************************
void WakeUp() { //Wake up for 2s before going to sleep again
unsigned long startTime = millis();
unsigned long interval = 2000; // 2 seconds
SleepCount = SleepCount + 1;
while (millis() - startTime < interval)
{
Serial.println("Seep Count"); //Sleep Status Check
Serial.println (SleepCount);
}
}