/*
Wokwi | projects
HELP(arduino)
lawington — March 18, 2026 6:59 PM
⚙ System Requirements
Your Smart Room must:
1. Automatic Light Control
Light turns ON only if:
It is DARK AND a person is detected
2. Manual Override
If button is pressed → Light turns ON regardless of conditions
3. Serial Monitor Output
Display:
Light level value
Distance value
System status (Light ON/OFF)
*/
#include <LiquidCrystal_I2C.h>
// user constants
const int DIST_THRESHOLD = 100;
const int LIGHT_THRESHOLD = 25;
// pin constants
const int TRIG_PIN = 7;
const int ECHO_PIN = 6;
const int BTN_PIN = 5;
const int LED_PIN = 4;
const int LDR_PIN = A0;
// global variables
bool btnMode = false;
bool oldBtnMode = true;
bool isLightOn = false;
bool oldLightOn = true;
int oldDistance = 0;
int oldLightVal = 0;
int oldBtnState = HIGH; // pin idles HIGH
LiquidCrystal_I2C lcd(0x27, 20, 4);
bool checkButton() {
bool wasPressed = false;
int btnState = digitalRead(BTN_PIN); // read the pin
if (btnState != oldBtnState) { // if it changed
oldBtnState = btnState; // remember the state
if (btnState == LOW) { // was just pressed
//Serial.println("Button Pressed");
wasPressed = true;
} else { // was just released
//Serial.println("Button Released");
}
delay(20); // short delay to debounce button
}
return wasPressed;
}
int getDistance() { // in centimeters
float distance = 0.0;
// send trigger
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// calculate distance
unsigned long duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration * 0.0343) / 2;
// return distance as an integer
return (int)(distance + 0.5);
}
int getLight() {
int sensorVal = analogRead(LDR_PIN);
int value = map(sensorVal, 0, 1023, 100, 0);
return value;
}
void initLCD() {
lcd.setCursor(0, 0);
lcd.print("Room light : ");
lcd.setCursor(0, 1);
lcd.print("Manual mode: ");
lcd.setCursor(0, 2);
lcd.print("Distance : ");
lcd.setCursor(0, 3);
lcd.print("Light lvl: ");
}
void showStatus(bool buttonMode, int dist, int light) {
// Light control logic
if (dist < DIST_THRESHOLD && light < LIGHT_THRESHOLD || buttonMode) {
isLightOn = true;
digitalWrite(LED_PIN, HIGH);
} else {
isLightOn = false;
digitalWrite(LED_PIN, LOW);
}
// status displays
// if the room light status changed
if (isLightOn != oldLightOn) {
oldLightOn = isLightOn;
Serial.print("Room light: ");
Serial.println(isLightOn ? "ON" : "OFF");
}
lcd.setCursor(13, 0);
lcd.println(isLightOn ? "ON " : "OFF");
// if the button changed
if (buttonMode != oldBtnMode) {
oldBtnMode = buttonMode;
Serial.print("Manual mode: ");
Serial.println(buttonMode ? "On" : "Off");
lcd.setCursor(13, 1);
lcd.println(buttonMode ? "On " : "Off");
// if the distance changed (by more than 2 cm)
} else if ((dist) > (oldDistance + 2) || (dist) < (oldDistance - 2)) {
oldDistance = dist;
Serial.print("Distance: ");
Serial.print(dist);
Serial.println(" cm");
lcd.setCursor(13, 2);
lcd.print(dist);
lcd.print(" cm ");
// if the light value changed
} else if (light != oldLightVal) {
oldLightVal = light;
Serial.print("Light value: ");
Serial.println(light);
lcd.setCursor(13, 3);
lcd.print(light);
lcd.print(" % ");
}
}
void showSplash() {
lcd.setCursor(5, 0);
lcd.print("Smart Room");
lcd.setCursor(2, 1);
lcd.print("Light Controller");
lcd.setCursor(7, 3);
lcd.print("V1.00");
delay(2000);
lcd.clear();
Serial.println("Smart Room V1.00");
Serial.println(" Ready!\n");
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(LED_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
showSplash();
initLCD();
}
void loop() {
if (checkButton()) btnMode = !btnMode;
int distance = getDistance();
int lightVal = getLight();
showStatus(btnMode, distance, lightVal);
}