Hi guys i have the following code running on a modified pro mini.
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <VirtualWire.h>
const byte LED = 13;
int ATimer;
typedef struct roverRemoteData
{
int TX_ID;
int Sensor1Data;// The variable were the data from each sensor
};
void AwakeTimer()
{
ATimer++;
}
//void DataTX()
//{
//
//}
void flash ()
{
pinMode (LED, OUTPUT);
digitalWrite (LED, HIGH);
delay (1000);
digitalWrite (LED, LOW);
delay (50);
ATimer = 0;
}
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
void setup ()
{
// VirtualWire setup
vw_setup(2000); // Bits per sec
vw_set_tx_pin(11);
pinMode(9,OUTPUT);
}
void loop ()
{
int BattValue = 0;
//delay(5000);
int BattCheckPin = A3;
pinMode(BattCheckPin,INPUT);
delay(100);
BattValue= analogRead(BattCheckPin);
delay(100);
AwakeTimer();
if ( ATimer == 2) // 45 = 6 min// 75 = 8 min // 450 = 1 Hour
{
flash ();
// DataTX();
digitalWrite(9,HIGH);
struct roverRemoteData payload;
payload.TX_ID = 3;
payload.Sensor1Data = BattValue;
vw_send((uint8_t *)&payload, sizeof(payload));
vw_wait_tx();
digitalWrite(9,LOW);
}
digitalWrite(9,LOW);
// 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);
sleep_enable();
// turn off brown-out enable in software
MCUCR = bit (BODS) | bit (BODSE);
MCUCR = bit (BODS);
sleep_cpu ();
// cancel sleep as a precaution
sleep_disable();
} // end of loop
it works great for power saving and to test it i have it wake up once a minute and read the voltage input on pin A3 . my problem is for some strange reason the pin reading does not update once a reading was taken.
Example
When starting the proram the analog read of pin A3 reads 1023 IE 3.3v as it is a 3.3v mini. but even if I remove the pin and apply a 1.5v voltage on the A3 pin it still reports 3.3v.
I have tried to place the line code
BattValue= 0;
after the transmission is complete and it still does not work.
does the sleep library have an impact ont eh analog read function or what is going on. the pin just not update its reading after the first bootup.
// disable ADC
ADCSRA = 0; [/code
I see you disable the ADC before sleeping, but you never reenable it.
In fact, I don't think it's necessary to disable the ADC before a power-down sleep, it should be disabled during sleep anyway.
okay I found the problem and fixed it, it turns out that you do infact need to disable the ADC before sleep otherwise it draws more current in sleep mode. My problem came in that once the unit wakes up the ADC has to be manually re enabled before and analog readings can be done.
here is my finished code below. ( Sorry about all the comments )
//Libraries//
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <VirtualWire.h>
//-----------------------------------End of Libraries---------------------------------------------//
// Global variables //
const byte LED = 13;
int ATimer;
int BattValue = 0;
int BattCheckPin = A3;
int DataRadioSwitch = 9;
int RadioTXPin = 11;
///////////////////////////////////// 8 = 1 min
///////////////////////////////////// 45 = 6 min
///////////////////////////////////// 75 = 8 min
int SleepTime = 450;///////////////// 450 = 1 Hour
///////////////////////////////////// 5400 = 12 Hours
///////////////////////////////////// 10800 = 24 Hours
//-----------------------------------End of Global Variables--------------------------------------//
// Data Structures//
typedef struct roverRemoteData
{
int TX_ID; // Just to make sure that the transmission is ID'd
int Sensor1Data;// The variable were the data from each sensor
};
//-----------------------------------End of Data Structures---------------------------------------//
// watchdog interrupt//
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog//
}
//------------------------------------End of Watchdog---------------------------------------------//
//Setup//
void setup ()
{
vw_setup(2000); // Radio Baud Rate//
vw_set_tx_pin(RadioTXPin ); // Set DATA radio TX Pin// NOTE: MUST BE PWM PIN
pinMode(DataRadioSwitch,OUTPUT);
}
//------------------------------------End of Setup-------------------------------------------------//
//Main Loop//
void loop ()
{
flash (); //HeartBeat Flash//
AwakeTimer(); //Awake Timer//
if ( ATimer == SleepTime)
{
BattRead();
DataTX();
} // End of Main loop if timer was reached
//----------------------------------- End of Actual executing code---------------------------//
//Preparing to go to sleep//
digitalWrite(DataRadioSwitch,LOW); //Switch Data Radio off
byte old_ADCSRA = ADCSRA; // disable ADC //
ADCSRA = 0; // disable ADC //
byte old_PRR = PRR; // disable Internal modules//
PRR = 0xFF; // disable Internal modules//
MCUSR = 0; // clear various "reset" flags//
// Watchdog Timer Parameters//
WDTCSR = bit (WDCE) | bit (WDE); // allow changes, disable reset
WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 8 seconds delay
wdt_reset(); // pat the dog once program has executed.
// Sleep Activation //
set_sleep_mode (SLEEP_MODE_PWR_DOWN); //Sleep mode Selection//
sleep_enable(); //Sleep Now//
// turn off brown-out enable in software//
MCUCR = bit (BODS) | bit (BODSE); //Brown out settings
MCUCR = bit (BODS); //Brown out set.
sleep_cpu (); //CPU is now sleeping
//--------------------------------------End of sleep Preperation-------------------------------//
// Once awake code executes from this point//
// Once CPU wakes up do the follwoing to restore full operations//
sleep_disable();
PRR = old_PRR;
ADCSRA = old_ADCSRA;
}
//----------------------------------------END OF MAIN PROGRAM---------------------------------------//
// VOIDS FUNCTIONS//
//Wake up timer//
void AwakeTimer()
{
ATimer++;
}
// Heartbeat flash//
void flash ()
{
pinMode (LED, OUTPUT);
digitalWrite (LED, HIGH);
delay (16);
digitalWrite (LED, LOW);
delay (16);
ATimer = 0; // Reset Wake up timer//
}
// Read Own battery State//
void BattRead()
{
BattValue = 0;
BattValue= analogRead(BattCheckPin);
}
// Transmit data//
void DataTX()
{
digitalWrite(DataRadioSwitch,HIGH);
struct roverRemoteData payload;
payload.TX_ID = 3;
payload.Sensor1Data = BattValue;
vw_send((uint8_t *)&payload, sizeof(payload));
vw_wait_tx();
digitalWrite(DataRadioSwitch,LOW);
}