#include <LiquidCrystal_I2C.h>
// Define I2C address for the display
#define I2C_ADDR 0x27
// Define pins for the LEDs
const int redLEDPin = 13;
const int greenLEDPin = 12;
const int yellowLEDPin = 11;
const int buttonPin = 2;
// Create an LCD object
LiquidCrystal_I2C lcd(I2C_ADDR, 16, 2);
// Variable to store the button state
int buttonState = 0;
// Variable to store the previous button state
int lastButtonState = 0;
// Variable to store the count
int count = 0;
void setup() {
// Initialize the button pin as input with internal pullup resistor
pinMode(buttonPin, INPUT_PULLUP);
// Initialize LED pins as output
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
// Turn off all LEDs initially
digitalWrite(redLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check if the button state has changed
if (buttonState != lastButtonState) {
// If the button is pressed
if (buttonState == LOW) {
// Increment the count
count++;
}
// Update the previous button state
lastButtonState = buttonState;
}
// Update the LCD display
lcd.setCursor(0, 0); // Set cursor to the beginning of the first line
lcd.print("Count: ");
lcd.print(count);
// Control the LEDs based on the count
if (count < 100) {
digitalWrite(yellowLEDPin, HIGH); // Yellow LED on
digitalWrite(redLEDPin, LOW); // Red LED off
digitalWrite(greenLEDPin, LOW); // Green LED off
} else if (count >= 100 && count < 200) {
digitalWrite(greenLEDPin, HIGH); // Green LED on
digitalWrite(yellowLEDPin, LOW); // Yellow LED off
digitalWrite(redLEDPin, LOW); // Red LED off
} else if (count >= 200) {
digitalWrite(redLEDPin, HIGH); // Red LED on
digitalWrite(greenLEDPin, LOW); // Green LED off
digitalWrite(yellowLEDPin, LOW); // Yellow LED off
}
// Delay for a short period
delay(100);
}