// Pin configuration
const int ldrPin = 13; // Pin for LDR
const int ledPins[] = {32, 33, 25, 26, 27}; // Pins for 5 LEDs
const int buttonIncrease = 19; // Pin for push button 1 (increase threshold)
const int buttonDecrease = 18; // Pin for push button 2 (decrease threshold)
int ledIndex = 0; // Index for current LED
int lightThreshold = 800; // Initial light threshold
void setup() {
Serial.begin(115200);
// Initialize LED pins as output
for (int i = 0; i < 5; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Start with all LEDs off
}
// Initialize LDR pin as input
pinMode(ldrPin, INPUT);
// Initialize push button pins as input without pull-up
pinMode(buttonIncrease, INPUT); // Button for increasing threshold
pinMode(buttonDecrease, INPUT); // Button for decreasing threshold
}
void loop() {
int ldrValue = analogRead(ldrPin); // Read LDR value
// Check push buttons
adjustThreshold();
Serial.print("LDR Value: ");
Serial.println(ldrValue); // For debugging, see the LDR value in Serial Monitor
Serial.print("Current Threshold: ");
Serial.println(lightThreshold);
// Check if the LDR detects light
if (ldrValue > lightThreshold && ledIndex < 5) {
digitalWrite(ledPins[ledIndex], HIGH); // Turn on the current LED
ledIndex++; // Move to the next LED
delay(300); // Small delay to debounce the light detection
}
// If all 5 LEDs are on, start blinking process
if (ledIndex == 5) {
for (int i = 0; i < 5; i++) {
blinkAllLeds(); // Blink all LEDs 5 times
}
// Reset LEDs and index after blinking
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], LOW);
}
ledIndex = 0; // Reset index for the next cycle
}
}
// Function to blink all LEDs 1 time
void blinkAllLeds() {
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], LOW);
}
delay(200);
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], HIGH);
}
delay(200);
}
// Function to adjust the threshold using buttons
void adjustThreshold() {
// Check if the increase button is pressed
if (digitalRead(buttonIncrease) == 1) { // HIGH indicates button press
lightThreshold += 100; // Increase threshold
delay(300); // Debounce delay
}
// Check if the decrease button is pressed
if (digitalRead(buttonDecrease) == 1) { // HIGH indicates button press
lightThreshold -= 100; // Decrease threshold
if (lightThreshold < 0) {
lightThreshold = 0; // Prevent threshold from going negative
}
delay(300); // Debounce delay
}
}