Go Down

Topic: Sampling analogRead() highs and lows. (Read 455 times) previous topic - next topic

Lsnyman

I have a need to record the high and low points of a sensor during the first 10 seconds.
from startup to 5 seconds the sensor will be driven low and from 5 to 10 seconds it will be driven high.
I want to return these 2 values.
i tried the following code but it doesnt seem to work. Am I doing something wrong? probably....
Code: [Select]

while (millis() < 5000) {
    sampleLow = analogRead(0);
    // wait a second so as not to send massive amounts of data
  delay(1000);
Serial.print(sampleLow);
  Serial.println("  ");

}

while (millis() < 10000) {

sampleHigh = analogRead(0);
       // wait a second so as not to send massive amounts of data
  delay(1000);
 
 
    Serial.print(sampleHigh);
  Serial.println("  ");
 
}

WizenedEE

millis() returns the time since the arduino was last reset (essentially), so it looks like that will work once, but then millis() will be above 5 seconds and the loops will immediately terminate.

You need to store the time when you're about to start the 5 second loop, and then find how long it has been since that time.

Also, it's not too great an idea to use a delay in those loops, unless you only want 5 samples.

And wouldn't you want sampleLow to only be set to the analogRead value if the analogRead value was lower than the current sampleLow value?

Lsnyman

Yes it only needs to work once at startup
I can remove the delay but 5 samles over 5 seconds is enough.
It seems to store the low value ok but the high value doesnt hold. After 10 seconds it shows 1 correct reading and then drops to an arbitrary low value

Grumpy_Mike

You need to post all your code.
It is not a good idea to use the absoloute value from millis() because it is not at zero as soon as your sketch starts to run due to the time it spends in the boot loader.

PaulS

Printing to the serial port also takes time. It is unlikely that you will get more than 4 samples during each period if you print and delay in the while loop.

robtillaart


you mean something like this?
Code: [Select]

//
// analog High Low
//
unsigned long lastTime = 0;
int high = 0;
int low = 1024;

void setup()
{
  Serial.begin(115200);
  Serial.println("start...");

  lastTime = millis();
  while ((millis() - lastTime) < 10000UL)
  {
    // MEASURE
    int x = analogRead(A0);

    // MATH
    low = min(low, x);
    high = max(high, x);
  }

  // OUTPUT
  Serial.print("H: ");
  Serial.println(high);
  Serial.print("L: ");
  Serial.println(low);
}

void loop()
{
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Lsnyman

Ah now that is a good way, thank you. I was thinking of Min Max but was stuck with having to have 2 numbers. Reversing the maximun and minimum possible is a great idea.
Sorry for not being clear about the print and delay, those were only there for testing so that I can see the function is working, they will be removed after.

Thank you Rob

Go Up