Hi
I’ve gotten a task that I have no idea how to do.
The task is to get the microbit, when connected to a speaker, to play a frequency (hz) that goes up and down like a sine wave /siunusoid so it souns like a siren.
I’ve tried using map under math to convert, but it doesn’t work.
I am completely lost as to how to solve this task.
Any suggestions?
The second part of the task is to change the frequency down/up using the A/B buttons. Blank there too.
hello sorry for late response. i just built something like this. so basically, with something as simple as this you can just calculate the sine values right away. and just plug them into the pitch pin block. i’m making a kind of synthesizer. so this is what the code looks like.

(i hope the image works)
so you initialize the frequency of the siren with carrier frequency. the frequency of the siren going up and down is the modulator frequency. and the how high the siren goes up and down with modulationAmount.
then the math is:
outputFrequency = carrierFrequency + modulationAmount * sin(modulationFrequency * running time (ms) / 1000)
Important:
- make sure that the looping code is inside the special every 1 ms loop instead of a normal forever loop.
- make sure that if you are using microbit v1 with pin output, that you use analog pitch (freq) for (0 ms) (this will keep playing the frequency until the pin is updated again)
cool thing:
by using the running time milliseconds to keep track of time, we don’t have to worry about updating time variables which might cause problems.
i hope this helps!