Go Down

Topic: Is there a delay on the sequence a code is running through? (Read 333 times) previous topic - next topic

kingofwoods

I'm planning on making some project with Arduino uno where I have to read out 3 sensors at the same time.

Example: I have 3 sensors that will get information at the same time I touch a box. If I run a piece of code:
Code: [Select]

Serial.print(analogRead(sensor1));
Serial.print(analogRead(sensor2));
Serial.print(analogRead(sensor3));


What will be the delay in ms or nanoseconds between 1 and 3? I need to know because my project needs to be very accurate.

Hypothetical, if I kick a ball, the moment my foot touches the ball should be absolute 0 pointer for all 3 sensors so I can do some math on the results. If this is the case, i4m happy. if sensor 3 gets his results 1 ms later because of latency in sequence I need to know so I can make adjustments.

So I hope my question is clear. How do I know what latency sensor feedback will be because of the sequence i'm putting them in my code.

Sorry if it isn't clear. English is not my native language.

sterretje

You can reasonably easy test timings yourself.

Code: [Select]
void loop()
{
  unsigned long startTime = millis();
  int x = 0;
  for (int cnt = 0; cnt < 100; cnt++)
  {
    x += analogRead(A0);
  }

  Serial.printlln(x);
  Serial.println(millis() -startTime);
}

The last line prints the duration in ms for 100 analogReads; you can use micros instead of millis if the total is short so you can get a more accurate result.

The manipulation and printing of x is added to prevent the compiler from optimising it away.

PS
An analogRead takes about 100 us if I'm not mistaken. The above code should confirm that. Be aware that when reading multiple analog inputs, you might have to read each one twice and discard the first reading.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

pert

You're better off to store the value of the three readings in variables, then print those variables. That way you avoid the time spent printing each reading before taking the next one. Then you only need to deal with the time each analogRead takes.

Robin2

You may find that this code gives you unexpected results
Code: [Select]
Serial.print(analogRead(sensor1));
Serial.print(analogRead(sensor2));
Serial.print(analogRead(sensor3));


When you change to a new ADC pin it is better to discard the first reading - so something like this
Code: [Select]
sensor1Val = analogRead(sensor1);
sensor1Val = analogRead(sensor1);

sensor2Val = analogRead(sensor2);
sensor2Val = analogRead(sensor2);

sensor3Val = analogRead(sensor3);
sensor3Val = analogRead(sensor3);

// then print the values


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MorganS

When you change to a new ADC pin it is better to discard the first reading - so something like this
I don't agree with that advice. The only time that is necessary is if you are pushing the analog system to its limits. If you're driving the registers to get the fastest performance then there are rare situations where the sample capacitor can hold some memory of the previous sample.

But the Arduino's analogRead() is not driving it to the limit. It is relatively slow and gives the capacitor plenty of time to charge. If the advice above was true then the vast majority of Arduino programs using two analog inputs would not work.
"The problem is in the code you didn't post."

kingofwoods

All right, Thx for the advice guys. I'll get working with the examples you gave me to see what results I get. I should figure the rest out on my self.

Go Up