1

Basically, how do I modify my code listed below to turn ON each of the three LEDs one at a time in a repetitive sequence while keeping the push-button pressed?

  • Red (3 sec),
  • Yellow (1 sec), and
  • Green (3 sec).

I'm trying to get all three LEDs to turn OFF when the push button is released.

The code I wrote that doesn't allow me to achieve what I'm trying to do (I'm not sure where I went wrong):

#define Button_Pin 2  //Push Button Pin
#define LED_Pin_1 11 //Red LED
#define LED_Pin_2 10 //Yellow LED
#define LED_Pin_3 9  //Green LED

byte Button_State;

void setup() {
  pinMode(Button_Pin, INPUT);
  pinMode(LED_Pin_1,  OUTPUT);
  pinMode(LED_Pin_2,  OUTPUT);
  pinMode(LED_Pin_3,  OUTPUT);
  
  
  Serial.begin(9600); 
  Serial.println("Design Part B - Digital I-O Control");
  delay(1000); 
}

void loop() {
  Button_State = digitalRead(Button_Pin);
  Serial.print("Button State: ");
  Serial.print(Button_State);
  
  
  if (Button_State == 1){
    digitalWrite(LED_Pin_1, HIGH); 
    delay(3000); //Wait for 3 Secs
    digitalWrite(LED_Pin_2, HIGH); 
    delay(1000); //Waits for 1 Sec
    digitalWrite(LED_Pin_3, HIGH); 
    delay(3000); 
    Serial.println(" --> Button Pressed --> LED ON");
  }
  else{
    digitalWrite(LED_Pin_1, LOW); 
    digitalWrite(LED_Pin_2, LOW); 
    digitalWrite(LED_Pin_3, LOW); 
    Serial.println(" Button is Released --> LED OFF");
  }
}

The circuit layouts being used

6
  • 2
    look carefully at the code inside the if (Button_State == 1){ block .... think about what it does and how long it takes to do it ... then examine the blinkWithoutDelay example sketch in the Arfuino IDE
    – jsotola
    Commented Jun 4, 2023 at 6:27
  • When you turn yellow on, should red be turned off? Or does it stay on?
    – Nick Gammon
    Commented Jun 4, 2023 at 7:12
  • I guess that the button pin is wired with an external pull down resistor say 10k.
    – 6v6gt
    Commented Jun 4, 2023 at 10:57
  • I've attached what the circuit looks like if that helps clear up anything. I will examine the example sketch you suggested and think more carefully about what that block does and so forth
    – Mars
    Commented Jun 4, 2023 at 21:54
  • 1
    The clue here is that once your LED sequence has started, it must run to completion - 7 seconds worth - before the button can be detected. Even if you hold the button for, say 3 seconds, starting shortly after the sequence starts, you may give up and release it before the sequence completes, and the code will never have tested it.
    – JRobert
    Commented Jun 4, 2023 at 22:35

2 Answers 2

1

You need a state machine (to remember which LED is currently on) and use millis() to find the current time, in a loop, which is testing whether the switch is closed or not.

If the switch is closed you compare the time now (with millis()) to the time you turned that particular LED on. When the time is up you advance the "state" (eg. a counter) to the next one, and remember the new time that this state change happened.

0

Having examined several sample sketches, including the recommended one, and watching instructional YouTube videos to grasp the fundamentals of Arduino programming, I am delighted to announce that my understanding of programming the Arduino and comprehending its functions has significantly improved, considering my novice status.

I can confidently assert that this question has been resolved, and I have enclosed the code that fulfills the initial requirements that was asked of me.

#define pushButton_pin 2
#define grn_LED_pin 9
#define yel_LED_pin 10
#define red_LED_pin 11

bool pushButton_state;

void setup() {
pinMode(pushButton_pin, INPUT);
pinMode(grn_LED_pin, OUTPUT);
pinMode(yel_LED_pin, OUTPUT);
pinMode(red_LED_pin, OUTPUT);

Serial.begin(115200);

Serial.println("Use push button (digital input) to control 3 LEDs (digital 
output)");
delay(1000); 
}

void loop() {
pushButton_state = digitalRead(pushButton_pin); 
Serial.println(pushButton_state); 
if (pushButton_state == 1) { 
digitalWrite(grn_LED_pin, LOW); 
digitalWrite(yel_LED_pin, LOW); 
digitalWrite(red_LED_pin, HIGH); 
Serial.println(" :push button state is 1, red LED is ON"); 
delay(2000);
digitalWrite(grn_LED_pin, LOW); 
digitalWrite(yel_LED_pin, HIGH); 
digitalWrite(red_LED_pin, LOW); 
Serial.println(" :push button state is 1, yel LED is ON"); 
delay(1000); 
digitalWrite(grn_LED_pin, HIGH); 
digitalWrite(yel_LED_pin, LOW); 
digitalWrite(red_LED_pin, LOW); 
Serial.println(" :push button state is 1, grn LED is ON");
delay(2000); 
} else {
digitalWrite(grn_LED_pin, LOW); 
digitalWrite(yel_LED_pin, LOW); 
digitalWrite(red_LED_pin, LOW); 
Serial.println(" :push button state is 0, all LEDs OFF"); 
}
delay(500);
}
2
  • 1
    But it appears that releasing the button has no immediate effect. It must still wait until the full cycle has completed before all leds are switched off. To improve this, you must use a "state machine" type solution as has already been suggested. Using delay() statements is very inflexible. Everything is blocked until these terminate.
    – 6v6gt
    Commented Jun 5, 2023 at 9:18
  • I stated that I wanted to get all three LEDs to turn OFF when the push button is released. I should've been clearer about exactly what I wanted to achieve so apologies for the confusion there, however I still wanted it to still run the full cycle when pressed before turning off.
    – Mars
    Commented Jun 5, 2023 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.