// Pins for LEDs
const int ledPins[] = {3, 4, 5, 6, 7};
// Pins for IR sensors
const int bottomIRPin = 9; // Bottom sensor
const int topIRPin = 8; // Top sensor
// Delay between LED transitions
const int transitionDelay = 500; // milliseconds
void setup() {
// Initialize LED pins as outputs
for (int i = 0; i < sizeof(ledPins) / sizeof(ledPins[0]); i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize IR sensor pins as inputs
pinMode(bottomIRPin, INPUT);
pinMode(topIRPin, INPUT);
}
void loop() {
// Check if obstacle is detected by the bottom IR sensor
if (detectObstacle(bottomIRPin)) {
// Start the LED transition from bottom to top
lightUpLEDsBottomToTop();
delay(100); // Wait for 2 seconds
turnOffLEDs();
// Wait for IR sensor to reset
while (digitalRead(bottomIRPin) == HIGH) {}
}
// Check if obstacle is detected by the top IR sensor
if (detectObstacle(topIRPin)) {
// Start the LED transition from top to bottom
lightUpLEDsTopToBottom();
delay(100); // Wait for 2 seconds
turnOffLEDs();
// Wait for IR sensor to reset
while (digitalRead(topIRPin) == HIGH) {}
}
}
// Function to detect obstacles using IR sensor
bool detectObstacle(int irPin) {
// Check if IR sensor input is HIGH (indicating obstacle)
return digitalRead(irPin) == HIGH;
}
// Function to light up LEDs one by one from bottom to top
void lightUpLEDsBottomToTop() {
for (int i = 0; i < sizeof(ledPins) / sizeof(ledPins[0]); i++) {
digitalWrite(ledPins[i], HIGH);
delay(transitionDelay);
}
}
// Function to light up LEDs one by one from top to bottom
void lightUpLEDsTopToBottom() {
for (int i = sizeof(ledPins) / sizeof(ledPins[0]) - 1; i >= 0; i--) {
digitalWrite(ledPins[i], HIGH);
delay(transitionDelay);
}
}
// Function to turn off LEDs in reverse order
void turnOffLEDs() {
for (int i = sizeof(ledPins) / sizeof(ledPins[0]) - 1; i >= 0; i--) {
digitalWrite(ledPins[i], LOW);
delay(transitionDelay); // Optional delay for better visual effect
}
}