Hi all,
I'm using the following sketch to evaluate the wind speed and overall the Gust.
What I want to do: Read the wind sensor each second during 10 seconds
What result I want: the average speed during this period and gust speed
What problem do I have? I don't knot how to erase the array after publishing and this average is not working
I simulated a random reading from 0 to 5 because I have not here the sensor
thank's for your help guys I'm very stuck
Eduard
//Global Variables
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
byte seconds_10s; //Keeps track of the "wind speed/dir avg" over last 10 seconds array of data
byte windspdavg[120]; //120 bytes to keep track of 10 seconds average
float windgust_10s[10]; //10 floats to keep track of 10 seconds max
float i;
float windspdkph_avg2m;
//These are all the weather values
float windspeedkph = 0; // [mph instantaneous wind speed]
float windgustkph = 0; // [mph past 10 second wind gust kph ]
long lastWindCheck = 0;
int AnemometerPin = 5;
int count = 0;
// volatiles are subject to modification by IRQs
volatile long lastWindIRQ = 0;
volatile byte windClicks = 0;
void wspeedIRQ()
// Activated by the magnet in the anemometer (2 ticks per rotation), attached to input AnemometerPin D5
{
if (millis() - lastWindIRQ > 10) // Ignore switch-bounce glitches less than 10ms (228KPH max reading) after the reed switch closes
{
lastWindIRQ = millis(); //Grab the current time
windClicks++; //There is 2.4011412KPH for each click per second.
}
}
//---------------------------------------------------------------
void setup()
{
///////////////////WindMeter///////////////////////////
pinMode(AnemometerPin, INPUT_PULLUP); // input from wind meters windspeed sensor
Serial.begin(9600); // open serial over USB
// attach external interrupt pins to IRQ functions
attachInterrupt(AnemometerPin, wspeedIRQ, FALLING);
// turn on interrupts
interrupts();
}
//---------------------------------------------------------------
void loop()
{
for (int i = 0; i < 10 ; i++) {
//Take a speed and direction reading every second for 10 seconds average
//if (++seconds_10s > 10) seconds_10s = 0;
//Calc the wind speed and direction every second for 10 second to get 10 seconds average
//float currentSpeed = get_wind_speed();
float currentSpeed = random(0, 5); //For testing
Serial.println();
Serial.print("currentSpeed:");
Serial.println(currentSpeed);
windspdavg[i] = (int)currentSpeed;
//if(seconds_10s % 10 == 0) displayArrays(); //For testing
//Check to see if this is a gust for the 10 seconds reading time
if (currentSpeed > windgust_10s[i])
{
//windgust_10s[seconds_10s] = currentSpeed;
windgust_10s[i] = currentSpeed;
}
delay(1000);
}
//Get readings from all sensors
getWeather();
//Rather than use a delay, keeping track of a counter allows the photon to
// still take readings and do work in between printing out data.
//This function prints the weather data out to the default Serial Port
Serial.println("Data to send ----------------");
Serial.print("windspeedkph=");
Serial.print(windspdkph_avg2m);
Serial.print(",");
Serial.print("windgustkph=");
Serial.print(windgustkph);
Serial.print(",");
Serial.println("");
if (i > 10) i = 0;
// windgust_10s = 0; //Zero out this minute's gust
delay(5000);
}
//---------------------------------------------------------------
//Returns the instataneous wind speed
float get_wind_speed()
{
float deltaTime = millis() - lastWindCheck; //750ms
deltaTime /= 1000.0; //Covert to seconds
float windSpeed = (float)windClicks / deltaTime; //3 / 0.750s = 4
windClicks = 0; //Reset and start watching for new wind
lastWindCheck = millis();
windSpeed *= 2.4011412;
Serial.println();
Serial.print("Windspeed:");
Serial.println(windSpeed);
return (windSpeed);
}
//---------------------------------------------------------------
void getWeather()
{
//Calc windspeed
windspeedkph = get_wind_speed();
//Calc windspdkph_avg2m
float temp = 0;
for(int y = 0 ; y < 10 ; y++)
temp += windspdavg[y];
temp /= 10.0;
float windspdkph_avg2m = temp;
//Find the largest windgust in the last 10 seconds
for (int x = 0; x < 10 ; x++)
{
if (windgust_10s[x] > windgustkph)
{
windgustkph = windgust_10s[x];
}
}
}