Pages: [1]   Go Down
Author Topic: Instant reading of one sensor whilst averaging another simultaneously  (Read 606 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to figure out a way if it is possible to have 2 different sensors displayed with one of them showing instant results and the second an average of results.

I have been using a "for loo"p for the averaging with an SHT75 sensor, putting the readings into an array etc. The problem is, is that the time taken to read the sensor 10 times is many many seconds which means that my other sensor which is a pressure sensor is not being polled..

Is there anyway to continuously display the pressure sensor reading and average the SHT75 in the background?

MAny thanks

ps I'm quite new to programming!
Logged

Croatia
Offline Offline
God Member
*****
Karma: 13
Posts: 524
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Poll your real time sensor and display both its result and the average result continuously, but use timer interrupt to trigger the other sensor and code that calculates the average only when needed (in intervals).
Logged

Pottstown, PA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 326
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why wait 10 times? You could read each sensor continuously. Display the current read only during the first 9 reads. After the 10th read, then start displaying the average along with the current. At the next read, throw out the first read thus leaving the last 10 values read. On each 'read', continue throwing out the earliest value read and adding the latest value read to the 10 'reads'. - Scotty
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks scotty.

Is there any sample code you have for the average readings, namely the rolling average. Is it a particular function which I haven't found yet?

Thanks

Peter
Logged

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

use blink without delay with few boolean statements...
Logged


Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ouh you could always use something like
Code:
if (Count <=10)
Something1=analogRead(A1);
Something2=analogRead(A2);
Total1=Something1+Total1;
Total2=Something2+Total2;
count++;
else
{
  Average1=Total1/Count;
  Average2=Total2/Count;
  Serial.println(Average1);
  Serial.println(Average2);
  Count=0;
}
Logged

Croatia
Offline Offline
God Member
*****
Karma: 13
Posts: 524
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
a = b;
b = c;
c = d;
...
j = analogRead(sensor);
someDisplayFunctionOrOther ((a + b + c + ... + j) / 10);
Logged

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

partial code with 2 sensors using a running average (TODO add 2 functions to read the sensors)
Code:
int average;

void setup()
{
  Serial.begin(115200);
  average = readSensor2(); // initial value
}

void loop()
{
  int direct = readSensor1();
  int average = average*0.6 + readSensor2()*0.4;  // weighted running average;  60% old/40% new  other weights possible of course;

  Serial.print(direct);
  Serial.print("\t");
  Serial.println(average);
}

for 1 sensor both the last value an
Code:
int average;

void setup()
{
  Serial.begin(115200);
  average = readSensor2(); // initial value
}

void loop()
{
  int direct = readSensor1();
  average = average*0.6 + direct*0.4;

  Serial.print(direct);
  Serial.print("\t");
  Serial.println(average);
}
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks everybody. Will try a few of the suggestions and see which works out best for this sensor. As teh temperatures could fluctuate wildly (intentionally). I reckon the a=b etc option is best as it will clear the buffers out reasonably quickly when I take the probe from room temperature into a hot air duct. But the other principle may well be useful for another project I'm doing.

Thanks again.

Logged

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

Quote
As teh temperatures could fluctuate wildly (intentionally).
Really? Any real sensor is going to have some lag time between when the sensed condition changes and when the reported value changes. If you read frequently, you will not get wild fluctuations.

Unless you are using a torch and a bucket of ice water to heat and cool the sensor directly, wild temperature fluctuations are unlikely.
Logged

Pages: [1]   Go Up
Jump to: