I have the following code which checks the latency of another board.
int triggerPin = 13;
int dataPin = 9;
int ejectorPin = A0;
unsigned long t_start = 0;
unsigned long t_end = 0;
unsigned long t_total = 0;
int value = 0;
void setup() {
// put your setup code here, to run once:
pinMode(triggerPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(ejectorPin, INPUT);
Serial.begin(9600);
}
void loop() {
t_start = micros();
while (value < 10 ) {
digitalWrite(triggerPin, HIGH);
digitalWrite(dataPin, HIGH);
delay(5.0);
digitalWrite(dataPin, LOW);
digitalWrite(triggerPin, LOW);
delay(1.0);
value = analogRead(ejectorPin);
}
t_end = micros();
t_total = (t_end - t_start);
Serial.println(double(t_total) / 1000);
Note that I'm using analogRead() inside the while loop. With this method I get 18.3 msec. However, if I use the analogRead() in the while condition:
void loop() {
t_start = micros();
while (analogRead() < 10 ) {
.
.
.
then I get 30.2 msec. I thought assigning value of analogRead() to a variable would take longer but here it's the opposite.
Can someone please explain the reason behind this behaviour?