@@ -38,7 +38,10 @@ float lastPressureSamples[LAST_SAMPLES_COUNT];
38
38
39
39
int minuteCount = 0 ;
40
40
bool 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;
42
45
43
46
float dP_dt;
44
47
boolean metric;
@@ -122,6 +125,8 @@ float getLastPressureSamplesAverage()
122
125
return lastPressureSamplesAverage;
123
126
}
124
127
128
+
129
+
125
130
// Algorithm found here
126
131
// http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf
127
132
// Pressure in hPa --> forecast done by calculating kPa/h
@@ -139,12 +144,12 @@ int sample(float pressure)
139
144
140
145
if (minuteCount == 5 )
141
146
{
142
- pressureAvg[ 0 ] = getLastPressureSamplesAverage ();
147
+ pressureAvg = getLastPressureSamplesAverage ();
143
148
}
144
149
else if (minuteCount == 35 )
145
150
{
146
- pressureAvg[ 1 ] = getLastPressureSamplesAverage ();
147
- float change = (pressureAvg[ 1 ] - pressureAvg[ 0 ] ) * CONVERSION_FACTOR;
151
+ float lastPressureAvg = getLastPressureSamplesAverage ();
152
+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
148
153
if (firstRound) // first time initial 3 hour
149
154
{
150
155
dP_dt = change * 2 ; // note this is for t = 0.5hour
@@ -156,8 +161,8 @@ int sample(float pressure)
156
161
}
157
162
else if (minuteCount == 65 )
158
163
{
159
- pressureAvg[ 2 ] = getLastPressureSamplesAverage ();
160
- float change = (pressureAvg[ 2 ] - pressureAvg[ 0 ] ) * CONVERSION_FACTOR;
164
+ float lastPressureAvg = getLastPressureSamplesAverage ();
165
+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
161
166
if (firstRound) // first time initial 3 hour
162
167
{
163
168
dP_dt = change; // note this is for t = 1 hour
@@ -169,8 +174,8 @@ int sample(float pressure)
169
174
}
170
175
else if (minuteCount == 95 )
171
176
{
172
- pressureAvg[ 3 ] = getLastPressureSamplesAverage ();
173
- float change = (pressureAvg[ 3 ] - pressureAvg[ 0 ] ) * CONVERSION_FACTOR;
177
+ float lastPressureAvg = getLastPressureSamplesAverage ();
178
+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
174
179
if (firstRound) // first time initial 3 hour
175
180
{
176
181
dP_dt = change / 1.5 ; // note this is for t = 1.5 hour
@@ -182,8 +187,9 @@ int sample(float pressure)
182
187
}
183
188
else if (minuteCount == 125 )
184
189
{
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;
187
193
if (firstRound) // first time initial 3 hour
188
194
{
189
195
dP_dt = change / 2 ; // note this is for t = 2 hour
@@ -195,8 +201,8 @@ int sample(float pressure)
195
201
}
196
202
else if (minuteCount == 155 )
197
203
{
198
- pressureAvg[ 5 ] = getLastPressureSamplesAverage ();
199
- float change = (pressureAvg[ 5 ] - pressureAvg[ 0 ] ) * CONVERSION_FACTOR;
204
+ float lastPressureAvg = getLastPressureSamplesAverage ();
205
+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
200
206
if (firstRound) // first time initial 3 hour
201
207
{
202
208
dP_dt = change / 2.5 ; // note this is for t = 2.5 hour
@@ -208,8 +214,8 @@ int sample(float pressure)
208
214
}
209
215
else if (minuteCount == 185 )
210
216
{
211
- pressureAvg[ 6 ] = getLastPressureSamplesAverage ();
212
- float change = (pressureAvg[ 6 ] - pressureAvg[ 0 ] ) * CONVERSION_FACTOR;
217
+ float lastPressureAvg = getLastPressureSamplesAverage ();
218
+ float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
213
219
if (firstRound) // first time initial 3 hour
214
220
{
215
221
dP_dt = change / 3 ; // note this is for t = 3 hour
@@ -218,7 +224,7 @@ int sample(float pressure)
218
224
{
219
225
dP_dt = change / 4 ; // divide by 4 as this is the difference in time from 0 value
220
226
}
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.
222
228
firstRound = false ; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop.
223
229
}
224
230
@@ -252,5 +258,13 @@ int sample(float pressure)
252
258
forecast = UNKNOWN;
253
259
}
254
260
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
+
255
269
return forecast;
256
270
}
0 commit comments