/*
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)
*/
// 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 = false;
bool isLightOn = false;
bool oldLightOn = false;
int oldDistance = 0;
int oldLightVal = 0;
int oldBtnState = HIGH; // pin idles HIGH
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 showStatus(bool button, int dist, int light) {
// if the button changed
if (button != oldBtnMode) {
oldBtnMode = button;
Serial.print("Manual mode: ");
Serial.println(button ? "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");
// if the light value changed
} else if (light != oldLightVal) {
oldLightVal = light;
Serial.print("Light value: ");
Serial.println(light);
}
// Light control logic
if (dist < DIST_THRESHOLD && light < LIGHT_THRESHOLD || btnMode) {
isLightOn = true;
digitalWrite(LED_PIN, HIGH);
} else {
isLightOn = false;
digitalWrite(LED_PIN, LOW);
}
// if the room light status changed
if (isLightOn != oldLightOn) {
oldLightOn = isLightOn;
Serial.print("Room light: ");
Serial.println(isLightOn ? "ON" : "OFF");
}
}
void showSplash() {
Serial.println("Smart Room V1.00");
Serial.println(" Ready!\n");
}
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
showSplash();
}
void loop() {
bool btnPress = checkButton();
if (btnPress) {
btnMode = !btnMode;
}
int distance = getDistance();
int lightVal = getLight();
showStatus(btnMode, distance, lightVal);
}