I hope I uploaded the code correctly, If not my appologies....
I want the green led to be on, unless the IR detector detects something. Once the IR is triggered, the green led should go out and after a second the red led should come on for 5 seconds. after 1 second, the yellow led should come on and stay for 5 seconds, then after a 1 second delay, the yellow led should go off and the green led come back on. This should run in a loop.
I have included my code below. The red and yellow leds lights as they should and cycle through. However the green led ONLY comes on when the cycle goes through and stays lit when the IR is triggered.
Any suggestions as to how to fix the code? Thanks in advance for your help.
// Define pin numbers
const int greenLEDPin = 2;
const int yellowLEDPin = 3;
const int redLEDPin = 4;
const int irSensorPin = 5;
void setup() {
// Initialize the LED pins as outputs
pinMode(greenLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
// Initialize the IR sensor pin as input
pinMode(irSensorPin, INPUT);
// Start with the green LED on
digitalWrite(greenLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
}
void loop() {
// Check if the IR sensor is triggered
if (digitalRead(irSensorPin) == HIGH) {
// Turn off the green LED
digitalWrite(greenLEDPin, LOW);
delay(500); // Wait for 0.5 seconds
// Turn on the red LED
digitalWrite(redLEDPin, HIGH);
delay(5000); // Wait for 5 seconds
// Turn off the red LED
digitalWrite(redLEDPin, LOW);
delay(500); // Wait for 0.5 seconds
// Turn on the yellow LED
digitalWrite(yellowLEDPin, HIGH);
delay(5000); // Wait for 5 seconds
// Turn off the yellow LED
digitalWrite(yellowLEDPin, LOW);
delay(500); // Wait for 0.5 seconds
// Turn on the green LED
digitalWrite(greenLEDPin, HIGH);
}
}
````Use code tags to format code for the forum`

