CCCPaste

"LDR Status" rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "LDR Status"
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-08-06 16:26:54

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* esp32 code for fault detection in 5 lights based */
	/* on 5 LDR threshold and display which light is */
	/* fault in lcd display */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include  // https://github.com/offcircuit/LCDIC2

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF I2C PINS *****/
const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_D21 = 21; // SDA pin
const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_D22 = 22; // SCL pin
const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // I2C address for the LCD

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Initialize the LCDIC2 object with the I2C address and dimensions of the display
LCDIC2 lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // 16 columns and 2 rows

// Define the LDR pins for the lights
const uint8_t ldrPins[5] = {34, 35, 32, 33, 25}; // Example GPIO pins for LDRs
const int threshold = 500; // Threshold value for fault detection

void setup(void)
{
    // Initialize the LCD and print a welcome message
    lcd.begin(); // Removed the conditional check for lcd.begin()
    lcd.print("System Ready"); // Print a message on the LCD
    
    // Initialize LDR pins as input
    for (int i = 0; i < 5; i++) {
        pinMode(ldrPins[i], INPUT);
    }
}

void loop(void)
{
    // Variable to track which light is faulty
    int faultyLight = -1;

    // Check each LDR to see if it is below the threshold
    for (int i = 0; i < 5; i++) {
        int ldrValue = analogRead(ldrPins[i]); // Read the LDR value
        if (ldrValue < threshold) {
            faultyLight = i; // Mark the faulty light
            break; // Exit the loop if a fault is detected
        }
    }

    // Display the faulty light on the LCD
    lcd.clear(); // Clear the previous message
    if (faultyLight != -1) {
        lcd.print("Faulty Light: ");
        lcd.print(faultyLight + 1); // Displaying light number (1-5)
    } else {
        lcd.print("All Lights OK"); // Display if no faults detected
    }

    delay(1000); // Wait for a second before the next check
}

/* END CODE */