@@ -186,42 +186,42 @@ namespace weatherbit {
186
186
/**
187
187
* returns the correct arrow name for the index supplied
188
188
*/
189
- //% weight=19
189
+ //% weight=19 blockId="weatherbit_directionArrow" block="wind direction arrow"
190
190
export function directionArrowName ( direction : number ) : number {
191
191
return directionArrow [ Math . round ( direction / 45 ) ]
192
192
}
193
193
194
194
/**
195
195
* returns the correct direction string for the index supplied
196
196
*/
197
- //% weight=18
197
+ //% weight=18 blockId="weatherbit_directionString" block="wind direction string"
198
198
export function directionString ( direction : number ) : string {
199
199
return directionStringArray [ Math . round ( direction / 22.5 ) ]
200
200
}
201
201
202
202
/**
203
- * Reads the number of times the rain gauge has filled and emptied
204
- * Returns 0.1mm of rain.
203
+ * Reads the rain gauge count of the number of times the bucket has filled and emptied
204
+ * Returns cumulative mm of rain.
205
205
*/
206
206
//% weight=34 blockId="weatherbit_rain" block="rain"
207
207
export function rain ( ) : number {
208
208
startRainMonitoring ( ) ;
209
- let tenthsOfMmOfRain = ( ( numRainDumps * 2794 ) / 1000 )
210
- return tenthsOfMmOfRain
209
+ let mmOfRain = numRainDumps * 0. 2794
210
+ return mmOfRain
211
211
}
212
212
213
213
/**
214
- * Returns the rate of rainfall in 0.1mm /hour based on the last time to fill the bucket
214
+ * Returns the rate of rainfall in mm /hour based on the last time to fill the bucket
215
215
*/
216
216
//% weight=35 blockId="weatherbit_rainRate" block="rain rate"
217
217
export function rainRate ( ) : number {
218
- startRainMonitoring ( ) ;
219
- if ( msRainDump == 0 )
220
- return 0
221
- else if ( control . millis ( ) - msRainDumpLast > msRainDump ) // rate estimate decays over time if no more dumps when rain stops
222
- return 2794 * 3600 / ( control . millis ( ) - msRainDumpLast )
223
- else
224
- return 2794 * 3600 / msRainDump
218
+ startRainMonitoring ( ) ;
219
+ if ( msRainDump == 0 )
220
+ return 0
221
+ else if ( control . millis ( ) - msRainDumpLast > msRainDump ) // rate estimate decays over time if no more dumps when rain stops
222
+ return 279.4 * 3600 / ( control . millis ( ) - msRainDumpLast )
223
+ else
224
+ return 279.4 * 3600 / msRainDump
225
225
}
226
226
227
227
/**
@@ -244,9 +244,14 @@ namespace weatherbit {
244
244
245
245
// Register event handler for a pin 2 high pulse
246
246
control . onEvent ( EventBusSource . MICROBIT_ID_IO_P2 , EventBusValue . MICROBIT_PIN_EVT_RISE , ( ) => {
247
- numRainDumps ++
248
- msRainDump = control . millis ( ) - msRainDumpLast
249
- msRainDumpLast = control . millis ( )
247
+ // The rain sensor seems to raise 3 events in quick succession for every tip
248
+ // filter these out based on time.
249
+ let gap = control . millis ( ) - msRainDumpLast
250
+ if ( gap > 1000 ) {
251
+ numRainDumps ++
252
+ msRainDump = gap
253
+ msRainDumpLast = control . millis ( )
254
+ }
250
255
} )
251
256
252
257
// only init once
@@ -311,10 +316,10 @@ namespace weatherbit {
311
316
return windMPH
312
317
}
313
318
314
- /**
319
+ /**
315
320
* Simulate weather - wind and rain
316
321
*/
317
- //% weight=0 blockId="weatherbit_simWeather" block="simulate weather"
322
+ //% weight=10 blockId="weatherbit_simWeather" block="simulate weather"
318
323
export function simWeather ( ) : void {
319
324
control . inBackground ( ( ) => {
320
325
let i = 0
0 commit comments