From 862647f842f995733210a705373f40711a5eb419 Mon Sep 17 00:00:00 2001 From: Juri Date: Sun, 23 Aug 2020 19:20:45 +0200 Subject: [PATCH] Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. --- libs/core/input.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/libs/core/input.ts b/libs/core/input.ts index 001f07a3..8f4d5529 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -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 + } + }