Arduino Time measure in double array

Hi, I would like to get suggestion how to do exception handling for the first or last double array value with time measuring technique using millis().

Here is the simple explanation,

  1. In double array, value is stored as [time, position for ID 1 servo, position for ID 2 servo, position for ID 3 servo ].

  2. In the loop, a timer using milis() compares the trajectory time span in double array.

My approach for the trajectory time span is simply to subtract the previous time from the very next one in two rows in double array (See trajectory in the code)

If the timer corresponds to stored time in each row of double array, it will do stored position in that row.

Issue:

As you know, there is no value after the last row in the double array, so I can not get the trajectory time span by subtracting two points in row, I am wondering that there any approach I can try to achieve my goal? (Such as except handling for the last row in array.., But I failed a lot :frowning: )

unsigned long now = 0;
unsigned long past = 0;

const int counts = 11; // The number of counts for Time Data.

int trajectory [counts][4]{ // Sampled Goal Position's Trjectory Data.
  { 0, 360, 360, 360}, // Time[ms], ID1, ID2, ID3
  {100, 340, 328, 300},
  {200, 320, 259, 262},
  {300, 278, 281, 289},
  {400, 205, 211, 226},
  {500, 238, 246, 270},
  {600, 274, 286, 218},
  {700, 211, 226, 269},
  {800, 247, 266, 218},
  {900, 281, 203, 263},
  {1000,209, 234, 202},
};




int j = 0;
int cnt = 0; // Loop three times
//int k = 0;
//bool zero_check = false; 

void loop() {
  
  now = millis(); // Start. 

 if(now - past >= trajectory[j+1][0] - trajectory[j][0]) // Time difference between the past (past) and current time (now) 0. 
  { 
      DEBUG_SERIAL.print("Now: ");    
      DEBUG_SERIAL.print(now);      
      DEBUG_SERIAL.print("\t Past: ");    
      DEBUG_SERIAL.print(past);      
      DEBUG_SERIAL.print("\t Now-Past: ");    
      DEBUG_SERIAL.print(now - past);      
      DEBUG_SERIAL.print("\t Trajectory time span in two points:");    
      DEBUG_SERIAL.print(trajectory[j+1][0] - trajectory[j][0]);            
      DEBUG_SERIAL.print("\t Trajectory point in time :");    
      DEBUG_SERIAL.print(trajectory[j][0]);
      DEBUG_SERIAL.print("\t ID1:");    
      DEBUG_SERIAL.print(trajectory[j][1]);
      DEBUG_SERIAL.print("\t ID2:");    
      DEBUG_SERIAL.print(trajectory[j][2]);
      DEBUG_SERIAL.print("\t ID3:");    
      DEBUG_SERIAL.print(trajectory[j][3]);
      DEBUG_SERIAL.print("\t J:");    
      DEBUG_SERIAL.println(j);

      j +=1;

      past = now; // Update time. 
  
      if (j >= counts-1 ) // J = 28
    { DEBUG_SERIAL.println("######################END#######################");    
      DEBUG_SERIAL.print("J = ");    
      DEBUG_SERIAL.println(j);    
      j = 1;       
    }
    
   }
}

You need to handle one of both end as a special case.

I would do If current time minus start time is larger than the last entry then you are done, if not you know you are somewhere in the array and it’s a matter of either maintaining an index or just finding in which interval you currently are by checking between pos and pos+1 in a for loop where pos goes from 0 to N-2 ( for (byte pos =0; pos < arrayCount-1; pos++) {…} where arrayCount is a constant obtained with the usual sizeof operator for an array (dividing the number of bytes by the size of the first entry)

not clear (to me) what you're trying to do. what event is being measured

if the first value in each row represents a time delta, the original values suggest a histogram where each row represents values for a range of time deltas in 100 msec intervals

but the code simply increments j (incorrectly) and conditionally generates a report. there doesn't appear to be any correspondence between the current measurement and row being considered

shouldn't j be reset to zero after it has been incremented to count

        j +=1;
        if (j >= counts)
        { 
            j = 0;
        }

considering that the table appears to contain values corresponding to time deltas, shouldn't the table be scanned for the row where the first value is >= to the time delta?

since loop() runs repeatedly, the "now" is likely to be minimally different than the previous value.

past will only be set to now when the condition is met -- not sure what is being measured