Created
May 3, 2012 02:54
-
-
Save adamzaninovich/2582679 to your computer and use it in GitHub Desktop.
shows the program with and without using an interrupt
This file contains 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
/* | |
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