#include <Ultrasonic.h>
const int LDRPin = A0; // LDR sensor connected to analog pin A0
const int trigPin = 7; // Ultrasonic sensor trig pin connected to digital pin 7
const int echoPin = 6; // Ultrasonic sensor echo pin connected to digital pin 6
const int buzzerPin = 9; // Buzzer connected to digital pin 9
const int ledPin = 11; // LED connected to digital pin 11
int ldrThreshold = 500; // Threshold value for LDR sensor
int distanceThreshold = 50; // Threshold distance for Ultrasonic sensor
int buzzerDuration = 500; // Duration for buzzer activation (in milliseconds)
int ledDuration = 500; // Duration for LED activation (in milliseconds)
int ldrValue = 0; // variable to store LDR sensor value
int distance = 0; // variable to store ultrasonic sensor distance value
Ultrasonic ultrasonic(trigPin, echoPin); // create ultrasonic sensor object
void setup() {
pinMode(LDRPin, INPUT); // set LDR pin as input
pinMode(trigPin, OUTPUT); // set ultrasonic sensor trig pin as output
pinMode(echoPin, INPUT); // set ultrasonic sensor echo pin as input
pinMode(buzzerPin, OUTPUT); // set buzzer pin as output
pinMode(ledPin, OUTPUT); // set LED pin as output
Serial.begin(9600); // start serial communication
}
void loop() {
ldrValue = analogRead(LDRPin); // read LDR sensor value
distance = ultrasonic.read(); // read ultrasonic sensor value
if (ldrValue < ldrThreshold && distance < distanceThreshold) { // if both sensors detect an object
Serial.println("Object detected!"); // print message to serial monitor
digitalWrite(buzzerPin, HIGH); // turn on the buzzer
digitalWrite(ledPin, HIGH); // turn on the LED
delay(buzzerDuration); // wait for the specified duration
digitalWrite(buzzerPin, LOW); // turn off the buzzer
digitalWrite(ledPin, LOW); // turn off the LED
delay(ledDuration); // wait for the specified duration
}
}
void loop() {
// put your main code here, to run repeatedly:
}