# Calculate time between 2 data of a sensor

I'm working on a project in my university and I'm having a trouble in calculate time. I have a conveyor and an IR sensor which I use to calculate the maximum and minimum distances of a box when it is moving on the conveyor. Then I want to calculate the time between the max and min distances of box. I have used millis() for timing but it is not accurate. Here is my code:

``````int IRpin = 0;
float volts, distance,firstdata;
int k = 0;
float min;
float mang[150];
int m;
void setup() {
Serial.begin(9600);
}

void loop() {
volts = analogRead(IRpin) * 0.0048828125;
distance = 13 * pow(volts, -1.10);
if (distance < 20)
{
Serial.println(distance);
mang[k] = distance;
k++;
}
else
{
if (k > 0)
{
min = mang[0];
for (int i = 1; i < k; i++)
{

if (min > mang[i])
{
min = mang[i];
m = i;
}
}
firstdata=mang[1];
}
``````

I store data the sensor send to me in an array. Now I want to calculate the time between firstdata [mang(1)] and [(min)]. I use 0A41SK Sharp IR sensor. Thank you for your time and sorry for my bad english.

• 1st - please format your code properly. Most people won't bother looking at it the way it is now. 2nd - I see no millis() or any other timing calls in your code. 3rd - if you only use the mang array for finding the minimal value - you don't need an array at all - just remember the last minimal value (and eventually its time) - this will save you 600 bytes of memory and the corruption caused at the 151st reading of distance < 20. 4th - 'min' is not a good choice for variable name - it is often defined as a macro, which will cause you problems. Jun 21, 2016 at 5:53
• `millis()` not accurate enough? What kind of accuracy do you need? Jun 21, 2016 at 9:04

Ok, yea...Please format this a little better (properly tabbed line spacing and such), and comments would go a long way to understanding this, because chances are people don't want to invest the time to decipher what's going on here without them. I'm not entirely sure what is going on here, but I'll just suggest what I would do in my understanding of the question, which is how to detect what the minimum and maximum value of the distance is and record the time at which each of these moments occurred.

I would have a float value to store the maximum and minimum distance as well as their times, and simply on every iteration of the loop check if the position was greater than the previous maximum distance or shorter than the previous minimum distance. If either of those are true, set the maximum/minimum distance to the current position and set the maximum/minimum distance time to the current time, using millis(). There's no reason I can see, if I'm understanding this correctly for an array to store all the data, it's just taking up memory space from what I see.

Hope this helps a bit, and, if I'm totally off base, again I'll emphasize the utility of comments in readability. Hope this helps and good luck.

I dont care much about code formatting, so i'll just leave you with 'indent' to satisfy the coding snobs that may have been flogged just a bit too hard by their programming teachers.. To the point (one of them) now; You're using int for your measurements against millis(). This will not fly if your measurements exceed 32 seconds, or if the runtime of the sketch exceeds 32 seconds. Please use unsigned long for all variables referencing millis().

I'm not exactly sure what you're trying to achieve with your code but from what i understood is that you're taking distance measurements to see if an object is front of your sensor and then you want to measure the time interval between each object passing by ....right?

Assuming my understanding is correct, what you need to do is first measure the distance to know if an object is there or not, and then if there isnt an object you start a 'timer' until an object appears and then print thy data to Serial Monitor....

``````//this is a comment...they make code easier to use and understand for you and others .....
int IRpin = 0;
float volts, distance,firstdata;
//don't Know why you need those...
/*int k = 0;
float min;
float mang[150];
int m;*/
boolean Exists;
boolean measured;

int startTime;
int Interval;
void setup() {
Serial.begin(9600);
}

void loop() {
volts = analogRead(IRpin) * 0.0048828125;
distance = 13 * pow(volts, -1.10);
if (distance < 20) //an object is in front of the sensor
{
Serial.println(distance);
Exists = true;//object is present
}
else
{
Exists = false; object isn't present
}

if (Exists == false) {// no object present

if(measured == false){//to see if the timer already began
startTime = millis();
measured = true;// so the code only runs once
Read = true; //so that we only measure the time when the timer begins
}}
else
{ if(Read == true){
Interval = millis() - startTime;
measured = false;