Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpentler/d11d700b15fb65aa375ccef5e72d19ed to your computer and use it in GitHub Desktop.
Save mpentler/d11d700b15fb65aa375ccef5e72d19ed to your computer and use it in GitHub Desktop.
#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
}
@mpentler
Copy link
Author

mpentler commented Aug 5, 2020

Ton of stuff in here is wrong, interrupt code mostly. Refactored in v2

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