Created
September 22, 2019 00:19
-
-
Save RyanFleck/1a81d05beae7ac63206f48685bb46697 to your computer and use it in GitHub Desktop.
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
/* | |
Ryan Fleck | |
Interrupt Programming Tests | |
*/ | |
// Light for testing feedback. | |
const byte ledPIN = 13; | |
// Interrupt pin zero is digital pin 2. | |
const byte interruptPIN = 2; | |
const long debounceDelay = 250; // 40 ms. | |
long debounceTime; | |
// Enables/disables testing feedback led. | |
volatile boolean enabled; | |
const boolean debug = true; | |
// the setup routine runs once when you press reset: | |
void setup() { | |
if(debug){ | |
Serial.begin(9600); | |
debugp("Debugging enabled."); | |
} | |
debugp("Init program."); | |
// initialize the digital pin as an output. | |
pinMode(ledPIN, OUTPUT); | |
pinMode(interruptPIN, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(interruptPIN), first_ISR, FALLING); | |
debounceTime = millis(); | |
enabled=true; | |
} | |
// the loop routine runs over and over again forever: | |
void loop() { | |
if(enabled){ | |
digitalWrite(ledPIN, HIGH); // turn the LED on (HIGH is the voltage level) | |
delay(200); | |
} | |
digitalWrite(ledPIN, LOW); // turn the LED off by making the voltage LOW | |
delay(200); // wait for a second | |
} | |
void first_ISR() { | |
if( (millis() - debounceTime) > debounceDelay ){ | |
debugp("ISR!"); | |
if(enabled){ | |
enabled=false; | |
}else{ | |
enabled=true; | |
} | |
debounceTime = millis(); | |
} | |
} | |
// Only print serial messages when debugging is enabled. | |
void debugp(String x){ | |
if(debug){ | |
Serial.println(x); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment