Skip to content

Instantly share code, notes, and snippets.

@adamzaninovich
Created May 3, 2012 02:54
Show Gist options
  • Save adamzaninovich/2582679 to your computer and use it in GitHub Desktop.
Save adamzaninovich/2582679 to your computer and use it in GitHub Desktop.
shows the program with and without using an interrupt
/*
This program just toggles an led when you press a button
*/
// without interrupt
#define LED 13
#define BUTTON 2
boolean last = LOW;
boolean ledOn = LOW;
void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}
void loop() {
boolean current = digitalRead(BUTTON);
if (current == HIGH && last == LOW) ledOn = !ledOn;
last = current;
digitalWrite(LED, ledOn);
}
// with interrupt...
// gets rid of a global var, a local var, an if, and an assignment
#define LED 13
#define BUTTON 0 // Intr 0 == Pin 2
volatile boolean ledOn = LOW;
void setup() {
pinMode(LED, OUTPUT);
attachInterrupt(BUTTON, toggle, RISING);
}
void loop() {
digitalWrite(LED, ledOn);
}
void toggle() {
ledOn = !ledOn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment