I need to replace delay() for millis()

const int pins [7] = {2,3,4,5,6,7,8}; //declaro los pines que utilizare

//declaro un array de bytes con la configuracion de cada uno de los numeros que se desplagaran 

const byte numerosDisplay[10] = {0b0111111, //0
                                 0b0000110,//1
                                 0b1011011,//2
                                 0b1001111,//3
                                 0b1100110,//4
                                 0b1101101,//5
                                 0b1111101,//6
                                 0b0000111,//7
                                 0b1111111,//8
                                 0b1101111};//9

void setup () {
  for(int i = 0; i < 7; i++) {
    pinMode(pins[i], OUTPUT); //declaramos pines como salidas
  }
  encendido (0);//llamamos a la funcion de encendido 
}
void loop () {
  for(int i = 0; i < 10; i ++) {
    encendido(i);//llamamos a la funcion encendido con el valor de i
    delay(1000);
  }
}
void  encendido (int numeros) {//funcion que enciende los leds del display
 //esto nos permite leer un byte en un posicion especifica
  byte numeroBit = numerosDisplay [numeros];
  for (int i = 0; i < 7; i++) {//esto nos permite recorrer las 7 posiciones para leer cada bit de los bytes
    int bit = bitRead (numeroBit, i); //que representan nuestros numeros
     digitalWrite(pins[i], bit);
     
  }
}

Good job properly using code tags in your first post!, But you haven't asked a question.

We won't simply write the code for you. You have to try it yourself then post your efforts here and then we can help you understand where you've gone wrong.

Here are a couple links to good tutorials to get you started:

Have a look at the Blink Without Delay example sketch.

The basic principle is...

  • capture startTime using millis()
  • loop
    {
    capture the currentTime using millis()
    is currentTime - startTime > delayDuartion
    If Yes, do something
    }

i tried with this but doesnt work hahaha. it is a seven segment display, counter (0 to 9)

unsigned long tiempoAnterior = 0;
const int pins [7] = {2,3,4,5,6,7,8}; //declaro los pines que utilizare

//declaro un array de bytes con la configuracion de cada uno de los numeros que se desplagaran 

const byte numerosDisplay[10] = {0b0111111, //0
                                 0b0000110,//1
                                 0b1011011,//2
                                 0b1001111,//3
                                 0b1100110,//4
                                 0b1101101,//5
                                 0b1111101,//6
                                 0b0000111,//7
                                 0b1111111,//8
                                 0b1101111};//9

void setup () {
  tiempoAnterior = millis();
  for(int i = 0; i < 7; i++) {
    pinMode(pins[i], OUTPUT); //declaramos pines como salidas
  }
  encendido (0);//llamamos a la funcion de encendido 
}
void loop () {

    for(int i = 0; i < 10; i ++) {
    encendido(i);//llamamos a la funcion encendido con el valor de i
    
          if( (millis() - tiempoAnterior) >= 1000){
      tiempoAnterior = millis();
    }
    
    }
}

void  encendido (int numeros) {//funcion que enciende los leds del display
 //esto nos permite leer un byte en un posicion especifica
  byte numeroBit = numerosDisplay [numeros];
  for (int i = 0; i < 7; i++) {//esto nos permite recorrer las 7 posiciones para leer cada bit de los bytes
    int bit = bitRead (numeroBit, i); //que representan nuestros numeros
     digitalWrite(pins[i], bit);
     
  }
}

You can get rid of the for loop... just use the main loop, and add 1 to i each time the duration is reached (max 9)

You should only call encendido() when the duration has passed.

what exactly do you mean it doesnt work? please be specific

nothing jumps out to me a "wrong" in your code, though you're not actual doing anything when time its read for an update (other than advancing the clock). I think you meant to have
encendido to execute after your timing if statement.

my general strategy is conceptually similar to what you're trying, just a little more refined:

uint32_t nextUpdateTime;
uint16_t updatePeriod;


void main
{
     ///do what ya gotta do here

   if(millis() >= nextUpdateTime)
   {
    nextUpdateTime+=updatePeriod;   //set the next time this update should run
   ///do your update here
    }
}


You almost hit, try this loop

void loop () {
  for (int i = 0; i < 10; i ++) {
    //(((encendido(i);//llamamos a la funcion encendido con el valor de i))) AQUI NO
    if ( (millis() - tiempoAnterior) >= 1000) {
      tiempoAnterior = millis();
      encendido(i); // aqui si, ¿ves la diferencia?
    }
  }
  // Saludos.

Greetings

1 Like

This is not a good way to use millis as you will eventually get issues when it overflows.

This won't work. What value will i be when the time limit is reached? A random number 0-9 ?

You need to get rid of the for loop altogether.

int i = 0;
void loop () 
{
  if(millis() - tiempoAnterior >= 1000)
  {
    tiempoAnterior = millis();
    
    encendido(i);

    i = (i + 1) % 10;
  }    
}
1 Like

there it seems to work, but it skips some numbers

Because i is not being updated correctly. Try the version in post #8 without the for loop.

1 Like

its work thanks

const int pins [7] = {2, 3, 4, 5, 6, 7, 8}; //declaro los pines que utilizare
const byte numerosDisplay[10] = {
  0b0111111,//0
  0b0000110,//1
  0b1011011,//2
  0b1001111,//3
  0b1100110,//4
  0b1101101,//5
  0b1111101,//6
  0b0000111,//7
  0b1111111,//8
  0b1101111 //9
};

void setup () {
  for (byte i = 0; i < 7; i++) {
    pinMode(pins[i], OUTPUT); //declaramos pines como salidas
  }
}

void loop () {
  if ((millis() / 100) % 10 == 0) {
    byte numeros = (millis() / 1000) % 10;
    for (byte i = 0; i < 7; i++)digitalWrite(pins[i], bitRead (numerosDisplay [numeros], i));
  }
}

Perfect hit, Try this one.

int period = 1000;
unsigned long time_now = 0;

void setup() {
 Serial.begin(115200);
}
void loop() {
time_now = millis();
Serial.println("Hello");
while (millis() < time_now + period){
//wait approx. [period] ms
}
}

The well known problem with this code is that after ~49 days millis() will reach it's highest possible count and start over(roll over) at that point your code will not work correctly.
This is why it is recommended to use only subtraction as it will not have this issue and will always work correctly after a roll over.
and it is just a small change to what you have now:

int period = 1000;
unsigned long time_now = 0;

void setup() {
 Serial.begin(115200);
}
void loop() {

if (millis() - time_now >= period){
time_now = millis();
Serial.println("Hello");
}
}

EDIT: Oops I just noticed that your code is also blocking just like delay()

Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

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