This is the final tutorial using the light sensors to either avoid or follow light. We will use all of the previous code that we’ve developed, add two methods lightMove() and setReference(). It will be possible at select each mode by pressing button A or button B.
First off I would like you to copy and paste the code from the previous example into Mu as a new project. Then delete the program loop, delete leftLineSenor, rightLineSensor variables and the lineDetector() method.
Now we need to create our method lightMove(), this method will have an argument called direction that can be a 1 or a 0. 1 for avoid and 0 for follow. The method is shown below;
I also added the following method called setReference();
and finally I added the following variable declaration under sensorSelect = pin16
So what does it do. I will describe the setReference() method first; this method will be invoked when Bit:Bot is started. It takes readings from both the left and right sensors adds them together and divides by 2. This calculates an ambient light level that we will use as the reference level.
Now to the lightMove() method; this method will create an average value from the amount of light coming into the sensors, if the amount of light is less than the reference level + 10 then the robot will not move and the following if statement will be evaluated, if the light is bright it will light up the LEDs a cyan colour to indicate that the light levels are too bright and the room needs to be darker for the robot to perform properly. If the room is dark enough the lights on the robot will light a pinkish colour and the robot will wait for a light to be shone on it before it starts to move.
NOTE: elif means else if. You use this instead of the else statement if you have another condition to be evaluated.
If the ambient light is greater than the reference level then the robot will need to do something. Firstly the robot needs to decide if the light is coming from its front or its side. It uses the following if statement to evaluate this;
This looks at the light coming into each sensor and if the values are within 50 of each other the robot decides that the light is coming directly from its front and either moves forwards in follower mode or backwards in avoid mode and lights the LEDs a yellowish colour.
Otherwise the robot will turn towards the light in follower mode and away from the light in avoid mode. The LEDs will be red for avoid and green for follow.
The LEDs can be one of five colours these are shown below;
- Cyan – Room too bright robot will not respond the different light levels
- Pink – Robot will respond to different light levels
- Yellow – robot moving forward or backwards because light is originating in front of the robot
- Green – robot is moving left or right towards the light source
- Red – robot is moving left or right away from the light source
Now we need a way to choose which mode to put the robot in. For this we will use the A and B buttons. Please note that the A and B buttons share the same pins as the IR sensors. When using Bit:Bot in this mode make sure that its on a dark or evenly coloured surface so the buttons aren’t activated by the IR sensors.
The below code is added to allow for this;
The above code will set the ambient light levels by calling the setReference() method and assigning the returned value to refLevel. The program loop is entered where the button presses are retrieved and the button with the most presses will dictate which mode to put the robot in. mode 1 is for light follower and mode 2 is for light avoider. If no buttons are pressed then the robot will stay in its current mode. The Micro:Bits LEDs will display A for avoider, F for follower and N for no mode selected. lightMove() method will be called with either a 1 or 0 in the argument field to decide if its in follow or avoid mode.
The mode variable is declared under the refLevel variable near the start of the program.
The complete program listing is shown below;
The above solution isn’t the most elegant however it works and it gave us an opportunity to cover elif statements, button presses, displaying letters on the Micro:Bit and to use the code developed from the previous examples.
Now upload the code to your robot, put it in a room that’s not too bright and use a torch to make the robot follow or avoid the light. Play with the code and see how Bit:Bot behaves.
You’ve done really well and should understand how to program the Bit:Bot to do stuff. All of these examples can be improved and expanded to give Bit:Bot more functionality. You can purchase an ultrasonic sensor for Bit:Bot from the 4Tronix website that will allow you to develop an obstacle sensing robot. Use the experience you’ve gained from these examples and expand on by coming up with your own ideas and coding them. Micro:Bits are good bits of hardware to cut your teeth with coding, when you get more experience maybe you could have a look at the Arduino family of products. This will let you do much more with many different types of micro-controllers from simple 8-bit AVR type controllers to more complex 32-bit ARM Cortex and even the Atheros AR9331 (Arduino Yun) that can run a small distro of the Linux operating system.
I have put the code files below, if you have any comments, questions or code it can be posted in the comments section below.
The below zip archive contains the code file from this example.