analogRead() delay?

A part of my code causes a significant delay in the loop, which makes me 'miss' the signals that should be received over serial. I have timed this using the serial monitor. The code is as follows:

ADCresolution is set to 12

int pressure
unsigned long pressureInterval = 2000

  if(currentMillis - previousPressureMillis >= pressureInterval)
  {
    SerialUSB.println("Requesting pressure");
    pressure = (4000 * (Indio.analogRead(CHpressuresensor) - 0.51 ) ) / 3.221;
    SerialUSB.println("Pressure measured");
    previousPressureMillis = currentMillis;
    Serial.print("t12.txt=");
    Serial.print("\"");
    if(pressure >= 80)
    {
      Serial.print("OK");
      Indio.digitalWrite(CHerrorlight, LOW);
    }else if(pressure < 79 && pressure >= 20)
    {
      Serial.print("LOW");
      Indio.digitalWrite(CHerrorlight, HIGH);
    }else
    {
      Serial.print("EMPTY");
      Indio.digitalWrite(CHerrorlight, HIGH);
    }
    Serial.print("\"");
    Serial.print("\xFF\xFF\xFF");
    SerialUSB.println("Message sent");
  }

Measured delays:

12:47:25.078 -> Requesting pressure
12:47:25.078 -> Pressure measured
12:47:25.112 -> Message sent
12:47:27.084 -> Requesting pressure
12:47:27.084 -> Pressure measured
12:47:27.084 -> Message sent
12:47:29.091 -> Requesting pressure
12:47:29.091 -> Pressure measured
12:47:29.091 -> Message sent
12:47:31.064 -> Requesting pressure
12:47:31.098 -> Pressure measured
12:47:31.098 -> Message sent
12:47:33.069 -> Requesting pressure
12:47:33.069 -> Pressure measured
12:47:33.102 -> Message sent

 

I cannot get my head around what could cause this, especially because one loop runs correctly, the next one is delayed after the conversion request, and another one is delayed after sending the pressure over serial. I'll have to use a higher update frequency for another sensor, so sticking with a 33ms delay is no option. ADCresolution 12 would be ok, but 14bit is preferable.

I have isolated this part of the code to make sure it's not caused by other parts of the loop. Any ideas are very much appreciated!

Kind regards

 

Yente
Yente
36
| 0 0 0
Asked on 4/9/20, 11:04 AM
0
vote
285 Views

Hi,

I tried your code to reproduce that 34ms delay you found, but i did not see anything like it. You rely on the Serial Monitor timestamp to draw your conclusions, that might not be very accurate. I suggest you do the time measurements on the MCU, you will see that the Indio.analogRead() function only takes about 560 microseconds. This does not depend on the Indio.setADCResolution(), 12 or 18 bits give the same speed. You can use code like this:

  if (millis() - previousPressureMillis >= pressureInterval)
  {
    SerialUSB.println("Requesting pressure");
    float pressure1 = (4000 * (Indio.analogRead(1) - 0.51 ) ) / 3.221;
    SerialUSB.println("Pressure measured");
    previousPressureMillis = millis();
    unsigned long start = micros();
    float pressure2 = (4000 * (Indio.analogRead(1) - 0.51 ) ) / 3.221;
    SerialUSB.println(micros() - start);
  }

If you are receiving data over Serial, that might trigger an interrupt during the analogRead, or your code may include other interrupt based functions, or it might be a Serial Monitor timing issue as mentioned above. 

Tom
Tom
5570
| 0 0 1
Answered on 4/10/20, 6:49 AM
0
vote

Your answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

Ask a Question

Keep Informed

About This Forum

This community is for professionals and enthusiasts of our products and services.

Read Guidelines

Question tools

61 follower(s)

Stats

Asked: 4/9/20, 11:04 AM
Seen: 285 times
Last updated: 4/10/20, 6:49 AM