@@ -38,7 +38,10 @@ float lastPressureSamples[LAST_SAMPLES_COUNT];
3838
3939int minuteCount = 0 ;
4040bool firstRound = true ;
41- float pressureAvg[7 ];
41+ // average value is used in forecast algorithm.
42+ float pressureAvg;
43+ // average after 2 hours is used as reference value for the next iteration.
44+ float pressureAvg2;
4245
4346float dP_dt;
4447boolean metric;
@@ -122,6 +125,8 @@ float getLastPressureSamplesAverage()
122125 return lastPressureSamplesAverage;
123126}
124127
128+
129+
125130// Algorithm found here
126131// http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf
127132// Pressure in hPa --> forecast done by calculating kPa/h
@@ -139,12 +144,12 @@ int sample(float pressure)
139144
140145 if (minuteCount == 5 )
141146 {
142- pressureAvg[ 0 ] = getLastPressureSamplesAverage ();
147+ pressureAvg = getLastPressureSamplesAverage ();
143148 }
144149 else if (minuteCount == 35 )
145150 {
146- pressureAvg[ 1 ] = getLastPressureSamplesAverage ();
147- float change = (pressureAvg[ 1 ] - pressureAvg[ 0 ] ) * CONVERSION_FACTOR;
151+ float lastPressureAvg = getLastPressureSamplesAverage ();
152+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
148153 if (firstRound) // first time initial 3 hour
149154 {
150155 dP_dt = change * 2 ; // note this is for t = 0.5hour
@@ -156,8 +161,8 @@ int sample(float pressure)
156161 }
157162 else if (minuteCount == 65 )
158163 {
159- pressureAvg[ 2 ] = getLastPressureSamplesAverage ();
160- float change = (pressureAvg[ 2 ] - pressureAvg[ 0 ] ) * CONVERSION_FACTOR;
164+ float lastPressureAvg = getLastPressureSamplesAverage ();
165+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
161166 if (firstRound) // first time initial 3 hour
162167 {
163168 dP_dt = change; // note this is for t = 1 hour
@@ -169,8 +174,8 @@ int sample(float pressure)
169174 }
170175 else if (minuteCount == 95 )
171176 {
172- pressureAvg[ 3 ] = getLastPressureSamplesAverage ();
173- float change = (pressureAvg[ 3 ] - pressureAvg[ 0 ] ) * CONVERSION_FACTOR;
177+ float lastPressureAvg = getLastPressureSamplesAverage ();
178+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
174179 if (firstRound) // first time initial 3 hour
175180 {
176181 dP_dt = change / 1.5 ; // note this is for t = 1.5 hour
@@ -182,8 +187,9 @@ int sample(float pressure)
182187 }
183188 else if (minuteCount == 125 )
184189 {
185- pressureAvg[4 ] = getLastPressureSamplesAverage ();
186- float change = (pressureAvg[4 ] - pressureAvg[0 ]) * CONVERSION_FACTOR;
190+ float lastPressureAvg = getLastPressureSamplesAverage ();
191+ pressureAvg2 = lastPressureAvg; // store for later use.
192+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
187193 if (firstRound) // first time initial 3 hour
188194 {
189195 dP_dt = change / 2 ; // note this is for t = 2 hour
@@ -195,8 +201,8 @@ int sample(float pressure)
195201 }
196202 else if (minuteCount == 155 )
197203 {
198- pressureAvg[ 5 ] = getLastPressureSamplesAverage ();
199- float change = (pressureAvg[ 5 ] - pressureAvg[ 0 ] ) * CONVERSION_FACTOR;
204+ float lastPressureAvg = getLastPressureSamplesAverage ();
205+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
200206 if (firstRound) // first time initial 3 hour
201207 {
202208 dP_dt = change / 2.5 ; // note this is for t = 2.5 hour
@@ -208,8 +214,8 @@ int sample(float pressure)
208214 }
209215 else if (minuteCount == 185 )
210216 {
211- pressureAvg[ 6 ] = getLastPressureSamplesAverage ();
212- float change = (pressureAvg[ 6 ] - pressureAvg[ 0 ] ) * CONVERSION_FACTOR;
217+ float lastPressureAvg = getLastPressureSamplesAverage ();
218+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
213219 if (firstRound) // first time initial 3 hour
214220 {
215221 dP_dt = change / 3 ; // note this is for t = 3 hour
@@ -218,7 +224,7 @@ int sample(float pressure)
218224 {
219225 dP_dt = change / 4 ; // divide by 4 as this is the difference in time from 0 value
220226 }
221- pressureAvg[ 0 ] = pressureAvg[ 5 ] ; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past.
227+ pressureAvg = pressureAvg2 ; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past.
222228 firstRound = false ; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop.
223229 }
224230
@@ -252,5 +258,13 @@ int sample(float pressure)
252258 forecast = UNKNOWN;
253259 }
254260
261+ // uncomment when debugging
262+ // Serial.print(F("Forecast at minute "));
263+ // Serial.print(minuteCount);
264+ // Serial.print(F(" dP/dt = "));
265+ // Serial.print(dP_dt);
266+ // Serial.print(F("kPa/h --> "));
267+ // Serial.println(weather[forecast]);
268+
255269 return forecast;
256270}
0 commit comments