# Function for Measuring time in an Arduino Code?

Hi,

I am using an IR sensor for a project of mine. I want to be able to move the sensor along an axis for say 1 meter. If there is an object within that 1 meter I want to be able to detect it.

I want to measure the amount of seconds the object is detected for so that I can do distance=speed*time to calculate how long the object is.

Is there a function I can measure time with?

I don't know about a function, but there is an operator -

glenmcmurchy:
I am using an IR sensor for a project of mine. I want to be able to move the sensor along an axis for say 1 meter. If there is an object within that 1 meter I want to be able to detect it.

What is the IR sensor going to detect?

You can use the millis() and micros() functions to measure time periods.

...R

glenmcmurchy:
Hi,

Hi.

I deleted your cross-post, is that OK?

How to use this forum

• Moderator

Robin2:
What is the IR sensor going to detect?

You can use the millis() and micros() functions to measure time periods.

...R

I am planning on moving the sensor along a plane for a set distance. I want to detect simple object lengths. Just rectangles tbh. I want to be able to measure the time that the sensor detects and object till it stops detecting an object.

are you able to explain to me these mills() and micros() functions as I am not familiar with them,
Thanks, Glen

Sorry, I thought this post didn't didn't post at first because my internet cut out. thats why i posted again

Millis() and microseconds() are functions that start counting time once your arduino is powered.

Once your sensor detects an object, call one of both and store the outcome in a variable.
Once your sensor stops.... sensing the object call the functions again and subtract the first reading from the last, giving you the exact time it sensed your object.

``````firstseen = millis();
dostuff();
timespent = millis() - firstseen;
``````

glenmcmurchy:
I am planning on moving the sensor along a plane for a set distance.

Does this mean that the sensor outputs (say) 1 when it detects an object and 0 when it does not?

are you able to explain to me these mills() and micros() functions as I am not familiar with them,

That is why there is a Reference section

...R

Yeh I looked at the reference section but it did not explain it very clearly but I understand Simpson_Jr's explanation so its all good.

And yeh pretty much I will be wanting the sensor to output 1 when object is detected and 0 when there is not. And from that calculate the total time that the output was at 1. if that makes sense

glenmcmurchy:
And yeh pretty much I will be wanting the sensor to output 1 when object is detected and 0 when there is not. And from that calculate the total time that the output was at 1. if that makes sense

When the sensor first detects the object record the value of millis() as (say) startMillis.
Subsequently, when it first detects the absence of the object record the value of millis() as endMillis.
The time taken will be endMillis - startMillis.

Note that variable that hold values from millis() must be defined as unsigned long

...R

Hi Robin2,

thanks I just have one more question,

I will need to measure the time the sensor is sensing an object. Would I be able to do this with an if statement that has multiple parameters. This is what I have so far

`````` unsigned long totaltime = millis();

if (sensor1 || sensor2 <801)
{
unsigned long time1 = millis();
}

if ( //this is the part i am stuck at
{
unsigned long time2 = millis();
}
``````

Then at the end i would do
time till detection = totaltime - time1
duration of detection = time1 - time2

``````if (sensor1 || sensor2 <801)
``````

No, C doesn’t work like that.

That reads as “if sensor1 is non-zero” OR “if sensor2 < 801”.

glenmcmurchy:
This is what I have so far

The line
if (sensor1 || sensor2 <801)
should be
if (sensor1 < 801 || sensor2 < 801)

However I can’t relate the code to your question. I thought I already explained how to “measure the time the sensor is sensing an object”.

You will have to explain the role of the two sensors. I thought you only had 1 sensor.

…R

I am using an IR sensor for a project of mine

Sounds a bit like Arthur "two sheds" Jackson.

Robin2:
I thought I already explained how to "measure the time the sensor is sensing an object".

You will have to explain the role of the two sensors. I thought you only had 1 sensor.

Hi Robin2,

Yeh I have 2 sensors and they both take readings from opposite sides. I have 2 because I want to take the readings from both sides so that if there is an object between them I can do:

size (width) of object = total distance - (sensor1 value + sensor2 value). It is for my university project.

yeh you did explain that actually, my bad! Just wondering how I would tell the arduino to start timing once there is an absence of an object. Because if I wrote

``````if (sensor1 >801 || sensor2 >801) //eg absent of an object because 800 millimetres is the length of the areas
{
unsigned long time2 =millis();
}
``````

That would just start timing from the start i think? (unless there was an object right at the start)

glenmcmurchy:
Yeh I have 2 sensors and they both take readings from opposite sides. I have 2 because I want to take the readings from both sides so that if there is an object between them I can do:

size (width) of object = total distance - (sensor1 value + sensor2 value). It is for my university project.

yeh you did explain that actually, my bad! Just wondering how I would tell the arduino to start timing once there is an absence of an object. Because if I wrote

This is getting more and more confused.

I can’t make any sense of that formula. Can you give some numerical examples for the values?

I thought the purpose of the sensors was to measure the distance but you seem to be using them for some other purpose.

I think we are at the stage where you need to post a drawing or diagram of the apparatus.

I had assumed from your earlier posts that the sensor just produces what amounts to an ON/OFF signal depending on whether it does or does not detect something. And I had assumed that you are using numbers >801 as indicative of ON and numbers <801 as indicatibve of OFF. And my suggestion for using millis() or micros() to measure time was based on that assumption. Record the start time when 801 is first exceeded. Subsequently record the end time when the value first falls below 801.

If this is all wrong please tell me what is the correct situation.

…R

Why are you using analog sensors?

``````if (sensor1 >801 || sensor2 >801) //eg absent of an object because 800 millimetres is the length of the areas
{
unsigned long time2 =millis();
}
``````

You don't want to do this anyway because the variable time2 immediately goes out of scope (disappears) at the closing bracket. You need to declare it elsewhere.

eg.

``````// at global scope
unsigned long time2;

// later
...
if (sensor1 >801 || sensor2 >801) //eg absent of an object because 800 millimetres is the length of the areas
{
time2 =millis();
}
``````

Right okay ill just start from the beginning.

I am building a device that scans down the sides of pallets loaded with product to check if they are stacked safely. The purpose of the sensors is to detect any protruding product and then measure the width and length of the protrusion and from the size of it decide whether it is a minor obstruction or a major (e.g. unsafe) obstruction. It is for my final year university project. The purpose of differentiating between minor and major is to avoid failing loaded pallets when loose labels are hanging off the product, as this can trick the sensors into thinking there is an obstruction. Hope you are still following me.

I want to try and use minimal sensors as possible, therefore I am going to use 2 for each side (8 total). So the 2 sensors will calculate the width of the obstruction by doing:

width = total distance (e.g. width of pallet which is 800mm for my prototype) - (sensor 1 value in mm+ sensor 2 value in mm)

therefore I want to use distance = speed*time to calculate the length of the obstruction down the way. Hence why i am using <800mm as this would mean there is an obstruction as anything >800mm will not be part of the pallet. does that make sense? ps sorry for the lengthy description! haha

Anyway I was thinking maybe I could use some Booleans to help tell the arduino when to start timing.

e.g. something like this (i am aware this is not written correct, I am just writing down what my train of thought)

``````if sensor1 <800 = true && sensor2 >800)
{
time2 =millis();
}
``````

ps thanks for the help guys and soz for being a n00b! haha I have attached an image that will hopefully help you understand what i was explaining

Hi
You are measuring time, do you know the speed so you can calculate distance?

Tom.......