int irSensorPin = A0; // Set the IR sensor pin to analog pin A0
int buzzerPin = 7; // Set the buzzer pin to digital pin 7
int greenLEDPin = 8; // Set the green LED pin to digital pin 8
void setup() {
pinMode(irSensorPin, INPUT); // Set the IR sensor pin to input
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin to output
pinMode(greenLEDPin, OUTPUT); // Set the green LED pin to output
Serial.begin(9600); // Initialize the serial communication
}
void loop() {
int distance = analogRead(irSensorPin); // Read the distance value from the IR sensor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < 30) { // If someone comes in range (less than 30cm away)
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
digitalWrite(greenLEDPin, HIGH); // Turn on the green LED
delay(500); // Wait for 500ms
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
digitalWrite(greenLEDPin, LOW); // Turn off the green LED
}
delay(4000); // Wait for 100ms before taking another reading
}