Adds soundLevel

To be replaced by pxt-common-packages when DAL is updated.
This commit is contained in:
Juri 2020-08-23 19:20:45 +02:00
parent 8700349d68
commit 862647f842

View File

@ -77,4 +77,27 @@ namespace input {
export function runningTimeMicros() {
return control.micros();
}
/**
* gets the level of loudness in 0-100%
*/
//% blockId="loudness"
//% block="Loudness"
export function soundLevel(): number {
let value = 0
let max = 0
let min = 1023
for (let index = 0; index < 32; index++) {
value = pins.analogReadPin(AnalogPin.MIC)
if (value > max) {
max = value
} else if (value < min) {
min = value
}
}
value = (max - min) * 977 / 10000
return value
}
}