Help me add millis funtion to this below code

Project discerption: Automatic stair case lighting with four ir sensors also with poeple count
i have used shift register for using 16 output pins.
and i cant really use delays in this which just makes this project of no use. as lights are set to be turned on one by on and turn off one by one..but as this are going with delays . poeple count isnt funtions as delay just stops the whole sketch.
please help me add millis funtions to my project sooo this can be a perfect use of pair.
code:

#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=1000;

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();
   
  }

  
  else if (count>0 &&  sensorState1 == LOW){ toupon();}
       if (count>0 &&  sensorState4 == LOW){ todownon();} 

  }

It's not a question of "adding to the code", it's a matter of a complete restructuring.
The usual advice is to do this as a state-machine.

1 Like

yes i can restructure the code with state machine.. but it can only work for sensors right . but how to add millis in shift register outputs?

Wrong.

Take a look at the Several things at the same time tutorial

1 Like

i get it..but all are set as outputs. for shift register outputs how to do that. ?
im not sure is it possible define each light . as im using shift register output pins.

I'm sorry, I'm not sure I understand what you mean.

The shift register doesn't know or care if the timing is done with delay or cooperative multitasking.

1 Like

Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.