Skip to content

Instantly share code, notes, and snippets.

@mpentler
Created August 5, 2020 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpentler/da535ab4c8e0996a93008392e021fd20 to your computer and use it in GitHub Desktop.
Save mpentler/da535ab4c8e0996a93008392e021fd20 to your computer and use it in GitHub Desktop.
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/io.h>
uint8_t LEDpin = PB0; // start on green LED
void setup() {
// Configure our input and output pins
DDRB = 0b00000111; // PB0-2 as inputs, leave PB3 (4th bit) as output (0)
PORTB |= (1 << PB3); // enable the pull-up resistor on PB3
sei(); // Enable interrupts
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
PCMSK |= _BV(PCINT3); // Use PB3 as interrupt pin
// Flash quick sequence so we know setup has finished
for (uint8_t k = 0; k < 10; k = k + 1) {
if (k % 2 == 0) {
PORTB = 0b00001111; // Notice PB3 is still 1 (so we don't lose the pullup)
}
else {
PORTB = 0b00001000; // And here too
}
delay(250);
}
PORTB |= (1 << LEDpin);
}
void sleep() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // set the correct mode
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sleep_cpu(); // sleep
// =========================================== sleeps here
sleep_disable(); // Clear SE bit
}
ISR(PCINT0_vect) { // Runs when the button is pushed
if (PINB & (1 << PB3) ) {
PORTB &= ~(1 << LEDpin); // turn off the current LED
if (LEDpin < PB2) { // if we're not currently on red...
LEDpin++; // ...we iterate the pin by one
}
else {
LEDpin = PB0; // otherwise we cycle back to green again
}
PORTB |= (1 << LEDpin); // Light the new LED
}
}
void loop() {
sleep(); // call our sleep routine
}
@mpentler
Copy link
Author

mpentler commented Aug 5, 2020

266 bytes of flash, 1 byte of RAM. Not sure if I can save anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment