#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// 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;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
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);
lcd.init(); // initialize the lcd
lcd.backlight();
// 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 6 Seconds");
lcd.clear(); // clear display
lcd.setCursor(4, 0); // move cursor to (4, 0)
lcd.print("INTERRUPT"); // print message at (4, 0)
lcd.setCursor(4, 1); // move cursor to (4, 1)
lcd.print("6 Seconds"); // print message at (4, 1)
// Indicate state on LED
digitalWrite(ledPin1, HIGH);
delay (6000); // delay for 6 seconds
// Indicate state off LED
digitalWrite(ledPin1, LOW);
toggleState = false;
lcd.clear();
}
blink();
}
void blink (){
// 500 ms delay
lcd.setCursor(4, 0); // move cursor to (4, 0)
lcd.print("BLINKING"); // print message at (4, 0)
lcd.setCursor(4, 1); // move cursor to (4, 1)
lcd.print("LED 1"); // print message at (4, 1)
digitalWrite(ledPin2, HIGH);
Serial.println("Blink LED 1");
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);
Serial.println("..............");
}
void checkSwitch(){
toggleState = true;
}