MBlock Using The Sensors Programs
MBlock Using The Sensors Programs
Of course, if we ask the panda to say “Hello!”, the panda is going to say “Hello!”.
Fortunately, we can replace this text with the name of whatever sensor we wish to
read. This is for the ultrasonic sensor:
For the plug-in sensors, you have to make sure the port selected is the same port as
the port you have plugged your sensor into.
Now, you can click this block and it will tell you the value of the sensor. To make it
continually update, you can put this block in a forever loop:
Challenge:
1. Can you find a way to display the values of different sensors at the same time?
2. Can you find out the range of possible values for all the sensors?
If (condition)
---- Do this code
The first thing I want to do in this lesson is to play some sound if it is dark. So the
condition is “it is dark”, and the code is “play sound”:
If (it is dark)
---- Play sound.
Remember the light sensor returns a value between 0 and 1023, where high
numbers represent a light environment. I would say a light sensor reading of less
than 500 represents a dark environment. So we can write our code as:
1. An “if”
And we need the light sensor “block” from the “Robtots” scripts:
The condition is “the light sensor is less than 500”, so we can put the light sensor
block in the left side of the “less than” block and write “500” in the right side. When
putting the blue block inside the green block, make sure the left side of the blue
block is aligned with the appropriate space of the green block. When the space in
the green block has a white outline it is ready to receive the blue block:
3. Finally, we need some code that will be run if the condition is met:
When this code runs, it will test the condition once and then stop running. If we want
it to continue to play music while dark we can add a forever loop. Also, remember to
include a descriptive comment:
Challenge:
1. Develop a program where the robot runs forward in the light and stops in the dark.
2. Develop a program where the robot dances in the dark (be creative with your
dance moves) and rests in the light.
3. Develop a program where the robot goes at different speeds depending on the
light – the lighter it is, the faster it goes.
4. Develop a program that plays lots of different notes – the more light sensed, the
higher the note.
In this lesson, I want to get the robot to go forward until it is close to an object, then
turn away from the object and go off in a new direction:
Forever:
---- If an object is detected
---- ---- Turn to a new direction
---- Else
---- ---- Go forward
And in code, that looks like this:
On testing this, I found that the robot wasn’t turning enough when it encountered an
object, so I added a wait command after the turn. Now I would like to get it to turn in
mBlock: Using the Sensors - User Guide
Computer Science Unit
a random direction – sometimes left and sometimes right. Fortunately, there is an
Operator block that I can use to do just that:
The robot can only turn left or right, so I am going to choose a random number
between 0 and 1. If the random number is 0, then the robot will turn left. If the
random number is 1, then the robot will turn right. So then the turning behavior will
look like this:
Challenge:
1. Can you put the code above together to make the completed program?
2. Can you write a program that follows an object? So if the object is too close, the
robot goes backwards, if the object is far away, it goes forward, and if the object is
not near or far, the robot stops.
3. Can you change your program from question 2, to make the robot move at
different speeds?
The line follower has 2 sensors which can detect a white surface (within the range of
1-2cm). It works by emitting IR (InfraRed) light and recording how much is reflected
back. If a lot is reflected back, it can be deduced it is close to a white surface. If a
little is reflected back, it can be deduced that the surface is black, or the sensor is
not near a surface.
A light surface reflects a lot of infrared light back to the receiver:
And it doesn’t work – the robot often goes off the track. This is because this program
is run from the mBlock IDE. Remember it takes time for the signals and commands
to be sent from the robot to the computer and back again. In this case, that delay
interferes with how the robot performs the line following. We need to upload this
program onto the board to reduce this delay. Then the program will work. For the
mBot, we use the “mBot program” block, go into Arduino mode and “Upload to
Arduino”:
Challenge:
1. The robot moves does not turn smoothly. Why is that? Can you fix it?
2. Can you write a program so the robot moves around a (white) table in a
random fashion without falling off? (Remember the line follower sensor detects
if light is reflected – or not reflected)
3. Can you write a program that follows a line AND avoids objects – if an obstacle
is detected, the robot should stop the motors and wait for the object to be
removed.
Challenge:
1. Can you finish off the program? Make it as long or as short as you want, and then
get playing your theremin.
2. The higher notes play more quickly than the lower notes. Why is that? Can you
change the program so that the notes all play at the same speed?
So there are 2 different sensors which affect the behavior of the mBot. This can be
displayed in the following table:
Now we have the actions clearly defined, we can write some pseudocode for our
program:
Forever {
---- While (the robot has not detected an edge) {
---- ---- If (an enemy is detected)
---- ---- ---- Charge
---- ---- Else
---- ---- ---- Move forward
---- }
---- Turn
}
Transferring this into code looks like this:
Challenge:
1. Use the LEDs and the buzzer to signal the different actions.
2. Have a competition. Can you change the code to make the mBot do better in the
competition?