First watch the video to follow along the build, feel free to pause the video, zoom in, to look closer at the pieces.
Forever checking if the button is pressed, if the distance is also under 200cm
It'll start playing sound at the volume of the variable [volume] - more on that later
Then it'll play at the pitch of the variable [height]
To make it a little more interesting, I also added some light effects - more on that later
If nothing is being pressed, it will stop all sounds.
There are three variables in total: [height], [volume], and [actual height]
You don't really need the [actual height] one, because that's just for you to monitor the actual height when playing,
So the code for that is quite simple. Just forever update the actual distance to the distance sensor.
The [height] controls the pitch, and I find that multiplying it by 3 is the sweet spot, but it can make it more inaccurate. Therefore, playing a less varied set of pitches.
I wanted the volume button to be the opposite of the natural way. Usually, the harder you press down, the louder it gets. But because the LEGO SPIKE hub isn't that loud, playing at full volume is actually the sweet spot.
How this works is that pressure in newtons is maxed at 10 (at least in SPIKE), so multiplying by 10 makes the max 100%. The minus 0 and plus 100 both make it a negative number (to make it quieter when pressing harder)
When the button is pressed, it shows the pressure with a progress bar on screen.
Also, the power button changes with the distance.
If the button isn't pressed, nothing will show.