Not sure how to implement mills() in 7-segment program

I am trying to implement mills() into my program in place of delay so that I can add in part of my program which is a traffic light. This is the counter which will be counting down to the light changing. I understand what the millis() function is, however i've been having difficulties trying to figure out how to properly use it in a program. Any help would be appreciated bellow is my code. Also i'm trying to figure out how to get both displays working together where the one will act as the ten's changing every 10 seconds and the other will function as the ones counting every second. This is my first project and it has been a difficult learning experience but a learning experience none the less :slight_smile:

//           ---A---
//          |       |
//          F       B
//          |       |
//           ---G---
//          |       |
//          E       C
//          |       |
//           ---D---
//
//        A = pin 22 & 47
//        B = pin 23 & 48
//        C = pin 24 & 49
//        D = pin 25 & 50
//        E = pin 26 & 51
//        F = pin 27 & 52
//        G = pin 28 & 53

byte seven_seg_digits[10][7] = { { 1,1,1,1,1,1,0 },  // = 0
                                                           { 0,1,1,0,0,0,0 },  // = 1
                                                           { 1,1,0,1,1,0,1 },  // = 2
                                                           { 1,1,1,1,0,0,1 },  // = 3
                                                           { 0,1,1,0,0,1,1 },  // = 4
                                                           { 1,0,1,1,0,1,1 },  // = 5
                                                           { 1,0,1,1,1,1,1 },  // = 6
                                                           { 1,1,1,0,0,1,0 },  // = 7
                                                           { 1,1,1,1,1,1,1 },  // = 8
                                                           { 1,1,1,1,0,1,1 }   // = 9
                                                           };

unsigned long previousMillis = 0;                          // sets previousMillis to 0
const long interval = 1000;                                // sets interval to 1000 or 1 second

void setup() { 
  // Tens Seven Segment Display
  pinMode(22,    OUTPUT); // A
  pinMode(23,    OUTPUT); // B
  pinMode(24,    OUTPUT); // C
  pinMode(25,    OUTPUT); // D
  pinMode(26,    OUTPUT); // E
  pinMode(27,    OUTPUT); // F
  pinMode(28,    OUTPUT); // G
  
  // Ones Seven Segment Display
  pinMode(47,    OUTPUT); // A
  pinMode(48,    OUTPUT); // B
  pinMode(49,    OUTPUT); // C
  pinMode(50,    OUTPUT); // D
  pinMode(51,    OUTPUT); // E
  pinMode(52,    OUTPUT); // F
  pinMode(53,    OUTPUT); // G
}
    
void sevenSegWrite1(byte digit) {                          // creates 7-segment variable
  byte pin = 47;                                           // start from pin 47 for the ones 7-segment
  for (byte segCount = 0; segCount < 7; ++segCount) {      // set count to 0, count <7, +count 
    digitalWrite(pin, seven_seg_digits[digit][segCount]);  // display on defined pin, the digit in the array based on position of count then move to next pin
    ++pin;
  }
}

void pause(long interval) {                                // attempt at defining variable millis instead of having to use delay
  previousMillis = millis();                               // previousMillis is millis
  if(millis() - previousMillis < 1000){                    // if millis - previousMillis is less than 1000
  }
}

void loop() {
  unsigned long currentMillis = millis();
  
  if(currentMillis - previousMillis >= interval) {  // if currentMillis - previousMillis is greater than or equal to interval of 1000
    previousMillis = currentMillis;                 // previousMillis = currentMillis increasing currentMillis to keep up with millis
     for(byte count = 10; count > 0; --count) {     // Counts down from 9 to 0
       delay(1000);                                 // Pauses every second to count down
       sevenSegWrite1(count - 1);                   // Displays the current number in the sequence on the 7-segment display
    
}
}
}

Silentbob417:
I am trying to implement mills() into my program in place of delay so that I can add in part of my program which is a traffic light. This is the counter which will be counting down to the light changing. I understand what the millis() function is, however i've been having difficulties trying to figure out how to properly use it in a program. Any help would be appreciated bellow is my code. Also i'm trying to figure out how to get both displays working together where the one will act as the ten's changing every 10 seconds and the other will function as the ones counting every second. This is my first project and it has been a difficult learning experience but a learning experience none the less :slight_smile:

void pause(long interval) {                                
// attempt at defining variable millis instead of having to use delay
  previousMillis = millis();          // previousMillis is millis
  if(millis() - previousMillis < 1000){   
  // if millis - previousMillis is less than 1000
  }
}

That will return immediately as you've just set previousMillis=millis() and millis() - previousMillis will always be less than 1000. while(millis() - previousMillis <1000); will work, but is 'blocking code' like delay() is.

void loop() {
  unsigned long currentMillis = millis();
  ////why are you defining currentMillis within loop()? 
  ////It should be defined at the start of your program.
  ////then just use currentMillis = millis(); here
  if(currentMillis - previousMillis >= interval) {  
// if currentMillis - previousMillis is greater than or
// equal to interval of 1000
////As you've given previousMillis a value of 0, this will
//// always be true the first time around the loop.
////You should define previousMillis =millis() before setup()
      previousMillis = currentMillis;               
// previousMillis = currentMillis increasing currentMillis 
// to keep up with millis
////   Define  count  = 9;  before setup()
     sevenSegWrite1(count); 
      // Displays the current number in the sequence on the 7-segment display
     count--;
     if (count<0) count = 9; //for continuous countdown
       }   
   } 
}

Watch your line length! Having to scroll across nearly 2 page widths to read your comments makes it very difficult to read your code.

Having a function called pause() that uses millis() is no different from using delay().

The concept of the "blink without delay" example sketch is very different.

I have written a demo sketch that shows how the blink without delay concept can be used to control several things. You may find it useful. Demonstration code for several things at the same time - Project Guidance - Arduino Forum

...R