How too add millis at this specific point of the code

how to replace this part of delay with millis?

 for(int i=0;i<16;i++) {
      sr.set(i, HIGH);
      delay(time1);
      Serial.print("turn on light");
      Serial.println(i);

can someone please help me out.

We need to know much more about what the full sketch is doing in order to provide advice

It is normal to use millis() for timing to avoid blocking other code from running, but the for loop blocks code anyway. To keep the loop() function running freely you will need to refactor your sketch, perhaps to use a state machine

Hard to say from that snippet. What do you want to do with the CPU cycles you will save by not using delay?

the probelm i face is during the delay time other sensors arent funtioning. which lose track of people count. thats why im looking to replace delays with millis and im really new to all this. please help me out.
the code is here

#include <ShiftRegister74HC595.h>
// create a global shift register object
// parameters: <number of shift registers> (data pin, clock pin, latch pin)
ShiftRegister74HC595<2> sr(14, 16, 15);

#define sensorPin1 5        //ir sensor 1
#define sensorPin2 7        //ir sensor 2
#define sensorPin3 9        //ir sensor 3
#define sensorPin4 11       //ir sensor 4



int sensorState1 = 0;
int sensorState2 = 0;
int sensorState3 = 0;
int sensorState4 = 0;
int count=0;
int time1=200;

void toupon() {                  //Turn on 1st to last light   
   for(int i=0;i<16;i++) {
      sr.set(i, HIGH);
      delay(time1);
      Serial.print("turn on light");
      Serial.println(i);
   }
}



void toupoff() {                  //Turn off from 1st to last light
   for(int i=0;i<16;i++) {
      sr.set(i, LOW);
      delay(time1);
      Serial.print("turn off light ");
      Serial.println(i);
   }
}

void todownon() {                 //turn on last to first light  
   
   for(int i = 15; i>=0; i--) {
      sr.set(i, HIGH);
      delay(time1);
      Serial.print("turn on light ");
      Serial.println(i);
   }
}
 
void todownoff() {                //turn off last to first light       

   for(int i = 15; i>=0; i--) {
      sr.set(i, LOW);
      delay(time1);
      Serial.print("turn off light ");
      Serial.println(i);
   }
}

void setup()
{
  Serial.begin(9600);
  Serial.println("FUN Started");
  Serial.print(count);
  pinMode (sensorPin1,INPUT);
  pinMode (sensorPin2, INPUT);
  pinMode (sensorPin3,INPUT);
  pinMode (sensorPin4,INPUT);
  
  
 
}

void loop() {
  int timex=800;
  
  sensorState1 = digitalRead(sensorPin1);
  sensorState2 = digitalRead(sensorPin2);
  sensorState3 = digitalRead(sensorPin3);
  sensorState4 = digitalRead(sensorPin4);
  int lower = digitalRead(sensorPin1);
  int upper = digitalRead(sensorPin4);

 if (count < 0)
    {
      count = 0;
    }
if(sensorState1 == LOW || sensorState4 == LOW ){   //counts people IN
    count++; 
    Serial.println(count);
    delay(timex);
  }

  if(sensorState2 == LOW || sensorState3 == LOW ){   //counts people OUT
    count--; 
    Serial.println(count);
    delay(timex);
  }
    if(count==0 && sensorState2 == LOW)
   
  {
    todownoff();
   
  }

if(count==0 && sensorState3 == LOW)
   
  {
    toupoff();
   
  }

 if (count==1  &&  sensorState1 == LOW)
 { 
  toupon();
  }
 if (count==1 &&  sensorState4 == LOW)
 { 
  todownon();
  } 

}

No, please check the above comment.

...is not a valid answer to the question.

Let's start with the toupon() function. It needs to be changed or replaced. If you set a boolean to true when it is required to do whatever the function does 16 times then you could call it 16 times whilst that variable remained true and stop when it is no longer required

In the function you should check millis() to ascertain whether enough time has passed since the last action and if so take the action using the value of a counter instead of i, reset the start time and increment the counter variable. If the counter has reached 16 then set the boolean to true to stop calling the function

The standard answer here is a state machine q.v.

But because all your functions are so similar, you could make something simpler.

Use blink without delay to do something every 200 milliseconds in loop. Have a variable tell you what that thing is, including nothing.

Based on which function is in play, light or shut off a single led and set the number for the next one to be touched when the time comes. When all are done, go back to do nothing mode.

as im new it feels hard to understand but this is helpful. will do some research about this. thanks

can you please share me some resource or source to learn about state meachines?

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