Please forgive the messyness I've disabled lots just so I could isolate if it was adding the counter each wdt 8 second sleep. Thanks for taking a look.
//AceTK - Deepsleep powersaving single cell lion charger
#include <avr/sleep.h> // includes library for sleep
#include <avr/power.h> // includes library for sleep
#include <avr/wdt.h> // watchdog timer
unsigned short int SolVlts = 2;
volatile unsigned long AuxVolts = 0;
byte intCounter, adcsra, mcucr1, mcucr2;
volatile int f_wdt=1;
short unsigned int auxcharging = 0;
short unsigned int Relay1on = 9;
short unsigned int Relay1off = 10;
float AuxBatt = 0;
volatile short unsigned int val = 0;
volatile unsigned long PowerCheckCount = 0;
ISR(WDT_vect)
{
if(f_wdt == 0)
{
f_wdt=1;
}
else
{
//Serial.println("WDT Overrun Error!!!");
}
}
void enterSleep(void)
{
// * SLEEP_MODE_IDLE -the least power savings
// * SLEEP_MODE_ADC
// * SLEEP_MODE_PWR_SAVE
// * SLEEP_MODE_STANDBY
// * SLEEP_MODE_PWR_DOWN -the most power savings
sleep_enable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// #ifdef WDTO_8S
wdt_enable(WDTO_8S); // enable WDT 8 seconds
wdt_reset();
adcsra = ADCSRA; //save the ADC Control and Status Register A
ADCSRA = 0; //disable ADC
cli();
mcucr1 = MCUCR | _BV(BODS) | _BV(BODSE); //turn off the brown-out detector
mcucr2 = mcucr1 & ~_BV(BODSE);
MCUCR = mcucr1;
MCUCR = mcucr2;
/* Now enter sleep mode. */
sleep_mode();
/* The program will continue from here after the WDT timeout*/
sleep_disable(); /* First thing to do is disable sleep. */
ADCSRA = adcsra;
/* Re-enable the peripherals. */
power_all_enable();
}
void SleepCheck()
{
int val;
val = digitalRead(SolVlts); // read the 1.092v/5v charge ready input pin
if (val > 0 && auxcharging == 0) // if 1.092v present boolean equals 1 / true
{
sleep_enable();
wdt_reset();
wdt_disable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
EIMSK |= _BV(INT0); //enable INT0
adcsra = ADCSRA; //save the ADC Control and Status Register A
ADCSRA = 0; //disable ADC
cli();
mcucr1 = MCUCR | _BV(BODS) | _BV(BODSE); //turn off the brown-out detector
mcucr2 = mcucr1 & ~_BV(BODSE);
MCUCR = mcucr1;
MCUCR = mcucr2;
sei(); //ensure interrupts enabled so we can wake up again
sleep_cpu(); //go to sleep
sleep_disable(); //wake up here
ADCSRA = adcsra; //restore ADCSRA
}
}
ISR(INT0_vect)
{
intCounter++;
EIMSK &= ~_BV(INT0); //one interrupt to wake up only
}
void AuxBattMan()
{
AuxVolts = analogRead(A2);
AuxVolts = analogRead(A2);
AuxVolts = analogRead(A2);
AuxVolts = analogRead(A2);
AuxBatt = AuxVolts / 240.70; // Aux cell resistive divide ratio converter 1023 / 240.70 = 4.25
if (AuxBatt < 4.1 && auxcharging == 0)
{
digitalWrite(Relay1on, HIGH);
//delay(4);
digitalWrite(Relay1on, LOW);
auxcharging=1;
}
if (AuxBatt > 4.1 && auxcharging == 1)
{
digitalWrite(Relay1off, HIGH);
//delay(4);
digitalWrite(Relay1off, LOW);
auxcharging=0;
}
}
void MiniSleep()
{
if(f_wdt == 1)
{
f_wdt = 0;
if (auxcharging == 1)
{
PowerCheckCount = PowerCheckCount+1;
}
// Re-enter sleep mode. //
PowerCheckCount = PowerCheckCount+1;
enterSleep();
}
else
{
// Do nothing. //
}
}
void PowerChecker()
{
if (PowerCheckCount >= 7) //12 hours = 4800 1 hour = 400 1 min = 7
{
digitalWrite(Relay1off, HIGH);
delay(1);
digitalWrite(Relay1off, LOW);
PowerCheckCount = 0;
}
Serial.println(PowerCheckCount);
}
void StatusFlash()
{
if (auxcharging == 0)
{
digitalWrite(13, HIGH);
delay(1);
digitalWrite(13, LOW);
delay(1);
}
if (auxcharging == 1)
{
digitalWrite(13, HIGH);
delay(1);
digitalWrite(13, LOW);
delay(1);
digitalWrite(13, HIGH);
delay(1);
digitalWrite(13, LOW);
delay(1);
}
}
void setup() {
//CLKPR = (1<<CLKPCE);
//CLKPR = B00000011; // prescaler powersaver / rampup
// 0000 - divide crystal by 1
// 0001 - divide crystal by 2
// 0010 - divide crystal by 4
// 0011 - divide crystal by 8
// 0100 - divide crystal by 16
// 0101 - divide crystal by 32
// 0110 - divide crystal by 64
// 0111 - divide crystal by 128
// 1000 - divide crystal by 256
analogReference(INTERNAL); // sets reference to internal 1.092v
pinMode(13, OUTPUT); // charge active light
pinMode(SolVlts, INPUT); // specifies pin 2 as available solar volts suitable for charge
EICRA = 0x00; //configure INT0 to trigger on low level
Serial.begin(9600);
delay(2);
if (PowerCheckCount > 0)
{
Serial.println(" Remembers Yay ");
delay(3);
}
if (PowerCheckCount == 0)
{
Serial.println(" Starting Up Forgotten ");
delay(3);
}
delay(4);
/*** Setup the WDT ***/
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).
*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
/* Enable the WD interrupt (note no reset). */
WDTCSR |= _BV(WDIE);
//digitalWrite(Relay1on, HIGH);
//delay(4);
//digitalWrite(Relay1on, LOW);
//delay(4);
//digitalWrite(Relay1off, HIGH);
//delay(4);
//digitalWrite(Relay1off, LOW);
auxcharging=0;
}
void loop()
{
SleepCheck(); // divide#mosfet#pin2 12v solar ready triggers pin
//SerialDiagnostics();
//AuxBattMan();
//Serial.print(AuxBatt);
//delay(2);
Serial.println("Working");
delay(3);
//Serial.print(AuxVolts);
//delay(2);
StatusFlash();
delay(1000);
MiniSleep(); // WDT Sleep for 8 seconds
//PowerChecker();
}