Last active
August 6, 2020 11:02
-
-
Save mpentler/d11d700b15fb65aa375ccef5e72d19ed 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 switchPin = 3; // Define our button pin | |
const int redLED = 2; // Now the LED pins | |
const int yellowLED = 1; | |
const int greenLED = 0; | |
int LEDpin = greenLED; // start on green LED | |
void setup() { | |
// Configure our input and output pins | |
pinMode(switchPin, INPUT_PULLUP); // use the internal pull-up resistor | |
pinMode(redLED, OUTPUT); | |
pinMode(yellowLED, OUTPUT); | |
pinMode(greenLED, OUTPUT); | |
// Flash quick sequence so we know setup has started | |
for (int k = 0; k < 10; k = k + 1) { | |
if (k % 2 == 0) { | |
digitalWrite(redLED, HIGH); | |
digitalWrite(yellowLED, HIGH); | |
digitalWrite(greenLED, HIGH); | |
} | |
else { | |
digitalWrite(redLED, LOW); | |
digitalWrite(yellowLED, LOW); | |
digitalWrite(greenLED, LOW); | |
} | |
delay(250); | |
} | |
digitalWrite(LEDpin, HIGH); // light the selected LED! | |
} | |
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 | |
sei(); // Enable interrupts | |
} | |
ISR(PCINT0_vect) { // Runs when the button is pushed | |
if (digitalRead(switchPin) == HIGH) { | |
digitalWrite(LEDpin, LOW); // 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 | |
} | |
digitalWrite(LEDpin, HIGH); // 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
Ton of stuff in here is wrong, interrupt code mostly. Refactored in v2