-1

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

}
4
  • Where is the sample[] array declared? Apr 19, 2014 at 8:47
  • 2
    also thisseems to be the working code. Add the bad code!
    – Lesto
    Apr 19, 2014 at 11:02
  • And where's your setup() method? You seem to be missing a lot of code: can you add more please? Apr 19, 2014 at 13:16
  • How much is SAMPLE_COUNT?
    – jfpoilpret
    Apr 20, 2014 at 8:03

1 Answer 1

3

What arduino board is being used? I find that some webserver code takes up a lot of the SRAM and in an UNO this leaves very little space for other code.

To save SRAM use the F macro on all the Serial.print statements that have quoted text. i.e:

Serial.print("Some text you want output");

This takes up SRAM when running. 1 byte per letter and space.

Serial.print(F("Some text you want output"));

This takes up no SRAM as it is stored in program space and is makes a big difference.

Search for a freeRam() and use that in debugging your sketch to see how much free space you have.

Relevant question: What can I do if I run out of Flash memory or SRAM?

3
  • Is there way to checkSRAM memory occupied. I am using arduino UNO board .I have removed all Serial.println statement from my code
    – AMPS
    Apr 21, 2014 at 4:33
  • @AMPS this question has already been asked (and answered): arduino.stackexchange.com/questions/763/…
    – jfpoilpret
    Apr 22, 2014 at 21:12
  • this is a really useful tip, though I think there are limitations with what can go in the argument of F(), for example this Serial.print(F((int)lsm.magData.x*100)); seems to cause a compile error. I suppose I could do the maths in a seperate line and then offer its result to F as a variable, but I guess that would probably defeat the obejective of using less memory? Mar 1, 2018 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.