/* * Module 3: The Light Trigger (Night Light) * Goal: Master analogRead() and the Serial Monitor */
const int LDR_PIN = A0; // Analog Pin
const int RED_LED = 9; // Red LED from previous modules
const int BUZZER_PIN = 8;
// Buzzer from previous modules // Threshold: If light drops below this, trigger alarm // (Adjust this value based on room brightness)
int threshold = 400;
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Initialize Serial communication at 9600 bits per second
Serial.begin(9600);
Serial.println(“Light Sensor Ready…”); }
void loop() {
// Read the value from the LDR (returns 0 to 1023)
int lightValue = analogRead(LDR_PIN);
// Print the value to the Serial Monitor
Serial.print(“Light Intensity: “);
Serial.println(lightValue);
// — LOGIC: Is it Dark? —
if (lightValue < threshold) { // IT IS DARK
digitalWrite(RED_LED, HIGH);
tone(BUZZER_PIN, 1000); // Constant beep
}
else {
// IT IS BRIGHT
digitalWrite(RED_LED, LOW);
noTone(BUZZER_PIN);
}
delay(100);
// Wait a bit so the Serial Monitor isn’t too fast
}