// Pin assignments
const int trigPin = 16;
const int echoPin = 17;
const int ledPin = 5;
const int ldrPin = 34;
long duration;
int distance;
int lightValue;
int invertVal;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // For debugging
}
void loop() {
// --- Ultrasonic sensor measurement ---
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Convert to cm
// --- Photoresistor measurement ---
lightValue = analogRead(ldrPin);
invertVal = map(lightValue, 32, 4063, 4063, 32); // LDR in Wokwi give inverted value so I inverted it back to be same as slider of LDR
// Debugging output
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm | Light: ");
Serial.println(invertVal);
// --- Logic: LED ON only if motion detected AND low light ---
// Motion detected if object < 20 cm
// Low light if photoresistor value < 300 (adjust threshold as needed)
if (distance > 0 && distance < 50 && invertVal < 800) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(100);
}