Arduino ir sensor with delay

I wanted to use ir sensor with arduino
But i wanted that this if statement should be executed only if ir sensor is high more than 100ms

If  (digitalRead  (ir)==LOW){
count ++;
}

Please help me fast

Store the time returned by millis() when the pin goes HIGH.
If the current time minus the stored time is greater than 100...

Quick enough for you?

1 Like

Yes awol thanks it was quick than i expected
But can you please give me a sample code for it
I want to exectute code when ir will be low
Sorry for mistake

Do you know what millis( ) is ?

1 Like
if  (digitalRead(ir) == LOW) {
  delay(100); // wait
  if (digitalRead(IR) == LOW) count ++; // if still LOW after 100ms...
}

Leo..

1 Like

Thanks wawa aka leo for solution
I thankyou from my heart

Thanks larry for the information

What do you want to happen if something blocks the beam for say one second ?
Do you want that to be registered as a single count or do you want it to be registered as 10 counts? Depending on what you are doing, the accepted solution may require some work.

1 Like

See actually i wanted to make a person counter
So if anything stops it for 1 second or more it should be registered as 1 person
But i dont think i would e stopped for that long time
But if it stops so it should consider 1 person
Can you help me with it

6v6gt??

You have to detect a high to low transition in the output of the sensor and set a timer and set a state variable called say detectorTriggered.
If, at any time, the output of the sensor is high and detectorTriggered is set then (1) unset detectorTriggered and (2) check the timer to see if more than 100ms has elapsed since the timer was set, in which case you increment your person counter.

It would be a good exercise for you to code it your self.

Actually 6v6gt i wanted to deliver this counter to a costumer in 1 week so can you please provide me code just for 1 ir and led as a example

If you open up the IDE, click on "files" then "examples" and scroll through, you'll find an example called "state change detection" or something like that.

If you want someone to write code for you, try asking in the "jobs and consultancy" section of the forum.

I could but I would not want to get into any product liability issues with your "customer" nor would I want, unwittingly, to do someone's school homework for them.

Try something and see if it works. Ask for specific help if required.
Hint: define detectorTriggered as boolean

ok thanks 6v6gt
can i get your mail or that kinda thing for contact if possible
and i will surely try that code advice with hint

thanks awol for the tip

No, you have try it yourself.
The easiest is to use an online simulator such as https://wokwi.com/
Since you can't easily test an IR sensor in a simulation use a push button instead, wiring it to ground and defining a pull up resistor on the chosen arduino pin.

Thanks for your help 6v6gt
Ill try me best

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