I have wind speed sensor that can measure max speed upto 30 m/s with analog out put 0-5v. I have simple code calling from the loop. When I put the wind speed average code, the web server is not working and it just hangs. If I comment out web server it's working fine. Let me know What makes my web server hang or not get connected. Is there any way I can change average taking code?
Working code should like below:
if wind speed exceed 30 m/s
mode =stow
else
mode =track
My code. And how can write average of windload of day to SDcard
#include"wind.h"
#include"mode.h"
float sampleAverage(void)
{
float accumulator = 0;
for (int i = 0; i < SAMPLE_COUNT; i++)
{
accumulator += sample[i];
}
return accumulator / SAMPLE_COUNT;
}
void Wind_calc()
{
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);*/
Wind_Speed = (6 * average);
Wind_Kmph = 3.6 * Wind_Speed;
/* Serial.print("Wind_Speed:");
Serial.print(Wind_Speed);
Serial.println("m/s");
Serial.print("Wind_Exceed_count:");Serial.println(Wind_Exceed_count);Serial.println(".................");
Serial.print("Wind_Kmph:");
Serial.print(Wind_Kmph);
Serial.println("KMPH");
*/
if((Wind_Speed>4.0)&&(Stow_flag==0))
{
Wind_Exceed_count=Wind_Exceed_count+1;
//Serial.println("Wind_Exceed_count");Serial.print(Wind_Exceed_count);
if(Wind_Exceed_count<5)
{
Stow_flag=0;
}
else
{
Stow_flag=1;
MODE=STOW;
}
}
else
{
Redo_Wind_calc();
}
}
}
void Redo_Wind_calc()
{
// Serial.println("Wind_Exceed_count");Serial.print(Wind_Exceed_count);
if(Wind_Speed>4.0 &&Stow_flag==1)
{
Wind_Exceed_count=Wind_Exceed_count+1;
if(Wind_Exceed_count<=10)
{
MODE=STOW;
}
else
{
MODE=TRACK;
Wind_Exceed_count=0;
Stow_flag=0;
}
}
}
void loop()
{
Wind_calc();
webcontrol();
}
void webcontrol()
{
//Web control function are defined here
}
sample[]
array declared?setup()
method? You seem to be missing a lot of code: can you add more please?SAMPLE_COUNT
?