Skip to content

Commit df32c41

Browse files
committed
Fix rain bucket debounce and change units to mm
1 parent dca4f8b commit df32c41

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

tests.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ if (weatherbit.windDirection() == 0) {
4343
weatherbit.simWeather()
4444
}
4545
basic.showNumber(0)
46-
loops.everyInterval(500, () => {
47-
let dir = weatherbit.windDirection()
48-
serial.writeValue("D", dir)
49-
serial.writeLine(weatherbit.directionString(dir))
50-
basic.showArrow(weatherbit.directionArrowName(dir))
46+
loops.everyInterval(1000, () => {
47+
//let dir = weatherbit.windDirection()
48+
//serial.writeValue("D", dir)
49+
//serial.writeLine(weatherbit.directionString(dir))
50+
//basic.showArrow(weatherbit.directionArrowName(dir))
51+
serial.writeValue("rain", weatherbit.rain())
52+
serial.writeValue("rainRate", weatherbit.rainRate())
5153
})

weatherbit.ts

+24-19
Original file line numberDiff line numberDiff line change
@@ -186,42 +186,42 @@ namespace weatherbit {
186186
/**
187187
* returns the correct arrow name for the index supplied
188188
*/
189-
//% weight=19
189+
//% weight=19 blockId="weatherbit_directionArrow" block="wind direction arrow"
190190
export function directionArrowName(direction: number): number {
191191
return directionArrow[Math.round(direction/45)]
192192
}
193193

194194
/**
195195
* returns the correct direction string for the index supplied
196196
*/
197-
//% weight=18
197+
//% weight=18 blockId="weatherbit_directionString" block="wind direction string"
198198
export function directionString(direction: number): string {
199199
return directionStringArray[Math.round(direction/22.5)]
200200
}
201201

202202
/**
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.
205205
*/
206206
//% weight=34 blockId="weatherbit_rain" block="rain"
207207
export function rain(): number {
208208
startRainMonitoring();
209-
let tenthsOfMmOfRain = ((numRainDumps * 2794) / 1000)
210-
return tenthsOfMmOfRain
209+
let mmOfRain = numRainDumps * 0.2794
210+
return mmOfRain
211211
}
212212

213213
/**
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
215215
*/
216216
//% weight=35 blockId="weatherbit_rainRate" block="rain rate"
217217
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
225225
}
226226

227227
/**
@@ -244,9 +244,14 @@ namespace weatherbit {
244244

245245
// Register event handler for a pin 2 high pulse
246246
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+
}
250255
})
251256

252257
// only init once
@@ -311,10 +316,10 @@ namespace weatherbit {
311316
return windMPH
312317
}
313318

314-
/**
319+
/**
315320
* Simulate weather - wind and rain
316321
*/
317-
//% weight=0 blockId="weatherbit_simWeather" block="simulate weather"
322+
//% weight=10 blockId="weatherbit_simWeather" block="simulate weather"
318323
export function simWeather(): void {
319324
control.inBackground(() => {
320325
let i = 0

0 commit comments

Comments
 (0)