Microbit Robot Trapped in Circle
Table of Contents
Microbit Robot:
Microbit Robot Trapped in Circle- This robot will stay trapped inside this circle forever. It will never be able to escape this circle. From now on, this circle is the robot’s entire world. You cannot only lock someone in a jail; you can also trap them in a circle like this. This poor Robot will cry and sigh out of frustration, but this is now its fate. It has to discharge its battery within this circle.
You know, a computer is an obedient servant; we give it commands, and it follows them. I have given commands to this robot in such a way that it will never cross this circle. Even if it somehow manages to cross the circle, it will be forced to come back because it has no other option. Its brain, which is the controller, will not let it leave this circle under any circumstances. Let me explain how it works.
You and I know this is a circle, but the robot has no idea. Even if it were a square, rectangle, or any irregular shape, the robot would not be able to cross the boundary. That is because I have clearly instructed the controller that it should never cross the black line, and if by mistake, due to speed, it does cross the line, it must come back inside the circle.
This robot has two IR sensors on its bottom side, which help us easily detect white and black colors. In the previous article, I programmed this robot in such a way that it would only follow the black line, turning it into a line follower robot.
Anyway, when both IR sensors detect the black line at the same time, the robot moves backward and then tries a different direction.
And if one IR sensor detects the line, the robot turns left or right.
This robot is not doing all this on its own; I have given it commands that control its motors through the microcontroller “Micro:bit”. Whenever it encounters a black line, the controller stops the robot from crossing it.
Now, let’s take a look at the code.
Amazon Links:
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
Micro:bit Robot Programming:
First, go to the website Makecode.microbit.org, and start a new project. Then, go to the extensions and search for the ‘cutebot.’ Add it to your project. In my previous getting started tutorial on the Microbit, I explained all of this in detail, so I highly recommend you read that article.
Anyway, first, we start with the “on start” event, which runs only once when we start or reset the Micro:bit. This is like the “void setup” function in the Arduino IDE. I defined a variable named “counter”.
If you want to create a new variable, just click on “Variables Category” then click on “Make a Variable”, type the name of your variable, and click “OK”. Your variable will be created, and then you can use it in your code.
Next, I turned on the built-in speaker of the Microbit. You can find this block in the “Music Category”.
With this block, you can turn the built-in speaker on or off. As you can see, there are many blocks in the Music category that you can use according to your needs.
Then, we have the “forever” event, which is like the “void loop()” function in the Arduino IDE. Our main code runs in the “forever” event.
If both IR sensors detect the black line at the same time, the robot’s left and right motors will move in reverse at a set speed. Then there is a 1-second delay. After that, we increment the counter and turn on the built-in speaker. The robot then turns to one side for half a second.
These two conditions mean that if any IR sensor detects the black line, the robot will turn.
This condition means if there is no black line, the robot will move forward.
Finally, the last condition means if the robot has detected the black line 5 or more times, reset the counter to zero. The robot will keep turning in a circle, move forward a little, turn the headlights on and off, then stop and play a yawning sound, and turn off the speaker. Then, everything will repeat. So, that is how to trap a robot inside a circle.
Watch Video Tutorial: