const int trigPin = 9; // Trigger pin of the ultrasonic sensor
const int echoPin = 10; // Echo pin of the ultrasonic sensor
const int ldrPin = A0; // LDR pin
const int ledPin = 11; // LED pin for the street light
int threshold = 500; // Adjust this threshold based on your LDR sensitivity
int dimIntensity = 100; // Adjust this value for the initial dim intensity
int brightIntensity = 255; // Adjust this value for the maximum brightness
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
// Turn off the street light initially
analogWrite(ledPin, 0);
}
void loop() {
// Read LDR value to determine day or night
int ldrValue = analogRead(ldrPin);
// If it's night, check for motion using the ultrasonic sensor
if (ldrValue < threshold) {
detectMotion();
} else {
// It's day, turn off the street light
analogWrite(ledPin, 0);
}
delay(500); // Adjust the delay time based on your requirements
}
void detectMotion() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check if someone or something is passing by
if (distance < 100) { // Adjust this distance threshold based on your needs
// Motion detected, increase brightness
analogWrite(ledPin, brightIntensity);
delay(1000); // Keep the light on for 1 second (adjust as needed)
// After detecting motion, return to dim intensity
}
analogWrite(ledPin, dimIntensity);
}