#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the I2C LCD display (address: 0x27, size: 16x2)
LiquidCrystal_I2C lcd(0x27, 16, 2);
int led1 = 8; // LED 1 connected to pin 6
int led2 = 7; // LED 2 connected to pin 7
int led3 = 6; // LED 3 connected to pin 8
// Analog pins for voltage measurement
int voltPin1 = A0; // Voltage measurement for LED 1
int voltPin2 = A1; // Voltage measurement for LED 2
int voltPin3 = A2; // Voltage measurement for LED 3
int currentLED = 0; // To keep track of the current LED being displayed
void setup() {
// Set LED pins as outputs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
// Initialize the LCD display with the correct number of columns and rows
lcd.begin(16, 2);
lcd.backlight(); // Turn on the backlight
// Print initial messages on the LCD
lcd.setCursor(0, 0); // First row, first column
lcd.print("LED Status:");
}
void loop() {
// Turn on the current LED and turn off others
digitalWrite(led1, currentLED == 0 ? HIGH : LOW);
digitalWrite(led2, currentLED == 1 ? HIGH : LOW);
digitalWrite(led3, currentLED == 2 ? HIGH : LOW);
updateLCD(); // Update the LCD with the current LED status and voltage
delay(1000); // Wait for 1 second
// Cycle through LEDs
currentLED = (currentLED + 1) % 3; // Move to the next LED
}
// Function to update the LCD with the status and voltage of the LEDs
void updateLCD() {
lcd.setCursor(0, 1); // Second row, first column
// Display LED status
// Read voltage for the current LED
float voltage = 0;
if (currentLED == 0) {
lcd.print("L1:");
lcd.print(digitalRead(led1) ? "On " : "Off ");
voltage = analogRead(voltPin1) * (5.0 / 1023.0);
lcd.print("V1:");
} else if (currentLED == 1) {
lcd.print("L2:");
lcd.print(digitalRead(led2) ? "On " : "Off ");
voltage = analogRead(voltPin2) * (5.0 / 1023.0);
lcd.print("V2:");
} else if (currentLED == 2) {
lcd.print("L3:");
lcd.print(digitalRead(led3) ? "On " : "Off ");
voltage = analogRead(voltPin3) * (5.0 / 1023.0);
lcd.print("V3:");
}
lcd.print(voltage, 2); // Print the voltage with 2 decimal places
}