I am building a battery operated RF remote , so I added a sleep function into a case statement to save battery life.
My problem is that I can not awaken it from sleep with the use of a push button switch.
If I move sei();
or the
attachInterrupt (digitalPinToInterrupt(3), sleepISR,LOW);
above
sleep_cpu
Then it will not go into full sleep. the case keeps cycling.
I have to power it off and back on to get it going again.
using an Atmega88PA on a breadboard and Arduino programming
everything works as it should including transmitting and receiving, but the wakeup won’t work
Probably something simple in the order of the code
FYI: this is my first attempt at sleeping a chip.
I also tried to run this outside of this case statement, using
if (millis()- timeoutStart2 > 5000)
, but, that didn’t work correctly either. It ran even though I never entered to 1st case of the switch case ??
Any thoughts ?
THANKS Fran
//USING ATMEGA88PA
//internal 8mhz processor
//THIS IS A SHORTENNED VERSION OF FULL CODE
#include <RCSwitch.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
RCSwitch Remote = RCSwitch();
const int sw1 = 20;
const int sw2 = 21;
const int sw3 = 3; //ALSO WAKE PIN
const int LED15 = 15;
const byte timeout1 = 1;
const byte timeout2 = 2;
const byte timeout3 = 3;
const byte timeout4 = 4;
const byte timeout5 = 5;
const byte timeout6 = 6;
byte timeout = timeout1;
unsigned long timeoutStart;
unsigned long timeoutStart2;
// PIN9 used to transmit
// PIN2 used to receive
void setup() {
pinMode(LED15, OUTPUT); //power
Serial.begin(9600);
Remote.enableTransmit(9);
Remote.enableReceive(0);
unsigned long int RF_data = Remote.getReceivedValue() ;
Remote.setProtocol(1);
Remote.setPulseLength(400);
}
void loop() {
//1 MINUTE NON USE of sw1 TIMEOUT plus 5 second to full sleep
switch (timeout)
{
case timeout1://1
if (digitalRead(sw1) == LOW)
digitalWrite(LED15, HIGH);
Remote.send(5592323, 24);
timeoutStart2 = 0;
{ timeoutStart = millis();
timeout = timeout2;
}
break;
case timeout2: //2
if (digitalRead(sw1) == LOW)
{
timeout = timeout4;
}
if (digitalRead(sw1) == HIGH && millis() - timeoutStart >= 60000)
{
timeout = timeout3;
}
break;
case timeout3: //3 warning blink
{ digitalWrite(LED15, LOW); delay(200);
digitalWrite(LED15, HIGH); delay(200);
digitalWrite(LED15, LOW); delay(200);
digitalWrite(LED15, HIGH); delay(200);
timeoutStart = 0;
timeoutStart2 = millis();
timeout = timeout4;
}
break;
case timeout4: //4
if ( digitalRead(sw1) == LOW)
{ timeoutStart = 0;
timeout = timeout1;
}
if (digitalRead(sw1) == HIGH)
{
timeout = timeout5;
}
break;
case timeout5: //5
if (digitalRead(sw1) == LOW)
{ timeoutStart2 = 0;
timeout = timeout1;
}
if (millis() - timeoutStart2 > 5000)
{ digitalWrite (LED15, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli();// no Interrupts
sleep_enable();
sleep_bod_disable();
sleep_cpu();
attachInterrupt (digitalPinToInterrupt(3), sleepISR, LOW);
sei(); //SET interrupt
timeout = timeout1;
}
break;
}
if (digitalRead (sw3) == LOW)
{ digitalWrite(LED14, HIGH);
Remote.send(5592335, 24);
}
}
//turn back on
void sleepISR()
{
detachInterrupt (digitalPinToInterrupt(3));
sleep_disable ();
}