Last active
August 6, 2020 11:02
-
-
Save mpentler/4d966cf83f14e09104bffcd38ed485bf 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
#include <avr/sleep.h> | |
#include <avr/interrupt.h> | |
const int redLED = PB2; // Define the LED pins | |
const int yellowLED = PB1; | |
const int greenLED = PB0; | |
int LEDpin = greenLED; // 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 | |
// Flash quick sequence so we know setup has started | |
for (int k = 0; k < 10; k = k + 1) { | |
if (k % 2 == 0) { | |
PORTB = 0b00001111; | |
} | |
else { | |
PORTB = 0b00001000; | |
} | |
delay(250); | |
} | |
PORTB |= (1 << LEDpin); | |
} | |
void sleep() { | |
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts | |
PCMSK |= _BV(PCINT3); // Use PB3 as interrupt pin | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // set the correct mode | |
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT) | |
sei(); // Enable interrupts | |
sleep_cpu(); // sleep | |
// =========================================== sleeps here | |
cli(); // Disable interrupts | |
PCMSK &= ~_BV(PCINT3); // Turn off PB3 as interrupt pin | |
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 < redLED) { // if we're not currently on red... | |
LEDpin++; // ...we iterate the pin by one | |
} | |
else { | |
LEDpin = greenLED; // otherwise we cycle back to green again | |
} | |
PORTB |= (1 << LEDpin); // Light the new LED | |
} | |
} | |
void loop() { | |
sleep(); // call our sleep routine | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment