Advertisement
pleasedontcode

**LED Control** rev_02

Nov 27th, 2024
82
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: **LED Control**
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2024-11-27 14:46:31
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* LCD Menu with Options */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* simple menu for your LCD          program a menu */
  23.     /* to control led on and off using a key */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <LiquidCrystal_I2C.h>
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void displayMenu(void);
  36. void controlLED(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t B_PushButton_PIN_D5 = 5;
  40.  
  41. /***** DEFINITION OF I2C PINS *****/
  42. const uint8_t A_LCD1602I2C_I2C_PIN_SDA_D2 = 2;
  43. const uint8_t A_LCD1602I2C_I2C_PIN_SCL_D1 = 1;
  44. const uint8_t A_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  45.  
  46. /***** LCD Configuration *****/
  47. LiquidCrystal_I2C lcd(A_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Initialize LCD with I2C address, columns, and rows
  48.  
  49. bool ledState = false; // Variable to hold the LED state
  50.  
  51. void setup(void)
  52. {
  53.     // put your setup code here, to run once:
  54.     pinMode(B_PushButton_PIN_D5, INPUT_PULLUP);
  55.     lcd.begin(); // Initialize the LCD
  56.     lcd.backlight(); // Turn on the backlight
  57.     displayMenu(); // Display the menu on the LCD
  58. }
  59.  
  60. void loop(void)
  61. {
  62.     // put your main code here, to run repeatedly:
  63.     if (digitalRead(B_PushButton_PIN_D5) == LOW) { // Check if the button is pressed
  64.         controlLED(); // Control the LED based on the button press
  65.         delay(500); // Debounce delay
  66.     }
  67. }
  68.  
  69. void displayMenu(void)
  70. {
  71.     lcd.clear(); // Clear the LCD
  72.     lcd.setCursor(0, 0); // Set cursor to the first line
  73.     lcd.print("LED Control Menu"); // Display menu title
  74.     lcd.setCursor(0, 1); // Set cursor to the second line
  75.     lcd.print("Press button"); // Prompt user to press the button
  76. }
  77.  
  78. void controlLED(void)
  79. {
  80.     ledState = !ledState; // Toggle LED state
  81.     if (ledState) {
  82.         // Code to turn on the LED
  83.         digitalWrite(LED_BUILTIN, HIGH); // Assuming LED_BUILTIN is used
  84.         lcd.setCursor(0, 1); // Set cursor to the second line
  85.         lcd.print("LED: ON      "); // Update display
  86.     } else {
  87.         // Code to turn off the LED
  88.         digitalWrite(LED_BUILTIN, LOW); // Assuming LED_BUILTIN is used
  89.         lcd.setCursor(0, 1); // Set cursor to the second line
  90.         lcd.print("LED: OFF     "); // Update display
  91.     }
  92. }
  93.  
  94. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement