Activity 1 - Reading Sensors A Day in the Life CTE Enrichment Grades 3-5 mblock Programs Using the Sensors Computer Science Unit This tutorial teaches how to read values from sensors in the mblock IDE. There are also tutorials which specifically focus on how to use the Makeblock sensors, for example the light sensor, the ultrasonic sensor and the line follower sensor. The easiest way to find out what value a sensor is giving is to have a Sprite (panda by default) say it. In the Looks Scripts, there is a say block: 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: The line follower sensor: And the on-board light 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:
Computer Science Unit 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? Construct Your Dreams!
Activity 2 - The Light Sensor Computer Science Unit The light sensor on the mcore board measures light the lighter it is, the higher the value, the darker it is, the lower the value. The range of the light sensor is 0-1023. In this lesson, we are going to program the mbot to play a note if it senses there is a lot of light, and play a different not if it senses there is not a lot of light. For this, we will need the IF block. When we use an IF in programming, there are 3 parts to what we write: 1. The IF itself 2. The condition 3. The code that is run, if the condition is met. So we need to write something like this: 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: If (the value on the light sensor IS LESS THAN 500) ---- Play sound. Writing this in mblock code, we need: 1. An if
Computer Science Unit 2. A condition. The condition in this example is made up of 2 parts. We need the green less than block in the Operators Script: 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: And when dropped in, it looks like this: 3. Finally, we need some code that will be run if the condition is met: When this code is put together, it looks like this: 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:
Computer Science Unit So, what if I wanted to play note C4 in the dark, but note D4 in the light. For this, we could use an if/else Control block: 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. Construct Your Dreams!
Activity 3 - The Ultrasonic Sensor Computer Science Unit The ultrasonic sensor measures distance. One of the eyes transmits a sound, and the other waits for the echo of the sound to return. From the time this process takes, the distance of the object from the sensor can be calculated. The ultrasonic sensor has a range of 3-400cm. If an object is outside this range, the sensor will return a value of 400. 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
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? Construct Your Dreams!
Activity 4 - The Line Follower Computer Science Unit 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: A dark surface only reflects a little light back to the receiver: In mblock there are 2 blocks associated with the line follower:
Computer Science Unit The first block will return a number between 0 and 3 based on the following values: The second block will return either true or false. Using the Line Follower block to program: If we want to have our robot follow a black line, we have the following pseudocode: If( line follower = 0) ---- Go forward Else if (line follower = 1) ---- Turn Left Else if(line follower = 2) ---- Turn Right Else if(line follower = 3) ---- Aaargh!! Help!! Where is the line? So let s now put that into mblock code:
Computer Science Unit 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 : Now the robot successfully follows the line. Using the Line Follower (port)(side)is(black/white) block to program: Using this block: If((Right is black) AND (Left is black)) ---- Go forward Else if((right is white) AND (Left is black)) ---- Turn Left Else ((Right is black) AND (Left is white)) ---- Turn Right
Else ((Right is white) AND (Left is white)) ---- Aaargh!! I m lost AGAIN?!? Computer Science Unit So for this we need an and block to combine the 2 conditional statements: So the final code looks like this: 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. Construct Your Dreams!
Activity 5 - The Ultrasonic Theremin Computer Science Unit A theremin is a musical instrument that plays notes according to where the musician s hands are. It has been around since about 1929 and this tutorial looks at how to make an mbot Theremin, using the ultrasonic sensor. If an object is less than 10cm away ---- Play a high note Else If an object is less than 20cm away ---- Play a lower note Else If an object is less than 30cm away ---- Play a lower note Else If... The start of my code looks like this: 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? Construct Your Dreams!
Activity 6 - Sumo mbot Computer Science Unit Get your mbot ready for battle in the ancient Japanese art of sumo wrestling. Program your robot to search for your opponent (with the ultrasonic sensor), and when found, attack. NOTE: This lesson is intended for use with 2 mbots fighting on a table. Please note that the mbots may fall off the table. Appropriate measures must be taken to ensure that no damage is caused while engaging in this activity. Remember the values that are returned by the line follower sensor: 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:
Computer Science Unit 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? Construct Your Dreams!