Here we will use the HC-SR04 Ultrasonic sensor to give Bot:Bot eyes. These sensors can be purchased on the internet and I suggest you buy one from 4Tronix as they have mounted it on a breakout board to get over the voltage issues (the sensor runs on 5v logic where as the Micro:Bit runs on 3v logic).
I have created a simple path finding algorithm as an introduction to this type of programming. One of the problems that we encounter is that if we explicitly tell Bit:bot what to do every time it encounters an obstacle it will move in a predictable way that will mean that the robot will either get stuck or go around in circles in certain scenarios. To combat this we will use random delays when the robot turns and random number of turns before changing direction.
I opened up the previous project and created a new tab called _03_PathFinder and added the following code;
I then added the following line to the loop() method in the _00_programLoop tab;
Remember that your Neopixels won’t work unless you carry out the Neopixel modification and you must add all libraries if you haven’t already links are in the code.
You can download the code by clicking Bitbot_Lib_PathFinder or from the resources page.