title: running a background process on arduino
body: <p>I am trying to get my arduino mega to run a function  in the background while it is also running a bunch of other functions. </p>
  
  <p>The function that I am trying to run in the background is a function to determine wind speed from an anemometer.  The way it processes the data is similar to that of an odometer in that it reads the number of turns that the anemometer makes during a set time period and then takes that number of turns over the time to determine the wind speed.  The longer time period that i have it run over the more accurate data i receive as there is more data to average. </p>
  
  <p>The problem that i have is there is a bunch of other data that i am also reading in to the arduino which i would like to be reading in once a second.  This one second time interval is too short for me to get accurate wind readings as not enough revolutions are being completed by the anemometer to give high accuracy wind data.</p>
  
  <p>Is there a way to have the wind sensor function run in the background and update a global variable once every 5 seconds or so while the rest of my program is running simultaneously and updating the other data every second.</p>
  
  <p>Here is the code that i have for reading the data from the wind sensor.  Every time the wind sensor makes a revolution there is a portion where the signal reads in as 0, otherwise the sensor reads in as a integer larger than 0.</p>
  
  <pre><code> void windmeterturns(){
       startime = millis();
       endtime = startime + 5000;
       windturncounter = 0;
       turned = false;
       int terminate = startime;
       while(terminate &lt;= endtime){
             terminate = millis();
             windreading = analogRead(windvelocityPin);
             if(windreading == 0){
                 if(turned == true){
                     windturncounter = windturncounter + 1;
                     turned = false;
                 }
             }
             else if(windreading &gt;= 1){
                 turned = true;
             }
             delay(5);
       }
   }
  </code></pre>
  
  <p>The rest of the processing of takes place in another function but this is the one that I am currently struggling with.  Posting the whole code would not really be reasonable here as it is close to a 1000 lines.</p>
  
  <p>The rest of the functions run with a 1 second delay in the loop but as i have found through trial and error the delay along with the processing of the other functions make it so that the delay is actually longer than a second and it varies based off of what kind of data i am reading in from the other sensors so a 5 loop counter for timing i do not think will work here</p>