#include <Arduino.h>
#include <LiquidCrystal.h>
const int rs = 4, en = 5, d4 = 18, d5 = 19, d6 = 21, d7 = 22; // pin numbers of the lcd display
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // object of the lcd display
bool st_1 = false; // state for button press led1
bool mode_st = false; // state for button press mode
bool st = false; // state for button press led2
int last_val = 0; // detecting change in lux value
void setup() {
Serial.begin(115200); // configuring the baud rate
pinMode(12, OUTPUT); // LED pin configuration
pinMode(14, OUTPUT); // LED pin configuration
pinMode(2, INPUT_PULLUP); // button pin configuration
pinMode(23, INPUT_PULLUP); // button pin configuration
pinMode(15, INPUT_PULLUP); // button pin configuration
lcd.begin(16, 2); // configuring the lcd display size
}
void loop() {
int a = analogRead(34); // taking reading from LDR sensor
int b = map(a, 32, 4063, 0, 255); // converting sensor data into a 0 to 255 value
int e = digitalRead(15); // taking reading from mode button
if (e == 0) {
mode_st = !mode_st;
delay(100);
}
if (mode_st) {
if (last_val != b) {
analogWrite(12, b); // change brightness of LED according to the sensor value
analogWrite(14, b); // change brightness of LED according to the sensor value
Serial.print("Automatic Mode Light LUX:"); // display the reading on the serial monitor
Serial.println(map(a, 32, 4058, 100, 0)); // display the reading on the serial monitor percentages of light lux
lcd.setCursor(0, 0);
lcd.print("Automatic Mode"); // display the sensor data
lcd.setCursor(0, 1); // position of the text on display
lcd.print("Light LUX: "); // display the sensor data
lcd.print(map(a, 32, 4058, 100, 0)); // display the sensor percentage
lcd.print("% "); // display the unit of the sensor
}
}
while (mode_st == false) {
int c = digitalRead(2); // taking reading from button 1
int d = digitalRead(23); // taking reading from button 2
lcd.setCursor(0, 0); // position of the text on display
lcd.print("Manually: ");
if (c == 0) {
st = !st;
delay(10);
}
if (d == 0) {
st_1 = !st_1;
delay(10);
}
if (digitalRead(15) == 0 && mode_st == false) {
lcd.setCursor(0, 0);
lcd.print("Automatic Mode");
lcd.setCursor(0, 1); // position of the text on display
lcd.print(" ");
mode_st = true;
delay(100);
break;
}
if (st) {
lcd.setCursor(0, 1); // position of the text on display
lcd.print("LED1 ON ");
analogWrite(12, 255);
} else {
lcd.setCursor(0, 1); // position of the text on display
lcd.print("LED1 OFF");
analogWrite(12, 0);
}
if (st_1) {
lcd.setCursor(8, 1); // position of the text on display
lcd.print("LED2 ON ");
analogWrite(14, 255);
} else {
lcd.setCursor(8, 1); // position of the text on display
lcd.print("LED2 OFF");
analogWrite(14, 0);
}
}
last_val = b; // recorded the previous sensor data
delay(100); // this speeds up the simulation
}