Advertisement
pleasedontcode

"LDR Status" rev_01

Aug 6th, 2024
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "LDR Status"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-08-06 16:26:54
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esp32 code for fault detection in 5 lights based */
  21.     /* on 5 LDR threshold and display which light is */
  22.     /* fault in lcd display */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <LCDIC2.h> // https://github.com/offcircuit/LCDIC2
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF I2C PINS *****/
  34. const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_D21 = 21; // SDA pin
  35. const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_D22 = 22; // SCL pin
  36. const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // I2C address for the LCD
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. // Initialize the LCDIC2 object with the I2C address and dimensions of the display
  40. LCDIC2 lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // 16 columns and 2 rows
  41.  
  42. // Define the LDR pins for the lights
  43. const uint8_t ldrPins[5] = {34, 35, 32, 33, 25}; // Example GPIO pins for LDRs
  44. const int threshold = 500; // Threshold value for fault detection
  45.  
  46. void setup(void)
  47. {
  48.     // Initialize the LCD and print a welcome message
  49.     lcd.begin(); // Removed the conditional check for lcd.begin()
  50.     lcd.print("System Ready"); // Print a message on the LCD
  51.    
  52.     // Initialize LDR pins as input
  53.     for (int i = 0; i < 5; i++) {
  54.         pinMode(ldrPins[i], INPUT);
  55.     }
  56. }
  57.  
  58. void loop(void)
  59. {
  60.     // Variable to track which light is faulty
  61.     int faultyLight = -1;
  62.  
  63.     // Check each LDR to see if it is below the threshold
  64.     for (int i = 0; i < 5; i++) {
  65.         int ldrValue = analogRead(ldrPins[i]); // Read the LDR value
  66.         if (ldrValue < threshold) {
  67.             faultyLight = i; // Mark the faulty light
  68.             break; // Exit the loop if a fault is detected
  69.         }
  70.     }
  71.  
  72.     // Display the faulty light on the LCD
  73.     lcd.clear(); // Clear the previous message
  74.     if (faultyLight != -1) {
  75.         lcd.print("Faulty Light: ");
  76.         lcd.print(faultyLight + 1); // Displaying light number (1-5)
  77.     } else {
  78.         lcd.print("All Lights OK"); // Display if no faults detected
  79.     }
  80.  
  81.     delay(1000); // Wait for a second before the next check
  82. }
  83.  
  84. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement