Question: this is what i did id my set up wrong? or is the code wrong? Program a system with three lights. The user presses start

this is what i did id my set up wrong? or is the code wrong?
Program a system with three lights. The user presses start and the first light comes on and stays on, ten seconds later the second light comes on and stays on, ten seconds later the third light comes on and stays on. After 10 more seconds with all lights on, everything shuts off. If the user presses stop at any time, everything shuts off.
const int buttonPin =2; // Push button pin
const int led1=8; // LED1 pin
const int led2=9; // LED2 pin
const int led3=10; // LED3 pin
bool isRunning = false; // To track if the sequence is active
bool buttonPressed = false; // To check if the button was pressed
void setup(){
pinMode(buttonPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop(){
if (digitalRead(buttonPin)== HIGH && !buttonPressed){
buttonPressed = true;
isRunning =!isRunning; // Toggle isRunning on each button press
if (isRunning){
startSequence();
} else {
stopSequence();
}
}
// Reset buttonPressed when button is released
if (digitalRead(buttonPin)== LOW){
buttonPressed = false;
}
}
void startSequence(){
digitalWrite(led1, HIGH);
delay(10000); // Wait for 10 seconds
if (!isRunning) return; // Check if we should stop
digitalWrite(led2, HIGH);
delay(10000); // Wait for 10 seconds
if (!isRunning) return; // Check if we should stop
digitalWrite(led3, HIGH);
delay(10000); // Wait for 10 seconds
if (!isRunning) return; // Check if we should stop
delay(10000); // Keep all LEDs on for 10 seconds
stopSequence(); // Turn off all LEDs after the full sequence
}
void stopSequence(){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
isRunning = false; // Reset running state
}
this is what i did id my set up wrong? or is the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!