/*
LED Toggle with Delay & Interrupt
led-toggle-interrupt.ino
Use pushbutton switch to toggle LED with interrupt
*/
// Define LED and switch connections
const byte ledPin1 = 13;
const byte ledPin2 = 12;
const byte buttonPin1 = 2;
// Boolean to represent toggle state
volatile bool toggleState = false;
void setup() {
// Set LED pin as output
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Set switch pin as INPUT with pullup
pinMode(buttonPin1, INPUT_PULLUP);
Serial.begin(9600);
// Attach Interrupt to Interrupt Service Routine
attachInterrupt(digitalPinToInterrupt(buttonPin1),checkSwitch, FALLING);
/*Interrupt Vector – the interrupt that you wish to use. Note that this is the internal Interrupt Vector number and NOT the pin number.
ISR – The name of the Interrupt Service Routine function that you are gluing to the interrupt.
Mode – How you want to trigger the interrupt.
For Mode, there are four selections:
RISING – Triggers when an input goes from LOW to HIGH.
FALLING – Triggers when an input goes from HIGH to LOW.
LOW – Triggered when the input stays LOW.
CHANGE – Triggers whenever the input changes state from HIGH to LOW or LOW to HIGH.
*/
}
void loop() {
if (digitalRead(buttonPin1) == LOW) {
Serial.println("Interrupt detected for 5 Seconds");
// Indicate state on LED
digitalWrite(ledPin1, HIGH);
delay (5000); // delay for 5 seconds
// Indicate state off LED
digitalWrite(ledPin1, LOW);
toggleState = false;
}
blink();
}
void blink (){
// 500 ms delay
digitalWrite(ledPin2, HIGH);
Serial.println("Blink LED 1");
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);
Serial.println("..............");
}
void checkSwitch(){
toggleState = true;
}