wind speed average

Dear all,

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.

int sensorPin=A0;
static float  total=0.0;
static int counter=10;
static float average=0.0;
static float Wind_Speed;
static float Wind_Kmph;

const int numReadings = 10;
float readings[numReadings];

void setup()
{
 Serial.begin(9600); 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[counter] = 0;  
}

void loop() 
{
  func1();
func2();
  int sensorValue = analogRead(sensorPin);
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.print("voltage: ");
  Serial.println(voltage); 
  
if(counter<10)
  {
    //total= total - readings[counter];  
  
 // total=total+voltage;
 readings[counter] =voltage ;
 total= total + readings[counter]; 
 Serial.print("total: ");
  Serial.println(total);
  counter=counter+1;
 // counter=counter+1;
  
  }else
  {
   counter=0;
    total=0.0;
    
  }
  Serial.print("counter: ");
  Serial.println(counter); 
 average=total/counter;
 Serial.print("average:");Serial.println(average);
 Wind_Speed=(6*average);
 Serial.print("Wind_Speed:");Serial.print(Wind_Speed);Serial.println("m/s");
 Wind_Kmph=3.6*Wind_Speed;
 Serial.print("Wind_Kmph:");Serial.print(Wind_Kmph);Serial.println("KMPH");
   
   
 Serial.println("................."); 
  delay(1000);
  
  
}

void func1()
{
Serial.println("hello");
}
void func2()
{
Serial.println("welcome");
}

how could make it fast without affecting my main program

delay(1000);
Seriously?

func2()l;

What?

my program will running 1000ms delay .but i want to take average for every short interval. SO my question is how can i do it???

Simple rolling average?

Do you have linkage issues in your project?
Why so many "static" variables?

Why are you re-zeroing "readings" in "setup"?

AWOL:
Simple rolling average?

Do you have linkage issues in your project?
Why so many "static" variables?

Why are you re-zeroing "readings" in "setup"?

no i don't have issue. I want to intialise all data to zero at starting.

I want to intialise all data to zero at starting.

By the time "setup()" runs, it already is zero.

setup function It get executed once controller start. Once it get started start accumulating the data.I have copied smoothing program from the analog

setup function It get executed once controller start

Yes, I know this.
I also know that your array is zeroed before "setup ()" gets called - I'm just pointing this out to you.

Edit: I just re-read your original post.
Your REALLY should get rid of that for loop in "setup()"
I suggest you scrap that code and start again.

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

Hope this helps.

The first nine readings will give a too small value as it has summed e.g. four readings but divides by ten

I know. :smiley:

Thought I'd see if he figured that out and ask why.

I am still on the hardware side, have only blinked a few LED’s so far, so I have no real deep knowledge of programming the Arduino.

I have used a few PLC's and in a PLC, I would run with this concept.

10=9
9=8
8=7
7=6
6=5
5=4
4=3
3=2
2=1
1 = new reading.

X=Sum (1;10)/10

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.