@@ -263,55 +263,62 @@ namespace weatherbit {
263
263
* Read the wind direction from the wind vane.
264
264
* Returns the compass bearing of the wind direction in degrees
265
265
*/
266
- let windDirLast = 9999
266
+ let windDir = 0
267
+ let windDirLast = 9999
267
268
//% weight=20 blockId="weatherbit_windDir" block="wind direction"
268
269
export function windDirection ( ) : number {
269
270
startWindMonitoring ( ) ;
270
271
271
- /* read until reading stable */
272
- let windDir = 0
272
+ /* read up to 10 times until reading stable-ish */
273
273
windDir = pins . analogReadPin ( AnalogPin . P1 )
274
- while ( Math . abs ( windDir - windDirLast ) > 10 ) { /* 10 is an arbitary number based on guess and observation */
274
+ for ( let i = 0 ; i < 10 ; i ++ ) {
275
+ if ( Math . abs ( windDirLast - windDir ) < 5 )
276
+ break ;
275
277
windDirLast = windDir
276
278
windDir = pins . analogReadPin ( AnalogPin . P1 )
277
279
}
280
+ windDirLast = windDir
278
281
279
-
280
- if ( windDir < 911 && windDir > 894 )
282
+ if ( windDir <= 913 && windDir > 879 )
281
283
return 0
282
- else if ( windDir < 670 && windDir > 653 )
284
+ else if ( windDir <= 681 && windDir > 616 )
283
285
return 22.5
284
- else if ( windDir < 709 && windDir > 692 )
286
+ else if ( windDir <= 746 && windDir > 681 )
285
287
return 45
286
- else if ( windDir < 395 && windDir > 382 )
288
+ else if ( windDir <= 395 && windDir > 382 )
287
289
return 67.5
288
- else if ( windDir < 408 && windDir > 394 )
290
+ else if ( windDir <= 415 && windDir > 395 )
289
291
return 90
290
- else if ( windDir < 381 && windDir > 364 )
292
+ else if ( windDir <= 381 && windDir > 364 )
291
293
return 112.5
292
- else if ( windDir < 492 && windDir > 475 )
294
+ else if ( windDir <= 509 && windDir > 457 )
293
295
return 135
294
- else if ( windDir < 439 && windDir > 422 )
296
+ else if ( windDir <= 457 && windDir > 415 )
295
297
return 157.5
296
- else if ( windDir < 584 && windDir > 564 )
298
+ else if ( windDir <= 615 && windDir > 552 )
297
299
return 180
298
- else if ( windDir < 543 && windDir > 526 )
300
+ else if ( windDir <= 552 && windDir > 509 )
299
301
return 202.5
300
- else if ( windDir < 820 && windDir > 803 )
302
+ else if ( windDir <= 833 && windDir > 802 )
301
303
return 225
302
- else if ( windDir < 801 && windDir > 784 )
304
+ else if ( windDir <= 802 && windDir > 747 )
303
305
return 247.5
304
- else if ( windDir < 994 && windDir > 977 )
306
+ else if ( windDir <= 994 && windDir > 971 )
305
307
return 270
306
- else if ( windDir < 933 && windDir > 916 )
308
+ else if ( windDir <= 940 && windDir > 914 )
307
309
return 292.5
308
- else if ( windDir < 964 && windDir > 947 )
310
+ else if ( windDir <= 970 && windDir > 940 )
309
311
return 315
310
- else if ( windDir < 864 && windDir > 847 )
312
+ else if ( windDir <= 879 && windDir > 834 )
311
313
return 337.5
312
314
else
313
315
return simDirection
314
316
}
317
+
318
+ //% weight=20 blockId="weatherbit_windDirRaw" block="wind direction (raw)"
319
+ export function windDirectionRaw ( ) : number {
320
+ return windDir
321
+ }
315
322
316
323
/**
317
324
* Read the instaneous wind speed form the Anemometer. Starting the wind
0 commit comments