#define ldr A0 // Define the pin for the Light Dependent Resistor (LDR)
#define led 7 // Define the pin for the LED
#define buzzer 9 // Define the pin for the passive buzzer
void setup() {
pinMode(ldr, INPUT); // Set the LDR pin as input
pinMode(led, OUTPUT); // Set the LED pin as output
pinMode(buzzer, OUTPUT); // Set the buzzer pin as output
Serial.begin(9600); // Start the serial communication at 9600 baud
}
void loop() {
int valor = analogRead(ldr); // Read the value from the LDR
int luz = map(valor, 0, 1023, 100, 0); // Map the value to a percentage (0-100%)
Serial.print("Illumination at: ");
Serial.print(luz);
Serial.println("%");
// If light level exceeds 100 lux, trigger alarm
if (valor < 923) { // Check if the analog value corresponds to more than 100 lux
digitalWrite(led, HIGH); // Turn on the LED
tone(buzzer, 1000); // Generate a 1000 Hz tone on the buzzer
} else {
digitalWrite(led, LOW); // Turn off the LED
noTone(buzzer); // Stop the tone on the buzzer
}
delay(1000); // Delay for 1 second before the next reading
}