/* Low Power ATtiny402 David Johnson-Davies - www.technoblogy.com - 15th October 2019 ATtiny402 @ 5 MHz (internal oscillator; BOD disabled) CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ #include // Arduino pins const int Speaker = 0; // PA6 piezo speaker pin const int Lamp = 1; // PA7 LED pin const int Button = 4; // PA3 pushbutton const int Unused2 = 2; // PA1 const int Unused3 = 3; // PA2 const int Unused5 = 5; // PA0/UPDI // Constants const unsigned long Alarm = 900000; // alarm time - 15 minutes void playBeep(void) { tone(Speaker, 523, 250); delay(250); tone(Speaker, 659, 250); delay(250); tone(Speaker, 523, 500); delay(500); noTone(Speaker); } void flashLed (int wait) { digitalWrite(Lamp, HIGH); delay(wait); digitalWrite(Lamp, LOW); } ISR(PORTA_PORT_vect) { PORTA.INTFLAGS = PORT_INT3_bm; // Clear PA3 interrupt flag } // Main loop ********************************************** void setup() { pinMode(Lamp, OUTPUT); pinMode(Speaker, OUTPUT); PORTA.PIN3CTRL = PORT_PULLUPEN_bm; // Same as pinMode(Button, INPUT_PULLUP); // Don't leave unused pins floating pinMode(Unused2, INPUT_PULLUP); pinMode(Unused3, INPUT_PULLUP); pinMode(Unused5, INPUT_PULLUP); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); } void loop () { unsigned long StartTime = millis(); // Long flash flashLed(125); delay(125); flashLed(125); while (millis() - StartTime < Alarm) { flashLed(1); delay(2000); } // Alarm playBeep(); delay(1000); PORTA.PIN3CTRL = PORT_PULLUPEN_bm | PORT_ISC_LEVEL_gc; // Pullup + Trigger on low level sleep_cpu(); // Continue after interrupt PORTA.PIN3CTRL = PORT_PULLUPEN_bm; // Turn off pin sense interrupt }