Calculate time between 2 data of a sensor

Hello guys, 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. Thank you for your time and sorry for my bad english.

I do not know about timing but did you tried using pulseIn(); ?? My suggestion can be totally wrong i am really new to Arduino

Let us see your code and state what IR sensor you are using to get the best help here.

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];
}

Here my code to show how I get the minimum distance, I store data the sensor send to me in an array. Now I want to calculate the time between firstdata [mang(1)] and [(min)]. Tks for your help. And I’m using 0A41SK Sharp IR sensor.

NewStudent: I do not know about timing but did you tried using pulseIn(); ?? My suggestion can be totally wrong i am really new to Arduino

I do not really understand your idea in using this function. Could you tell me more.

Unless you record the (relative) time of the minimum and maximum points, you are not going to be able to calculate the time between them. Millis() would be ideal. Did you declare your time variables as unsigned long in your earlier attempt ?

6v6gt: Unless you record the (relative) time of the minimum and maximum points, you are not going to be able to calculate the time between them. Millis() would be ideal. Did you declare your time variables as unsigned long in your earlier attempt ?

Yes I did. But the problem is I don't know how to record time of those two points. Do you have any suggestion for me?

I’m not sure I understand your difficulty in using millis() to calculate a time difference. Here is very basic example-

unsigned long myMaxMs ;
unsigned long myMinMs ;
unsigned long myTimeDifference ;
. . .
. . .
void loop() {
   . . .
   myMaxMs = millis() ;
   . . .
   . . .
   myMinMs = millis() ;
   . . .
   . . .
   myTimeDifference = myMaxMs - MyMinMS ; // time difference. use abs() to keep it positive if required.
   . . .
}

In your code, you’ll soon have a problem with:

  if (distance < 20)
  {
    Serial.println(distance);
    mang[k] = distance;
    k++;
  }

if distance remains under 20, the bounds of array mang will be exceeded.

I guess that you have derived the formula from the data sheet for the sensor and it is giving results in the expected range:

  volts = analogRead(IRpin) * 0.0048828125;
  distance = 13 * pow(volts, -1.10);

6v6gt:
I’m not sure I understand your difficulty in using millis() to calculate a time difference. Here is very basic example-

unsigned long myMaxMs ;

unsigned long myMinMs ;
unsigned long myTimeDifference ;
. . .
. . .
void loop() {
. . .
myMaxMs = millis() ;
. . .
. . .
myMinMs = millis() ;
. . .
. . .
myTimeDifference = myMaxMs - MyMinMS ; // time difference. use abs() to keep it positive if required.
. . .
}




In your code, you'll soon have a problem with: 


if (distance < 20)
{
Serial.println(distance);
mang[k] = distance;
k++;
}



if distance remains under 20, the bounds of array mang[] will be exceeded.


I guess that you have derived the formula from the data sheet for the sensor and it is giving results in the expected range:


volts = analogRead(IRpin) * 0.0048828125;
distance = 13 * pow(volts, -1.10);

Thank for your reply and code of millis(). Here is what I’m trying to do


I’m making a project which is a conveyor can calculate the size and weight of a box. Base on the picture above, I want to capture the distance from sensor to the box at 3 points and the time when the box cross sensor between 3 points. When i got those data and the speed of the conveyor I can calculate the length and width of the box using pythagorean formula. I know the limitation if I use array to store data but I don’t know any other ideal to capture then calculate those data.

You don't need an array. Here is the basic pseudo code.

loop() {

   if object has just come into view this loop iteration {
       ObjectFirstDetectedMs = millis() ;
       ObjectMinimumDistance = CalculatedObjectDistance() ;
       ObjectMinimumMs = millis() ;
       ObjectInView = true ;
   }
   if ( objectInView == true ) {
      if ( ObjectMinimumDistance > CalculatedObjectDistance() ) {
         ObjectMinimumDistance = CalculatedObjectDistance() ;
         ObjectMinimumMs = millis() ;
      }
   }
   else {
      if object has just gone out of view this loop iteration {
          objectLastDetectedMs = millis() ;
          ObjectInView = false ;
          // OK. You now have a start time, an end time and a minimum distance time
          // so do you can complete your calculation
      }
   }
}