#include <Arduino.h>
// Pin definitions (matching the image)
const int MOTION_SENSOR_PIN = 32; // D32
const int LIGHT_SENSOR_PIN = 34; // D34
const int DAY_LED_PIN = 12; // D12
const int NIGHT_LED_PIN = 13; // D13
// Thresholds
const int LIGHT_THRESHOLD = 500; // Adjust based on your light sensor readings
const int MOTION_TIMEOUT = 5000; // 5 seconds (adjust as needed)
// Variables
bool motionDetected = false;
unsigned long motionStartTime = 0;
void setup() {
Serial.begin(115200);
pinMode(MOTION_SENSOR_PIN, INPUT);
pinMode(DAY_LED_PIN, OUTPUT);
pinMode(NIGHT_LED_PIN, OUTPUT);
}
void loop() {
// Read sensor values
int lightValue = analogRead(LIGHT_SENSOR_PIN);
int motionValue = digitalRead(MOTION_SENSOR_PIN);
// Check for motion
if (motionValue == HIGH) {
motionDetected = true;
motionStartTime = millis();
}
// Check if motion timeout has expired
if (motionDetected && (millis() - motionStartTime > MOTION_TIMEOUT)) {
motionDetected = false;
}
// Determine day or night
bool isDay = (lightValue > LIGHT_THRESHOLD);
// Control LEDs
if (isDay) {
digitalWrite(DAY_LED_PIN, LOW); // Day LED off
digitalWrite(NIGHT_LED_PIN, LOW); // Night LED off
} else {
digitalWrite(DAY_LED_PIN, LOW); // Day LED off
if (motionDetected) {
// Full brightness when motion detected at night
analogWrite(NIGHT_LED_PIN, 255);
} else {
// 30% brightness when no motion at night
analogWrite(NIGHT_LED_PIN, 76); // 30% of 255
}
}
delay(50); // Small delay to prevent rapid readings
}