Hi all,
my ATtiny is used to realise big power save for ESP8266.
the circuit and code worked fine, until the battery was empty and I replaced the battery. As of that moment, the ATtiny was no longer performing the same cycles. After a few times disconnecting and reconnecting the power, it all worked fine.
I started to investigate this issue and I notice:
When I disconnect (and reconnect) the power while the ATtiny is running : No issue: the ATtiny does his job perfectly after the power cycle: software starts up as expected.
When I disconnect (and reconnect) the power while the ATtiny is in sleep mode: the ATtiny does not start the job. After a few (3 to 5) times of powercycling, the ATtiny starts to funciton correctly. Sometimes it also become "normal" working without additional powercycles... If I wait long enough..
#include <avr/sleep.h>
#include <avr/wdt.h>
#define ESP 4
boolean on = true;
boolean off = false;
// int watchdogIterations = 38; // multiply this by 8 seconds, that is the time the sleep will take. 37.5 = 300 sec = 5 min
int watchdogIterations = 1 ; // multiply this by 8 seconds, that is the time the sleep will take.
int currentIteration = 1; // I want the ESP to be powered when I connect power to the ATtiny.
ISR(WDT_vect) // this part of the code is executed each time the ATtiny wakes up.
{
}
void sleepSetup() {
byte bb = 9 & 7;
bb |= (1<<5); //Set the special 5th bit
//This order of commands is important and cannot be combined
MCUSR &= ~(1<<WDRF); //Clear the watchdog reset
WDTCR |= (1<<WDCE) | (1<<WDE); //Set WD_change enable, set WD enable
WDTCR = bb; //Set new watchdog timeout value
WDTCR |= _BV(WDIE); //Set the interrupt enable, will keep unit from resetting after each int
}
void setup()
{
pinMode (ESP, OUTPUT);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Power down everything, wake up from WDT
sleep_enable();
}
void loop(){
pinMode (ESP, OUTPUT);
currentIteration++;
if (currentIteration > watchdogIterations)
{
digitalWrite (ESP, on);
delay(12000); // This delay (in millisec.) is the amount of time the ESP gets power.
digitalWrite (ESP, off);
currentIteration = 0;
}
sleepSetup();
sleep_mode();
}
What might be the difference if the power interruption occurs during the sleep mode?
How can I correct this? (as the power off cycle should become 10 minutes and the power on cycle is only 12 seconds... I have a high chance that the power is disconnected while the ATtiny is sleeping.)
ATtiny85_ESPswitch_6.ino (1.39 KB)