//*EXERCISE: If the push button is pressed, the relay turns ON and the LCD displays "Relay 1 ON". If the push button is released,the relay turns off and the LCD displays "OFF".
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
int relay1=4, relay2=5;
int pb1=2, pb2=3;
int val, val2;
void setup() {
// put your setup code here, to run once:
pinMode(relay1,OUTPUT);
pinMode(pb1,INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Relay Module");
delay(200);
lcd.setCursor(0,1);
lcd.print("Activity");
delay(5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RELAY 1");
lcd.setCursor(0,1);
lcd.print("RELAY 2");
}
void loop() {
// put your main code here, to run repeatedly:
val = digitalRead(pb1);
val2 = digitalRead(pb2);
if(val==LOW){
digitalWrite(relay1, HIGH);
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print("ON");
}
else{
digitalWrite(relay1, LOW);
lcd.setCursor(8,0);
lcd.print("OFF");
}
if(val2==LOW){
digitalWrite(relay2,HIGH);
lcd.setCursor(8,1);
lcd.print(" ");
lcd.setCursor(8,1);
lcd.print("ON");
}
else{digitalWrite(relay2,LOW);
lcd.setCursor(8,1);
lcd.print("OFF");
}
}