I turn off vext before setting the state to LOWPOWER. But vext turns back on again while asleep or going to sleep. I know this because i have a GPS vcc connected to Vext. and the PPS led keeps flashing.
My sleep prep code:
void onSleep()
{
turnOffRGB();
Radio.Rx(0);
Radio.Sleep();
delay(3000);
uint32_t sleepTime;
volts = getBatteryVoltage();
while (second()>59){ // make sure seconds are not going to flip over while calculating sleep time
}
int MyMinute=minute();
while(MyMinute>=WakeInterval){
MyMinute=MyMinute-WakeInterval;
}
sleepTime=(((WakeInterval)-MyMinute-1)60)+(61-second());
if (ValidTime==false){
sleepTime= map(constrain(volts,3600,4220),3600,4220,655,7); // time sync lost so sleep for between 30 seconds and 6 minutes
if (debug==true){
Serial.print("Not synced… ");
}
}
if (volts<LowVoltage){
sleepTime+=36008; // sleep for 6 extra hours as low battery
}
if (debug==true){
Serial.print("VOLTS=");
Serial.println((float)volts/1000);
digitalClockDisplay();
Serial.print("Time Awake ");
Serial.print((unsigned long)(millis()-WakeMillis)/1000);
Serial.println(" Seconds");
Serial.print("Sleep for ");
Serial.print (sleepTime);
Serial.println(" seconds");
Serial.println(millis());
delay(100);
}
Serial.flush();
Serial.end();
// iicSerial1.flush();
// iicSerial1.end();
Wire.end();
VEXToff();
digitalWrite(Vext,HIGH); // turn off power to the sensor
CySysWdtDisable();
TimerInit( &wakeUp, onWakeUp );
TimerSetValue( &wakeUp, sleepTime*1000 );
TimerStart( &wakeUp );
//CySysPmDeepSleep;
state=LOWPOWER;
}
my machine state loop:
void loop()
{
if (millis()-WDTmillis>(60*30*1000)){ // if not gone to sleep for 30 minutes
HW_Reset(0); //failed to power down so reboot
}
switch(state)
{
case TX:
TXPacket();
break;
case RX:
RXhandle();
break;
case WAIT_TX_DONE:
break;
case CHECK_BACK:
state=RX;
break;
case LOWPOWER:
SleepMillis=millis();
LoRaWAN.sleep();
//lowPowerHandler();
break;
default:
break;
}
if (state!=LOWPOWER){
Radio.IrqProcess( );
}
}