#include <LiquidCrystal.h>
const int BTN_PIN = 6;
const int RELAY_PIN = 5;
const int RS = 12, EN = 11, D4 = 10, D5 = 9, D6 = 8, D7 = 7;
bool isRelayOn = false;
int oldBtnState = HIGH;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
bool checkButton() {
bool wasPressed = false;
int btnState = digitalRead(BTN_PIN); // read the pin
if (btnState != oldBtnState) { // if it changed
oldBtnState = btnState; // remember the state
if (btnState == LOW) { // was just pressed
//Serial.println("Button Pressed");
wasPressed = true;
} else { // was just released
//Serial.println("Button Released");
}
delay(20); // short delay to debounce button
}
return wasPressed;
}
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(RELAY_PIN, OUTPUT);
lcd.setCursor(1, 0);
lcd.print("Solenoid Test");
lcd.setCursor(1, 1);
lcd.print("Relay is Off");
}
void loop() {
int btnPressed = checkButton();
if (btnPressed) {
isRelayOn = !isRelayOn;
// if relay operation is "backwards" switch lines below
//digitalWrite(RELAY_PIN, isRelayOn);
digitalWrite(RELAY_PIN, !isRelayOn);
lcd.setCursor(10, 1);
lcd.print(isRelayOn ? "On " : "Off");
}
}