# Help for traffic/crosswalk light code using mills()

**URL:** <https://forum.arduino.cc/t/help-for-traffic-crosswalk-light-code-using-mills/1146028>\
**Category:** General Guidance\
**Created:** [July 8, 2023, 8:19pm UTC](https://forum.arduino.cc/t/help-for-traffic-crosswalk-light-code-using-mills/1146028 "2023-07-08T20:19:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![b1ond3e](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@b1ond3e](https://forum.arduino.cc/u/b1ond3e)\
**Post date:** [July 8, 2023, 8:19pm UTC](https://forum.arduino.cc/t/help-for-traffic-crosswalk-light-code-using-mills/1146028/1 "2023-07-08T20:19:44Z")

</div>

Hello, I am working on simulating 2 traffic and crosswalk light patterns at a 2 way intersection  
using the Sparkfun inventor's kit. This is my first time using millis() and am not sure if it is being used correctly or not. Especially the "if ((unsigned long)(currentMillis - previousMillis) \>= interval) {  
previousMillis = currentMillis;}" part. When I run the code on my Arduino/breadboard setup it only runs the code through once.  
Any help, tips or guidance would be much appreciated!

```arduino
#define LEDr1 12
#define LEDy1 11
#define LEDg1 10
#define LEDo1 9
#define LEDw1 8

#define LEDr2 7
#define LEDy2 6
#define LEDg2 5
#define LEDo2 4
#define LEDw2 3

int interval=25000; //25sec

unsigned long previousMillis=0;

void setup() {
  pinMode(LEDg1, OUTPUT);
  pinMode(LEDy1, OUTPUT);
  pinMode(LEDr1, OUTPUT);
  pinMode(LEDw1, OUTPUT);
  pinMode(LEDo1, OUTPUT);
  
  pinMode(LEDg2, OUTPUT);
  pinMode(LEDy2, OUTPUT);
  pinMode(LEDr2, OUTPUT);
  pinMode(LEDw2, OUTPUT);
  pinMode(LEDo2, OUTPUT);
  
  Serial.begin(9600);
}

void loop() {
unsigned long currentMillis = millis();
  Serial.println (millis());

if(millis() > 10){
  digitalWrite(LEDr1,HIGH); //red 1 on
  }
if(millis() > 10){
  digitalWrite(LEDw1,HIGH); //white 1 on
  }
if(millis() > 10){
  digitalWrite(LEDg2,HIGH); //green 2 on
  }
if(millis() > 10){
  digitalWrite(LEDo2,HIGH); //orange 2 on
  }
if(millis() > 8000){
  digitalWrite(LEDw1,LOW); // white 1 off
  }
if(millis() > 8000){
  digitalWrite(LEDo1,HIGH); //orange 1 blink start
  }
if(millis() > 8500){
  digitalWrite(LEDo1,LOW);
  }
if(millis() > 9000){
  digitalWrite(LEDo1,HIGH);
  }
if(millis() > 9000){
  digitalWrite(LEDg2,LOW); //green 2 off
  }
if(millis() > 9000){
  digitalWrite(LEDy2,HIGH); //yellow 2 on
  }
if(millis() > 9500){
  digitalWrite(LEDo1,LOW);
  }
if(millis() > 10000){
  digitalWrite(LEDo1,HIGH);
  }
if(millis() > 10500){
  digitalWrite(LEDo1,LOW);
  }
if(millis() > 11000){
  digitalWrite(LEDo1,HIGH);
  }
if(millis() > 11500){
  digitalWrite(LEDo1,LOW);
  }
if(millis() > 12000){
  digitalWrite(LEDo1,HIGH); //orange 1 blink stop; orange 1 on
  }
if(millis() > 13000){
  digitalWrite(LEDr1,LOW); //red 1 off
}
if(millis() > 13000){
  digitalWrite(LEDg1,HIGH); //green 1 on
  }
if(millis() > 13000){
  digitalWrite(LEDy2,LOW); //yellow 2 off
  }
if(millis() > 13000){
  digitalWrite(LEDr2,HIGH); //red 2 on
  }
if(millis() > 13000){
  digitalWrite(LEDo2,LOW); //orange 2 off
  }
if(millis() > 13000){
  digitalWrite(LEDw2,HIGH); //white 2 on
  }
if(millis() > 21000){
  digitalWrite(LEDw2,LOW); //white 2 off
  }
if(millis() > 21000){
  digitalWrite(LEDo2,HIGH); //orange 2 blink start
  }
if(millis() > 21500){
  digitalWrite(LEDo2,LOW);
  }
if(millis() > 22000){
  digitalWrite(LEDo2,HIGH);
  }
  if(millis() > 22500){
  digitalWrite(LEDo2,LOW);
  }
if(millis() > 23000){
  digitalWrite(LEDo2,HIGH);
  }
if(millis() > 23500){
  digitalWrite(LEDo2,LOW);
  }
if(millis() > 24000){
  digitalWrite(LEDo2,HIGH);
  }
if(millis() > 24500){
  digitalWrite(LEDo2,LOW);
  }
if(millis() > 25000){
  digitalWrite(LEDo1,HIGH); //orange 2 blink stop; orange 2 on
  }
if(millis() > 21000){
  digitalWrite(LEDg1,LOW); //green 1 off
  }
if(millis() > 21000){
  digitalWrite(LEDy1,HIGH); //yellow 1 on
  }
if(millis() > 25000){
  digitalWrite(LEDy1,LOW); //yellow 1 off
  }
if(millis() > 25000){
  digitalWrite(LEDo1,LOW); //orange 2 off
  }
if(millis() > 25000){
  digitalWrite(LEDr2,LOW); //red 2 off
  }

if ((unsigned long)(currentMillis - previousMillis) >= interval) {
  previousMillis = currentMillis;
}

}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 8, 2023, 8:24pm UTC](https://forum.arduino.cc/t/help-for-traffic-crosswalk-light-code-using-mills/1146028/2 "2023-07-08T20:24:49Z")

</div>

Welcome to the forum

A couple points

```arduino
if(millis() > 10){
  digitalWrite(LEDg2,HIGH); //green 2 on
  }
if(millis() > 10){
  digitalWrite(LEDo2,HIGH); //orange 2 on
  }

```

1. Why have you repeated the test ?

2. Once the millis() value is greater than 10 it will be 49 days until it is less than 10 again and then only for 10 milliseconds. Do you think that your code will work ?

I suggest that you take a look at [Using millis() for timing. A beginners guide](https://forum.arduino.cc/index.php?topic=503368.0), [Several things at the same time](https://forum.arduino.cc/index.php?topic=223286.0) and the BlinkWithoutDelay example in the IDE

---

<div class="post-metadata">

**Author:** ![b1ond3e](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@b1ond3e](https://forum.arduino.cc/u/b1ond3e)\
**Post date:** [July 8, 2023, 9:05pm UTC](https://forum.arduino.cc/t/help-for-traffic-crosswalk-light-code-using-mills/1146028/3 "2023-07-08T21:05:16Z")

</div>

I see, I do not need to repeat the test and can just put all the "digitalWrite"s in the single if statement. I hope to get this code to constantly loop right after the last line and not wait the 49 days for the millis to reset. I will take a look at the page linked and update here once I work it out. Thank you very much!

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [July 9, 2023, 2:34am UTC](https://forum.arduino.cc/t/help-for-traffic-crosswalk-light-code-using-mills/1146028/4 "2023-07-09T02:34:25Z")

</div>

Take a look at " [Non-blocking countdown sport timer implementation question](https://forum.arduino.cc/t/non-blocking-countdown-sport-timer-implementation-question/1146090/6)" on this forum, it has many similarities.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [July 9, 2023, 3:48am UTC](https://forum.arduino.cc/t/help-for-traffic-crosswalk-light-code-using-mills/1146028/5 "2023-07-09T03:48:22Z")

</div>

Hello [b1ond3e](https://forum.arduino.cc/u/b1ond3e)

In general - Arrays and structs are your friends.  
Don't duplicate code in your sketch. Write code once - use it multiple times.

Have a nice day and enjoy coding in C++.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [July 9, 2023, 12:05pm UTC](https://forum.arduino.cc/t/help-for-traffic-crosswalk-light-code-using-mills/1146028/6 "2023-07-09T12:05:23Z")

</div>

is it real project ? have you some photos?

```arduino
#define LEDr1 12
#define LEDy1 11
#define LEDg1 10
#define LEDs1 9 //stop
#define LEDw1 8 //walk

#define LEDr2 7
#define LEDy2 6
#define LEDg2 5
#define LEDs2 4
#define LEDw2 3
const byte pins[10] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3};
const unsigned int interval = 25000; //25sec

void setup() {
  Serial.begin(115200);
  for (byte i = 0; i < 10; i++)pinMode(pins[i], OUTPUT);
  for (byte i = 0; i < 10; i++)digitalWrite(pins[i], HIGH);
  delay(100);
  for (byte i = 0; i < 10; i++)digitalWrite(pins[i], LOW);
  delay(100);
}

void loop() {
  static unsigned int currentMS = 0;
  static unsigned int prevMS = 0;
  currentMS = unsigned int((millis() % interval) / 500)
  if (currentMS != prevMS) {
    Serial.println (millis());
    switch (currentMS) {
      case 0:
        digitalWrite(LEDr1, HIGH); //red 1 on
        digitalWrite(LEDw1, HIGH); //white 1 on
        digitalWrite(LEDg2, HIGH); //green 2 on
        digitalWrite(LEDs2, HIGH); //orange 2 on
        break;

      case 16:
        digitalWrite(LEDw1, LOW); // white 1 off
        digitalWrite(LEDs1, HIGH); //orange 1 blink start
        break;

      case 17:
        digitalWrite(LEDs1, LOW);
        break;

      case 18:
        digitalWrite(LEDs1, HIGH); //orange 1 on
        digitalWrite(LEDg2, LOW); //green 2 off
        digitalWrite(LEDy2, HIGH); //yellow 2 on
        break;

      case 19:
        digitalWrite(LEDs1, LOW);
        break;

      case 20:
        digitalWrite(LEDs1, HIGH);
        break;

      case 21:
        digitalWrite(LEDs1, LOW);
        break;

      case 22:
        digitalWrite(LEDs1, HIGH);
        break;

      case 23:
        digitalWrite(LEDs1, LOW);
        break;

      case 24:
        digitalWrite(LEDs1, HIGH); //orange 1 blink stop; orange 1 on
        break;

      case 26:
        digitalWrite(LEDr1, LOW); //red 1 off
        digitalWrite(LEDr2, HIGH); //red 2 on
        digitalWrite(LEDy2, LOW); //yellow 2 off
        digitalWrite(LEDg1, HIGH); //green 1 on
        digitalWrite(LEDs2, LOW); //orange 2 off
        digitalWrite(LEDw2, HIGH); //white 2 on
        break;

      case 42:
        digitalWrite(LEDw2, LOW); //white 2 off
        digitalWrite(LEDs2, HIGH); //orange 2 blink start
        digitalWrite(LEDg1, LOW); //green 1 off
        digitalWrite(LEDy1, HIGH); //yellow 1 on
        break;

      case 43:
        digitalWrite(LEDs2, LOW);
        break;

      case 44:
        digitalWrite(LEDs2, HIGH);
        break;

      case 45:
        digitalWrite(LEDs2, LOW);
        break;

      case 46:
        digitalWrite(LEDs2, HIGH);
        break;

      case 47:
        digitalWrite(LEDs2, LOW);
        break;

      case 48:
        digitalWrite(LEDs2, HIGH);
        break;

      case 49:
        digitalWrite(LEDs2, LOW);
        break;

      case 50:
        digitalWrite(LEDs1, HIGH); //orange 2 blink stop; orange 2 on
        digitalWrite(LEDy1, LOW); //yellow 1 off
        digitalWrite(LEDs1, LOW); //orange 2 off
        digitalWrite(LEDr2, LOW); //red 2 off
        break;
    }
    prevMS = currentMS;
  }
}
```

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [July 9, 2023, 1:36pm UTC](https://forum.arduino.cc/t/help-for-traffic-crosswalk-light-code-using-mills/1146028/7 "2023-07-09T13:36:10Z")

</div>

understanding how non-blocking timing based on millis() becomes much easier if two things are done at the beginning of the explanation:

1. explain that non-blocking timing works **totally different** then "just wait"
2. it is epxlained on an everyday example

Most tutorials don't do these two things. And this lack of very important introduction makes it hard **er** to understand than it could be.

Here is a tutorial that does exactly

1. explain that non-blocking timing works **totally different** then "just wait"
2. it is epxlained on an everyday example

> [@Example-code for timing based on millis() easier to understand through the use of example-numbers / avoiding delay()](https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017/1):
>
> non-blocking timing. Execute code only from time to time. There are a lot of different ways to learn programming. This thread wants to add another approach that is different to the yet existing ones. UPDATE 06.01.2023 if you are mainly interested in applying non-blocking timing you can do a quick read of this short tutorial / demonstration If you are intersted in understanding the details how it works go on reading here. I use an everyday analogon in this post to explain the basic principle …

best regards Stefan

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 5, 2024, 1:36pm UTC](https://forum.arduino.cc/t/help-for-traffic-crosswalk-light-code-using-mills/1146028/8 "2024-01-05T13:36:56Z")

</div>

Closed on January 5, 2024, 1:36pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
