From 0c360e9a5d64198beb145df76ac5398819a84308 Mon Sep 17 00:00:00 2001 From: Amerlander Date: Sat, 22 Aug 2020 10:16:42 +0200 Subject: [PATCH] add Loudness Block --- libs/core/input.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/libs/core/input.ts b/libs/core/input.ts index 001f07a3..3d8c7a78 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -77,4 +77,29 @@ namespace input { export function runningTimeMicros() { return control.micros(); } + + + /** + * gets the level of loudness in 0-100% + */ + //% blockId="loudness" + //% block="Loudness" + + export function loudness(): number { + let value = 0 + let max = 0 + let min = 1023 + for (let index = 0; index < 32; index++) { + value = pins.analogReadPin(50) + if (value > max) { + max = value + } else if (value < min) { + min = value + } + } + value = (max - min) * 977 / 10000 + return value + } + + }