Advertisement
pleasedontcode

**Lamp Control** rev_01

Nov 12th, 2024
65
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: **Lamp Control**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-13 00:41:31
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Control an LED based on LDR sensor readings, */
  21.     /* displaying the light intensity on a LiquidCrystal */
  22.     /* I2C LCD. The LED will turn on in low light and off */
  23.     /* in bright conditions. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <LiquidCrystal_I2C.h> // https://github.com/marcoschwartz/LiquidCrystal_I2C
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  36. LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD with I2C address 0x27, 16 columns and 2 rows
  37.  
  38. int sensorPin = A0;
  39. int sensorValue = 0;
  40.  
  41. void setup(void)
  42. {
  43.     // Initialize serial communication
  44.     Serial.begin(9600);  
  45.     // Set pin 6 as an output for the LED
  46.     pinMode(6, OUTPUT);  
  47.     // Initialize the LCD
  48.     lcd.begin();  
  49.     lcd.print("LDR Lampu Otomatis");  
  50.     lcd.setCursor(0, 1);  
  51.     lcd.print("Praktek Otodidak");  
  52.     delay(3000);  
  53.     lcd.clear();
  54. }
  55.  
  56. void loop(void)
  57. {
  58.     // Read the sensor value from the LDR
  59.     sensorValue = analogRead(sensorPin);  
  60.     Serial.println(sensorValue);  
  61.     // Convert the sensor value to voltage
  62.     float voltage = sensorValue * (5.0 / 1023.0);  
  63.     Serial.println(voltage);  
  64.  
  65.     // Control the LED based on light intensity
  66.     if (voltage <= 1) {    
  67.         digitalWrite(6, HIGH); // Turn on the LED in low light
  68.         lcd.clear();    
  69.         lcd.setCursor(0, 0);    
  70.         lcd.print("Volt : ");    
  71.         lcd.print(voltage);    
  72.         lcd.setCursor(0, 1);    
  73.         lcd.print("Lampu Nyala");    
  74.         delay(1000);    
  75.     }
  76.     else {    
  77.         digitalWrite(6, LOW); // Turn off the LED in bright light
  78.         lcd.clear();      
  79.         lcd.setCursor(0, 0);    
  80.         lcd.print("Volt : ");    
  81.         lcd.print(voltage);    
  82.         lcd.setCursor(0, 1);    
  83.         lcd.print("Lampu Mati");    
  84.         delay(1000);    
  85.     }
  86. }
  87.  
  88. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement