Issue with millis timing using leds

Hi guys, I have an issue with my code where my millis timing (the if statement) is supposedly being ignored, the leds light up to green but not after two seconds, it can happen at any time.

Another question is after the button is pressed I would like the system to delay two seconds before lighting up the green led, but I could not get this to happen, anyone know any ways to do it?

[code]

#define State1 1
#define ButtonWaiting 0

int state =0;
int red = 12;
int yellow = 11;
int green = 10;
int buttonPIN = 2;
int redPeda = 5;
int greenPeda = 4;
int counter = 0;
unsigned long previousTime;

bool oldButtonState;

void setup(){
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(buttonPIN, INPUT);
pinMode(redPeda, OUTPUT);
pinMode(greenPeda, OUTPUT);
digitalWrite(redPeda , HIGH);
digitalWrite(green, HIGH);
Serial.begin (9600);
Serial.println ("State 1 Button: Not Pressed Traffic Light: Green Pedestrian Light: Red");
previousTime = millis();

}

void loop()
{
bool buttonState = digitalRead(buttonPIN);

  switch (state)  {
  case ButtonWaiting:
   if (buttonState != oldButtonState && buttonState == HIGH)
   {
     Serial.println ("State 1  Button: Pressed  Traffic Light: Green  Pedestrian Light: Red");

     state = State1;
   }
   break;
  case State1:

   int currentTime= millis();
   int elapsedTime= currentTime-previousTime;
   if (elapsedTime>=2000);
   {
   digitalWrite(red, HIGH);
   digitalWrite(green, LOW);
   Serial.println ("State 2  Button: Not Pressed  Traffic Light: Green  Pedestrian Light: Red"); 
   
   previousTime = millis();
   }
   
   state = ButtonWaiting;
   break;
   
   }

oldButtonState = buttonState;

}

[code]

Note that the code is incomplete.

Hi there, I'm having an issue where my if (elapsedTime>=2000) statement is being supposedly ignored, the system lights up the green led no matter what time it is, all it needs is the button press.
Also, I wanted to have a 2second delay after the button had been pressed before the green led lights up but after a bit of time testing things, I couldn't figure it out.

Note that the code is incomplete.

#define State1 1
#define ButtonWaiting 0

int state =0;
int red = 12;
int yellow = 11;
int green = 10;
int buttonPIN = 2; 
int redPeda = 5;
int greenPeda = 4;
int counter = 0;
unsigned long previousTime;


bool oldButtonState;

void setup(){
    pinMode(red, OUTPUT);
    pinMode(yellow, OUTPUT);
    pinMode(green, OUTPUT);
    pinMode(buttonPIN, INPUT);
    pinMode(redPeda, OUTPUT);
    pinMode(greenPeda, OUTPUT);
    digitalWrite(redPeda , HIGH);
    digitalWrite(green, HIGH);
    Serial.begin (9600);
    Serial.println ("State 1  Button: Not Pressed  Traffic Light: Green  Pedestrian Light: Red");
    previousTime = millis();

}


  void loop()
  {
    bool buttonState = digitalRead(buttonPIN);
     
      switch (state)  {
      case ButtonWaiting:
       if (buttonState != oldButtonState && buttonState == HIGH)
       {
         Serial.println ("State 1  Button: Pressed  Traffic Light: Green  Pedestrian Light: Red");

         state = State1;
       }
       break;
      case State1:

       int currentTime= millis();
       int elapsedTime= currentTime-previousTime;
       if (elapsedTime>=2000);
       {
       digitalWrite(red, HIGH);
       digitalWrite(green, LOW);
       Serial.println ("State 2  Button: Not Pressed  Traffic Light: Green  Pedestrian Light: Red"); 
       
       previousTime = millis();
       }
       
       state = ButtonWaiting;
       break;
       
       }
  oldButtonState = buttonState;

  }

Also ignore my first post, I'm not able to delete it.

if (elapsedTime>=2000);

Loose the >>>>โ€”โ€”โ€”> ;

;


int currentTime= millis();
int elapsedTime= currentTime-previousTime;

unsigned long currentTime= millis();
unsigned long elapsedTime= currentTime-previousTime;

Thanks for the help Larry. Can I ask if you know a way to delay the green led lighting up until 2 seconds after the button has been pressed?
That is my intention with the if statement, currently it waits 2 seconds from the arduino starting in order to enter state 1 rather than 2 seconds after the button press.

Hello
Did you ever used the array instruction?
It is more easy to drop all led pins into an array and coded a second array containing the needed led state to the running time slot. That is a very little bit coding in OOP. But this makes it more easy to change the led sequence and/or adding more leds.

The above would need to be added to the first state case

I unfortunately have never used arrays so I don't think I will for this task, I do appreciate the suggestion though. I would've considered it if I knew how to use them.

Try the above.

I've placed it into the ButtonWait case but then the red LED never lights up. I think this means the if statement (not the one shown here) is never true. Not sure if I've done something wrong here.

 case ButtonWaiting:
       if (buttonState != oldButtonState && buttonState == HIGH)
       {
         
         Serial.println ("State 1  Button: Pressed  Traffic Light: Green  Pedestrian Light: Red");
         previousTime = millis();
         state = State1;
       }
       break;

Hello
Iยดll take a view into my sketch box to find a traffic light example.

When you make changes in the sketch, always repost the new version.


How is the switch wired ?

FYI

How are your LEDs wired ?

Oh I don't have a physical switch controlling the system. I use switch cases to control the system.
Here is the full updated code.

#define State1 1
#define ButtonWaiting 0

int state =0;
int red = 12;
int yellow = 11;
int green = 10;
int buttonPIN = 2; 
int redPeda = 5;
int greenPeda = 4;
int counter = 0;
unsigned long previousTime;


bool oldButtonState;

void setup(){
    pinMode(red, OUTPUT);
    pinMode(yellow, OUTPUT);
    pinMode(green, OUTPUT);
    pinMode(buttonPIN, INPUT);
    pinMode(redPeda, OUTPUT);
    pinMode(greenPeda, OUTPUT);
    digitalWrite(redPeda , HIGH);
    digitalWrite(green, HIGH);
    Serial.begin (9600);
    Serial.println ("State 1  Button: Not Pressed  Traffic Light: Green  Pedestrian Light: Red");
    previousTime = millis();
}


  void loop()
  {
    bool buttonState = digitalRead(buttonPIN);
     
      switch (state)  {
      case ButtonWaiting:
        
       if (buttonState != oldButtonState && buttonState == HIGH)
       { 
         Serial.println ("State 1  Button: Pressed  Traffic Light: Green  Pedestrian Light: Red");
         previousTime = millis();
         state = State1;
       }
       break;
      case State1:
       
       unsigned long currentTime= millis();
       unsigned long elapsedTime= currentTime-previousTime;
       if (elapsedTime>=2000)
       {
       digitalWrite(red, HIGH);
       digitalWrite(green, LOW);
       Serial.println ("State 2  Button: Not Pressed  Traffic Light: Green  Pedestrian Light: Red"); 
       
       previousTime = millis();
       }
       
       state = ButtonWaiting;
       break;
       
       }
  oldButtonState = buttonState;

  }

I just saw this message, here is an image of the wiring.

Your switch (button) on pin 2 needs to be wired as S3 in the schematic on post #11. (from pin 2 to the switch then to GND i.e. 0v)

You then will need to look for a LOW on buttonPIN to determine if it was pushed (closed).
When not pushed it will be HIGH.

Your will need to change this line:
pinMode(buttonPIN, INPUT);
to:
pinMode(buttonPIN, INPUT_PULLUP);

1 Like

Why is there a 10k resistor on the red LED ?

It's not for the red LED, its connected to the button.
Also regarding your last post, Is it not already wired to go to ground immediately?
Correct me if I'm wrong.
I attempted to pullup the resistor but that seemed to yield the same result.

Is the switch currently wired as S3 or S1 in the schematic on post #11 ?

Ah I see what you mean. I'll try wiring it up now.

I've tried to wire it up but now the button won't even be recognised when I press it.
I may have stuffed something up.
Here is the new updated code and an image of the schematic.

#define State1 1
#define ButtonWaiting 0

int state =0;
int red = 12;
int yellow = 11;
int green = 10;
int buttonPIN = 2; 
int redPeda = 5;
int greenPeda = 4;
int counter = 0;
unsigned long previousTime;


bool oldButtonState;

void setup(){
    pinMode(red, OUTPUT);
    pinMode(yellow, OUTPUT);
    pinMode(green, OUTPUT);
    pinMode(buttonPIN, INPUT_PULLUP);
    pinMode(redPeda, OUTPUT);
    pinMode(greenPeda, OUTPUT);
    digitalWrite(redPeda , HIGH);
    digitalWrite(green, HIGH);
    Serial.begin (9600);
    Serial.println ("State 1  Button: Not Pressed  Traffic Light: Green  Pedestrian Light: Red");
    previousTime = millis();
}


  void loop()
  {
    bool buttonState = digitalRead(buttonPIN);
     
      switch (state)  {
      case ButtonWaiting:
        
       if (buttonState != oldButtonState && buttonState == LOW)
       { 
         Serial.println ("State 1  Button: Pressed  Traffic Light: Green  Pedestrian Light: Red");
         previousTime = millis();
         state = State1;
       }
       break;
      case State1:
       
       unsigned long currentTime= millis();
       unsigned long elapsedTime= currentTime-previousTime;
       if (elapsedTime>=2000)
       {
       digitalWrite(red, HIGH);
       digitalWrite(green, LOW);
       Serial.println ("State 2  Button: Not Pressed  Traffic Light: Green  Pedestrian Light: Red"); 
       
       previousTime = millis();
       }
       
       state = ButtonWaiting;
       break;
       
       }
  oldButtonState = buttonState;

  }