Last active
July 10, 2023 22:37
-
-
Save mpentler/b74f4829248a855a7f5eb1be1b50a420 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 | |
// Code rev: v0.3 07/08/2020 | |
// 0.01: initial Arduino test code, ATMega328p based | |
// 0.1: Changed to ATTiny85, compiles fine | |
// 0.15: Switched to ATTiny13 and Microcore, switched to direct I/O and removed Arduino-like helper functions | |
// 0.2: Global LEDpin variable replaced with spare register - now 0 bytes RAM used!, compiled size now reduced by a third since 0.1! | |
// 0.3: Power improvements: ADC disabled saved 200-300ish microamps | |
#include <avr/sleep.h> | |
#include <avr/interrupt.h> | |
#include <avr/io.h> | |
#include <avr/power.h> | |
void setup() { | |
// Configure our input and output pins | |
DDRB = 0b00000111; // PB0-2 as outputs, leave PB3 (4th bit) as input (0), the rest as inputs | |
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 | |
ADCSRA = 0; // Disable the ADC to save power | |
// Flash quick sequence so we know setup has finished | |
uint8_t k = 30; | |
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! | |
} | |
delayMicroseconds(16383); // cheaper in bytes than delay() but you can't delay for as long as you'd like | |
k--; | |
} while (k); | |
PORTB |= (1 << PB0); // Light green to start with | |
EEARL = PB0; // Use a spare register to keep track of LED, set it to green initially. | |
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 << EEARL); // turn off the current LED | |
EEARL++; | |
if (EEARL == 3) { // have we gone past red?... | |
EEARL = PB0; // if so, cycle back to green again | |
} | |
PORTB |= (1 << EEARL); // Light the new LED | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment