#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD, using the default I2C address 0x27
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define pins
const int buttonPin = 8; // Button input
const int ledPin = 9; // LED output
const int buzzerPin = 10; // Buzzer output
bool stopRequested = false; // Track if stop has been requested
bool lastButtonState = LOW; // Last button state for debouncing
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50; // Debounce delay in milliseconds
void setup() {
// Initialize LCD, LED, button, and buzzer
lcd.begin(16, 2);
lcd.backlight();
pinMode(buttonPin, INPUT); // Button as input
pinMode(ledPin, OUTPUT); // LED as output
pinMode(buzzerPin, OUTPUT); // Buzzer as output
// Initial LCD message
lcd.setCursor(0, 0);
lcd.print("No Stop Request");
}
void loop() {
// Read button state with debounce
int buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// When button is pressed, toggle stop request
if (buttonState == HIGH && !stopRequested) {
stopRequested = true;
requestStop();
}
}
lastButtonState = buttonState;
// Reset stop request if button is pressed again
if (stopRequested && buttonState == LOW) {
stopRequested = false;
resetDisplay();
}
}
void requestStop() {
// Activate LED and buzzer
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(2000); // Sound the buzzer for 500 ms
digitalWrite(buzzerPin, LOW);
// Display "Stop Requested" on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stop Requested");
delay(5000);
}
void resetDisplay() {
// Turn off LED
digitalWrite(ledPin, LOW);
// Clear LCD and show default message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Stop Request");
}