void LowPower_::sleepWithTheDog() { WDTCONSET = 1<<15; // Turn on WDTCONSET = 0x01; // Kick the dog! int r = disableInterrupts(); enterSleepMode(); restoreInterrupts(r); WDTCONCLR = 1<<15; // Turn off }
void loop() { if (cycleVoltageMode == true) { outputSystemMode(sk720Mode); #ifdef DEBUG debug("current state: "); printSystemMode(sk720Mode); #endif sk720Mode = getNextSystemState(sk720Mode); delay(CYCLE_MODE_DELAY_MS); } else { if (wakeup == true) { wakeup = false; const unsigned long now = micros(); if (pinChanged == true) { pinChanged = false; readLedStates(&systemLedState, now); } sk720ModeBuffer[currentSk720Mode] = inferSystemMode(&systemLedState, now); #ifdef DEBUG SK720Mode oldState = sk720Mode; #endif sk720Mode = getFilteredSk720Mode(sk720ModeBuffer, sk720Mode, sk720ModeBuffer[currentSk720Mode]); #ifdef DEBUG if (sk720Mode != oldState) { debug("time "); debuglnNum(now); debug("redLed "); printSingleLedMode(systemLedState.red.mode); debug("greenLed "); printSingleLedMode(systemLedState.green.mode); debug("newState "); printSystemMode(sk720ModeBuffer[currentSk720Mode]); debug("filteredState "); printSystemState(sk720Mode); debug("filterIndex "); debuglnNum(currentSk720Mode); printLedState(&systemLedState.red); printLedState(&systemLedState.green); debugln("---"); } #endif if (currentSk720Mode == STATE_BUFFER_SIZE - 1) { currentSk720Mode = 0; } else { currentSk720Mode++; } outputSystemMode(sk720Mode); } enterSleepMode(); } }
void main(void) { //assume switch is off at bootup (see flowchart) //enable global interrupts sei(); //enable pin change interrupt on SWITCH_PIN, FRONT_LED_PIN, and BACK_LED_PIN PCMSK = 1<<SWITCH_PIN | 1<<FRONT_LED_PIN | 1<<BACK_LED_PIN; //clear PC interrupt flag in case it got randomly set during bootup GIFR |= 1<<PCIF; //enable PC interrupt GIMSK = 1<<PCIE; while(1) { //everything happens in PC interrupt routine enterSleepMode(); } }