Created
August 6, 2020 15:19
-
-
Save mpentler/9f4f3f46866e9152a9375fb84467febb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Conference Mood Badge - Mark Pentler 2020 | |
// Tested with: ATTiny85 and 13 | |
// | |
#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 | |
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 { | |
PORTB = 0b00001000; // No else condition if we turn all lights off each pass - Notice PB3 is still 1 (so we don't lose the pullup) | |
if (k % 2 == 0) { | |
PORTB = 0b00001111; // Light all the lights! | |
} | |
delay(250); | |
k--; | |
} while (k); | |
PORTB |= (1 << PB0); // Light green to start with | |
sei(); // Enable interrupts | |
} | |
void loop() { | |
sleep(); // call our sleep routine | |
} | |
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment