I am trying to calculate average of 10 sample wind speed sensor. wind speed sensor out put is analog 0-5v ,wind speed measuring range is 0-30m/s .response time is 1 s. resolution 0.1m/s . start up wind speed is 0.5 m/s.below code i have written is correct or wrong.
how could make it fast without affecting my main program.like cyclic interrupt in PLC systems. for every set time it take sample and gives output.
I took your code and added a way to store and average the last ten readings. I used your code hoping it would help you see what I did. Your code uses way too many floats. I'd store the actual analog readings which are an int and convert to a float when averaging and printing. It's best to minimize the use of floats where possible and with a little thought it's usually possible.
This basically uses the concept found in blink without delay for the timing of taking samples.
const int SENSOR_PIN = A0;
const int SAMPLE_COUNT = 10;
const unsigned long SAMPLE_RATE = 2000UL; // every two seconds
// array to store the samples
float sample[SAMPLE_COUNT];
byte sampleIndex = 0;
// function to average the samples
float sampleAverage(void)
{
float accumulator = 0;
for (int i = 0; i < SAMPLE_COUNT; i++)
{
accumulator += sample[i];
}
return accumulator / SAMPLE_COUNT;
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
static unsigned long lastSampleTime = 0;
unsigned long currentTime = millis();
// check if time for new reading
if (currentTime - lastSampleTime >= SAMPLE_RATE)
{
lastSampleTime = currentTime;
int sensorValue = analogRead(SENSOR_PIN);
float voltage = sensorValue * (5.0 / 1023.0);
Serial.print("voltage: ");
Serial.println(voltage);
// add the newest reading at the oldest pointer
sample[sampleIndex] = voltage;
// advance to next index which is now oldest
sampleIndex++;
// check for and handle rollover
if (sampleIndex == SAMPLE_COUNT)
{
sampleIndex = 0;
}
// the sample array now contains the last ten readings
// sampleIndex now points to the oldest reading
// it will be replaced with the newest next time
float average = sampleAverage();
Serial.print("average:");
Serial.println(average);
float Wind_Speed = (6 * average);
Serial.print("Wind_Speed:");
Serial.print(Wind_Speed);
Serial.println("m/s");
float Wind_Kmph = 3.6 * Wind_Speed;
Serial.print("Wind_Kmph:");
Serial.print(Wind_Kmph);
Serial.println("KMPH");
Serial.println(".................");
}
}
After the loop reads 10 times, the buffer is full and the output is a running average. Also, the reading will ramp up to the reading if anyone were to watch. It would be easy enough to output a line like loading buffer until 10 is > 0
Not sure if this is a waste of resources in an Arduino. one day I will know. : )
does this make sense ?
robtillaart:
The first nine readings will give a too small value as it has summed e.g. four readings but divides by ten
Ya i know. @ beginning assume my wind speed sensor is constant so it reads 0 voltage , when you start rotating it it pic up speed and store relevant values. Once it pick up speed it reads average 10 sample.(condition if wind speed is not moving at all)
if wind speed already gain the speed , i think it catches all the values @ speed it cached.