Hi, can have a question about my code. I have written a code for the lighting system (if Day the led light will turn off, the LCD will show the day led turned off, and when dark the led will turn on, and the LCD will show the night led turned on). Then I need to add a notification if there is a led faulty the red led will turn on and show which led is faulty (in this part I already wrote in the code but it is not working). Can you help me to check thank you very much.
#include <LiquidCrystal.h>
// include liquid crystal library
const int rs = 12, e = 11, db4 = 5, db5 = 4, db6 = 3, db7 = 2;
LiquidCrystal lcd(rs, e, db4, db5, db6, db7);
//LCD Pins connection
int ldrPin = A0;
int ledPin1 = 6;
int ledPin2 = 7;
int ledPin3 = 8;
int ledFaulty = 9; // Pin for LED faulty status
void setup()
{
lcd.begin(16, 2); // Set lcd number of row and column
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledFaulty, INPUT_PULLUP); // Set pin for LED faulty status as input with internal pull-up resistor
pinMode(ldrPin, INPUT);
}
void loop()
{
int ldrStatus = analogRead(ldrPin);
bool led1Faulty = digitalRead(ledFaulty); // Read the status of ledPin1
bool led2Faulty = digitalRead(ledFaulty); // Read the status of ledPin2
bool led3Faulty = digitalRead(ledFaulty); // Read the status of ledPin3
// Turn off the red LED and display error message on the LCD if any of the LED pins is faulty
if (led1Faulty || led2Faulty || led3Faulty)
{
digitalWrite(ledFaulty,HIGH);
lcd.setCursor(1, 0); // Set lcd cursor to the second row and first column
lcd.print("LED"); // Display the message "LED" on the LCD
if (led1Faulty)
{
lcd.print("1"); // Display "1" on the LCD if ledPin1 is faulty
}
if (led2Faulty)
{
lcd.print("2"); // Display "2" on the LCD if ledPin2 is faulty
}
if (led3Faulty)
{
lcd.print("3"); // Display "3" on the LCD if ledPin3 is faulty
}
lcd.print(" faulty"); // Display "faulty" on the LCD
}
else
{
digitalWrite(ledFaulty, LOW); // Turn off the red LED if none of the LED pins is faulty
if (ldrStatus <= 200)
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
Serial.print("It's BRIGHT, LED turn off : ");
Serial.println(ldrStatus);
lcd.setCursor(1, 0); // Set lcd cursor to the second row and first column
lcd.print("DAY !"); // First line message
lcd.setCursor(1, 1); // Set lcd cursor to the second row and second column
lcd.print("LED turn off "); // Second row message
}
else
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
Serial.print("It's DARK, LED turn on : ");
Serial.println(ldrStatus);
lcd.setCursor(1, 0); // Set lcd cursor to the second row and first column
lcd.print("NIGHT!"); // First row message
lcd.setCursor(1, 1); // Set lcd cursor to the second row and second column
lcd.print("LED turn on "); // Second row message
}
}
}
#include <LiquidCrystal.h>
// include liquid crystal library
const int rs = 12, e = 11, db4 = 5, db5 = 4, db6 = 3, db7 = 2;
LiquidCrystal lcd(rs, e, db4, db5, db6, db7);
//LCD Pins connection
int ldrPin = A0;
int ledPin1 = 6;
int ledPin2 = 7;
int ledPin3 = 8;
int ledFaulty = 9; // Pin for LED faulty status
void setup()
{
lcd.begin(16, 2); // Set lcd number of row and column
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledFaulty, INPUT_PULLUP); // Set pin for LED faulty status as input with internal pull-up resistor
pinMode(ldrPin, INPUT);
}
void loop()
{
int ldrStatus = analogRead(ldrPin);
bool led1Faulty = digitalRead(ledFaulty); // Read the status of ledPin1
bool led2Faulty = digitalRead(ledFaulty); // Read the status of ledPin2
bool led3Faulty = digitalRead(ledFaulty); // Read the status of ledPin3
// Turn off the red LED and display error message on the LCD if any of the LED pins is faulty
if (led1Faulty || led2Faulty || led3Faulty)
{
digitalWrite(ledFaulty,HIGH);
lcd.setCursor(1, 0); // Set lcd cursor to the second row and first column
lcd.print("LED"); // Display the message "LED" on the LCD
if (led1Faulty)
{
lcd.print("1"); // Display "1" on the LCD if ledPin1 is faulty
}
if (led2Faulty)
{
lcd.print("2"); // Display "2" on the LCD if ledPin2 is faulty
}
if (led3Faulty)
{
lcd.print("3"); // Display "3" on the LCD if ledPin3 is faulty
}
lcd.print(" faulty"); // Display "faulty" on the LCD
}
else
{
digitalWrite(ledFaulty, LOW); // Turn off the red LED if none of the LED pins is faulty
if (ldrStatus <= 200)
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
Serial.print("It's BRIGHT, LED turn off : ");
Serial.println(ldrStatus);
lcd.setCursor(1, 0); // Set lcd cursor to the second row and first column
lcd.print("DAY !"); // First line message
lcd.setCursor(1, 1); // Set lcd cursor to the second row and second column
lcd.print("LED turn off "); // Second row message
}
else
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
Serial.print("It's DARK, LED turn on : ");
Serial.println(ldrStatus);
lcd.setCursor(1, 0); // Set lcd cursor to the second row and first column
lcd.print("NIGHT!"); // First row message
lcd.setCursor(1, 1); // Set lcd cursor to the second row and second column
lcd.print("LED turn on "); // Second row message
}
}
}