Pages: [1]   Go Down
Author Topic: Sampling analogRead() highs and lows.  (Read 408 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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("  ");
 
}
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14051
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


you mean something like this?
Code:
//
// 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()
{
}
Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 2
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: