# How too add millis at this specific point of the code

**URL:** <https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415>\
**Category:** Programming\
**Created:** [July 23, 2021, 7:58pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415 "2021-07-23T19:58:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![boopesh\_boo](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@boopesh\_boo](https://forum.arduino.cc/u/boopesh_boo)\
**Post date:** [July 23, 2021, 7:58pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/1 "2021-07-23T19:58:34Z")

</div>

how to replace this part of delay with millis?

```cpp
 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.

---

<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 23, 2021, 8:08pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/2 "2021-07-23T20:08:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [July 23, 2021, 8:09pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/3 "2021-07-23T20:09:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![boopesh\_boo](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@boopesh\_boo](https://forum.arduino.cc/u/boopesh_boo)\
**Post date:** [July 23, 2021, 8:11pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/4 "2021-07-23T20:11:53Z")

</div>

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

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

}

```

---

<div class="post-metadata">

**Author:** ![boopesh\_boo](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@boopesh\_boo](https://forum.arduino.cc/u/boopesh_boo)\
**Post date:** [July 23, 2021, 8:12pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/5 "2021-07-23T20:12:43Z")

</div>

No, please check the above comment.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [July 23, 2021, 8:21pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/6 "2021-07-23T20:21:53Z")

</div>

> [@boopesh\_boo](#):
>
> No, please check the above comment.

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

---

<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 23, 2021, 8:23pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/7 "2021-07-23T20:23:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [July 23, 2021, 8:31pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/8 "2021-07-23T20:31:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![boopesh\_boo](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@boopesh\_boo](https://forum.arduino.cc/u/boopesh_boo)\
**Post date:** [July 23, 2021, 8:31pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/9 "2021-07-23T20:31:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![boopesh\_boo](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@boopesh\_boo](https://forum.arduino.cc/u/boopesh_boo)\
**Post date:** [July 23, 2021, 8:37pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/10 "2021-07-23T20:37:22Z")

</div>

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

---

<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:** [November 20, 2021, 8:37pm UTC](https://forum.arduino.cc/t/how-too-add-millis-at-this-specific-point-of-the-code/888415/11 "2021-11-20T20:37:54Z")

</div>

Closed on November 20, 2021, 8:37pm UTC

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