pxt-calliope/docs/reference/input/on-sound.md
Juri Wolf 5f7a8e5301
Updates for V4 (#197)
* update yotta defaults for 16kb devices

* refactor deprecated blocks

* updates for button events

* update button events

* update refference

* update docs

* update docs

* update button event blocks

* update docs

* update block id
2022-08-10 09:36:19 -07:00

1.1 KiB

on Sound

Run some code when the microphone hears a sound.

input.onSound(DetectedSound.Loud, function () {})

The microphone will detect sounds that are quiet or loud. You can have the microphone detect a sound at a certain level and run some code in and event when it hears the sound. There are two sound ranges you can detect for: loud or quiet.

~ reminder

works with micro:bit V2 only image

This block requires the micro:bit V2 hardware. If you use this block with a micro:bit v1 board, you will see the 927 error code on the screen.

~

Parameters

  • sound: the type of sound to detect: loud or quiet.
  • handler: the code to run when a sound is heard.

Example

Show an icon animation when the microphone detects a sound.

input.onSound(DetectedSound.Loud, function () {
    basic.showIcon(IconNames.Square)
    basic.showIcon(IconNames.SmallSquare)
    basic.showIcon(IconNames.SmallDiamond)
    basic.clearScreen()
})

See also #seealso

sound level, set sound threshold

microphone