Treasure hunting with micro:bits!
![treaure_hunt3.png](/assets/media/images/treaure_hunt3.2e16d0ba.fill-931x590.png)
Can you use a micro:bit to find another micro:bit?
You can, and we put it to work in a fun treasure hunt activity for a children's birthday party!
How it works:
When a micro:bit receives a radio message, it also measures the strength of the signal that's been received. We can use that signal to estimate how far away we are from another micro:bit.
In this example, we've set up one micro:bit to act as a beacon. The beacon was programmed to constantly send out a radio message. The message wasn't really important, because the signal strength is what we were really interested in, so we just sent the number 1 as fast as we could. We also had the beacon flash once every 5 seconds to make the project a bit more visual. The beacon was then put into a treasure chest and hidden in the garden.
You can see our beacon code here.
The rest of the micro:bits were programmed as receivers. When they received the radio messages, we ignored the message but looked at the signal strength. Now, distance isn't the only factor that affects signal strength - radio waves often like to bounce around and play tricks on us - but it's useful enough for us to be able to figure out if we are getting closer or further away.
We wanted to graph that signal strength on the display of the micro:bit, but the signal strength can be a bit noisy, or it can vary quite a bit from radio message to radio message. So to smooth things out, we are going to sample 10 radio messages in a row and create an average of the signal strength. This will smooth out the signal but will create a little bit of lag in the system - although, not enough to matter in our example here.
To store the messages, we create a list with 10 spaces in it. Every time we receive a new message, we record the signal strength in the list using the insert at beginning block, and remove the oldest entry in the list with the remove last value from list block. This means that we will always have the last 10 signal strength values in the list. We can then add them all together and divide the total by the list size to work out the average value.
![treasure_hunt_code_snippet.png](/assets/media/images/treasure_hunt_code_snippet.width-1200.png)
Finally, we graph the average value to the microbit display. Because signal strength is a negative value (closer to zero means higher), we use a map block to map the value back to a positive number between 0 and 100 before we graph it. The signal strength is displayed as a bar graph on the micro:bit display - the more that are lit up, the closer you are to the beacon.
You can see our receiver code here.
Now you can have a lot of fun and games by hiding the beacon, and handing out a bunch of receivers. You could even modify this further to have multiple beacons in different radio groups, and use buttons for the receivers to change which radio group they are a part of. Let us know if you give it a go - we would love to hear about how you've extended the code to create even more fun and games!
Click here to Contact Us.