Skip to content

Instantly share code, notes, and snippets.

@mpentler
Created August 5, 2020 14:55
Show Gist options
  • Save mpentler/046f66307973300144bcdb28ba378125 to your computer and use it in GitHub Desktop.
Save mpentler/046f66307973300144bcdb28ba378125 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
uint8_t k = 10;
do {
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);
k--;
} while (k);
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

260 bytes. Can anyone do any better?

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