Microduino MIXC104E KIT-mCookie-Education User Manual MIX4 Book 02202018 SS indd

Microduino Inc. KIT-mCookie-Education MIX4 Book 02202018 SS indd

Contents

User manual Part 2

156
Project Worksheet
Complete the worksheet below to your best abilities.
1. A solution.
2. List all the parts needed for the solution. What is the purpose of each part?
3. What is a simple explanation of the logic for the solution?
4. Create a ow chart of the solution.
5. How do you assemble and connect the modules for the solution?
6. Congure blocks to relate to the ow chart.
Use two Motors to move the car. Use the Joystick to receive input from the user to go straight, turn left or right.
Adjust the speed of the Motors accordingly.
157
Project Worksheet - Answers
2. List all the parts needed for the solution. What is the purpose of each part?
1x mCenter+ - control the project, connect sensors and trinkets to project, and supply power.
1x Hub Connector Cable - connect sensors and trinkets to the mCenter+.
1x USB Cable - connect mCenter+ to a computer for charging and programming the project.
2x Motor - to move the RC car.
1x Motor Controller - controls and drives the attached motors.
1x Joystick - to the direction of the RC car.
2x Wheel - attach to motor.
158
Project Worksheet - Answers
3. What is a simple explanation of the logic for the solution?
Use two Motors to move the RC car.
Use the Joystick to input the direction to move the car (left, right, up, down).
Depending on the Joystick’s direction being held down:
Up - move car forward (both motors rotating forward).
Down - move car backward (both motors rotating backwards).
Left - move car left (motor 1 rotating forward slowly, motor 2 rotating forward fast).
Right - move car right (motor 1 rotating forward fast, motor 2 rotating forward slowly).
None - stop car (both motors turned o).
For Left and Right directions, the dierence in motor speeds causes the car to turn.
Turning Left Turning RightForward
159
Project Worksheet - Answers
4. Create a ow chart of the solution.
Start
Set variable for motor speed
Is Joystick "Up"
being pressed?
Is Joystick "Down"
being pressed?
Is Joystick "Left"
being pressed?
Is Joystick "Right"
being pressed?
Set motors to move
RC car forward
Set motors to move
RC car backward
Set motors to turn
RC car left
Set motors to turn
RC car right
Set motors to off
(stop RC car)
Short delay
Loop
forever.
True
True
True
True
False
False
False
False
160
Project Worksheet - Answers
5. How do you assemble and connect the modules for the solution?
1. Stack the Motor Controller onto mCenter+.
2. Connect a Motor to connector “1A1B” on the Motor Controller.
3. Connect a Motor to connector “2A2B” on the Motor Controller.
4. Connect the Joystick to pin A0/A1.
5. Insert one end of the USB cable into mCenter+ and connect the other to a computer.
D2/D3
D8/D9
I2C A2/A3
I2C
D4/D5
A6/A7
A0/A1
I2C
D6/D7
161
Project Worksheet - Answers
6. Congure blocks to relate to the ow chart.
Start
Set variable for motor speed
Is Joystick "Up"
being pressed?
Is Joystick "Down"
being pressed?
Is Joystick "Left"
being pressed?
Is Joystick "Right"
being pressed?
Set motors to move
RC car forward
Set motors to move
RC car backward
Set motors to turn
RC car left
Set motors to turn
RC car right
Set motors to off
(stop RC car)
Short delay
Loop
forever.
True
True
True
True
False
False
False
False
162
The Script Block Locator
Loop.
Click Green Flag to activate script.
Short delay before looping.
Set the variable for the speed of the motors.
Check if the Joystick (pin A0) “Up” key is being pressed.
Check if the Joystick (pin A0) “Right” key is being pressed.
Check if the Joystick (pin A0) “Left” key is being pressed.
Check if the Joystick (pin A0) “Down” key is being pressed.
Set motors to move car forward.
Set motors to move car backward.
Set motors to turn car left.
Set motors to turn car right.
Otherwise (no key being pressed), turn motors o.
Control
Events
mCookie
Data
Operators
(Create “motor_speed” variable
rst in the variable tab.)
163
Click the green
ag to activate
the script.
1. Press and hold the Joystick in the Up,
Down, Left or Right position.
2. The Motors will turn accordingly to move
the RC car.
Testing the Program
Important:
Ensure that mCenter+ is on. Flip the switch on mCenter+
to turn it on. An LED on mCenter+ will light up to indicate
power is being supplied.
Ensure you have selected the Serial Port and ashed the
special rmware (mCookie) rst before testing (this only
needs to be done once unless the rmware is overwritten).
Ensure you have selected the Serial Port and connected
before testing.
Flash rmware
Select Serial Port
Testing:
Note: If the program is working correctly and you wish to use the program without being connected to your computer. Switch to Arduino mode
(top right), then “Flash Program” (this overrides the special rmware, you will need to reash to use Live mode).
164
Create a Structure
Create a structure for the project using building blocks, paper, or other crafting materials.
165
Project Challenge
How can you modify the speed in which the car moves?
Adjust the turning radius (quicker turning).
Add another Joystick input (“Center” pressing).
Make this input to cause the car to spin (motor 1 forward, motor 2 backward).
166
Review
Motor Controller - controls and drives attached motors. Can control up to 2 motors.
Motor - converts electrical energy into mechanical energy in the form of torque.
Motor - Set Speed block - set the speed of a motor.
Wheel - attaches to the Motor.
Joystick - input module (sensor) which can detect the position of the stick (up, down, left, right
,
center (press)).
Joystick State block - returns TRUE if the position of the stick state matches the checking state.
Otherwise returns FALSE.
167
Expanding the Project
Research
Write down a way of expanding this project. Create it on your own.
Research more about the history of RC cars. Besides being controllable directly by a wired
Joystick, what other methods are used to control RC cars?
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
168
Safety Car
MIX 4 - Project 7
(Old Cards: MIX4B-02)
169
A collision avoidance system is an automobile safety system designed to reduce the severity of a collision.
It is also known as a precrash system, forward collision warning system, or collision mitigating system. It
uses radar (all-weather) and sometimes laser (LIDAR) and camera (employing image recognition) to de-
tect an imminent crash. GPS sensors can detect xed dangers such as approaching stop signs through a
location database.
Once the detection is done, these systems either provide a warning to the driver when there is an im-
minent collision or take action autonomously without any driver input (by braking or steering or both).
Collision avoidance by braking is appropriate at low vehicle speeds (e.g. below 50 km/h), while collision
avoidance by steering is appropriate at higher vehicle speeds. Cars with collision avoidance may also be
equipped with adaptive cruise control, and use the same forward-looking sensors.
Source: https://en.wikipedia.org/wiki/Collision_avoidance_system
About Collision Avoidance
170
Review of Modules
Usage
The Line Finder module is able to detect reectivity or gray level of a surface. In gen-
eral, darker or less reective surfaces produces a higher value. While a lighter or more
reective surface produces a lower value. It must be connected to a pin with an
“A” prex (A0/A1, A2/A3, A6/A7).
Line Finder
Analog Read block
This block returns the value that the Line Finder detects. A value between 0 and 1023 is
returned.
Pin that Line Finder
is connected to.
0 1023
Darker,
less reective surfaces.
Lighter,
more reective surfaces.
Surface Reectivity
Value Returned
171
Review of Modules
A Motor is a electrical machine which converts electrical energy to mechanical energy. It
is capable of rotating clockwise and counter clockwise. It is controlled by a Motor Con-
troller. A Motor Controller can control up to two Motors. A Wheel can be attached to the
Motor.
Motor
Motor Controller Wheel
Motor - Set Speed block
This block sets the speed of the motor. Positive values turns the Motor in one direc-
tion. While negative values turns the Motor in the opposite direction. A value of 0
stops the Motor. Values between -255 and 255 are accepted. 255 is maximum clock-
wise speed. -255 is maximum counter-clockwise speed.
Motor - Brake block
This block sets the speed to 0 to stop the Motor.
Motor to congure.
Motor to congure.
Speed to set.
172
Review of Blocks
Addition Block
This block returns the sum of the left input and right input.
Division Block
This block returns the value of dividing the left input by the right input.
Multiplication Block
This block returns the product of the left input and right input.
Subtraction Block
This block returns the value of subtracting the left input by the right input.
Less Than Block
This block returns TRUE if the left input is less than the right input.
Equal Block
This block returns TRUE if the left input is equal to the right input.
Greater Than Block
This block returns TRUE if the left input is greater than the right input.
173
Review of Concepts
Creating a Variable in Scratch
Click on the “Variable” category.
Then click on the “Create variable...” button.
Name your variable.
174
Review of Concepts
Creating a Variable in Scratch (continued)
Several new blocks will appear which allows
access and modication of the new variable.
Variable Value block
Variable Set block
Variable Change block
This block returns the current value of the variable.
This block sets the value of the variable. It will overwrite the current value.
This block changes the variable value by adding the input value (negative values or blocks are accepted).
input value
175
The Problem
How can we create an autonomous car which moves forward until it reaches an
object, then moves backwards until it reaches an object, then repeats?
176
Project Worksheet
Complete the worksheet below to your best abilities.
1. A solution.
2. List all the parts needed for the solution. What is the purpose of each part?
3. What is a simple explanation of the logic for the solution?
4. Create a ow chart of the solution.
5. How do you assemble and connect the modules for the solution?
6. Congure blocks to relate to the ow chart.
Use two Line Finders to detect obstacles (objects) in the front and back of the car. Use two motors to move the
car.
177
Project Worksheet - Answers
2. List all the parts needed for the solution. What is the purpose of each part?
1x mCenter+ - control the project, connect sensors and trinkets to project, and supply power.
2x Hub Connector Cable - connect sensors and trinkets to the mCenter+.
1x USB Cable - connect mCenter+ to a computer for charging and programming the project.
2x Motor - to move the RC car.
1x Motor Controller - controls and drives the attached motors.
2x Wheel - attach to motor.
2x Line Finder - detect objects in the front and back of the car.
178
Project Worksheet - Answers
3. What is a simple explanation of the logic for the solution?
Use two Motors to move the RC car.
Use two Line Finders (one in front, one in back) to detect objects in the way.
Move forward until an object is detected (Line Finder in front value < 700).
Move backward until an object is detected (Line Finder in back value < 700).
Repeat.
179
Project Worksheet - Answers
4. Create a ow chart of the solution.
Start
Set variable for motor speed
Short delay
Line Finder (front)
doesn’t detect an object
Line Finder (back)
doesn’t detect an object
Set motors to move
RC car forward
Set motors to move
RC car backward
Short delay
Short delay
True
True
False
False
Loop
forever.
180
Project Worksheet - Answers
5. How do you assemble and connect the modules for the solution?
1. Stack the Motor Controller onto mCenter+.
2. Connect a Motor to connector “1A1B” on the Motor Controller.
3. Connect a Motor to connector “2A2B” on the Motor Controller.
4. Connect a Line Finder to pin A0/A1.
5. Connect a Line Finder to pin A2/A3.
6. Insert one end of the USB cable into mCenter+ and connect the other to a computer.
D2/D3
D8/D9
I2C A2/A3
I2C
D4/D5
A6/A7
A0/A1
I2C
D6/D7
181
Project Worksheet - Answers
6. Congure blocks to relate to the ow chart.
Start
Set variable for motor speed
Short delay
Line Finder (front)
doesn’t detect an object
Line Finder (back)
doesn’t detect an object
Set motors to move
RC car forward
Set motors to move
RC car backward
Short delay
Short delay
True
True
False
False
Loop
forever.
182
The Script Block Locator
Loop.
Click Green Flag to activate script.
Short delay before looping.
(Create “motor_speed” variable
rst in the variable tab.)
Set the variable for the speed of the motors.
Repeat until Line Finder (A0)
in front detects an object.
Repeat until Line Finder (A2)
in back detects an object.
Short delay before looping.
Short delay before looping.
Set motors to move car forward.
Set motors to move car backward.
Control
Events
mCookie
Data
Operators
Arduino
183
Click the green
ag to activate
the script.
1. The car will move forward until the Line Finder (front) detects an object.
2. The car will then move backward until the Line Finder (back) detects an object.
3. Repeat.
Testing the Program
Important:
Ensure that mCenter+ is on. Flip the switch on mCenter+
to turn it on. An LED on mCenter+ will light up to indicate
power is being supplied.
Ensure you have selected the Serial Port and ashed the
special rmware (mCookie) rst before testing (this only
needs to be done once unless the rmware is overwritten).
Ensure you have selected the Serial Port and connected
before testing.
Flash rmware
Select Serial Port
Testing:
Note: If the program is working correctly and you wish to use the program without being connected to your computer. Switch to Arduino mode
(top right), then “Flash Program” (this overrides the special rmware, you will need to reash to use Live mode).
184
Create a Structure
Create a structure for the project using building blocks, paper, or other crafting materials.
185
Project Challenge
Modify the script to use “If Else” blocks instead of “Repeat Until” blocks.
Add a third condition check to stop the car if both Line Finders detect an object.
186
Review
Motor Controller - controls and drives attached motors. Can control up to 2 motors.
Motor - converts electrical energy into mechanical energy in the form of torque.
Motor - Set Speed block - set the speed of a motor.
Wheel - attaches to the Motor.
Line Finder - detects the reective surface or distance of an object.
Analog Read block - returns the value of the reective surface from the Line Finder sensor.
187
Expanding the Project
Research
Write down a way of expanding this project. Create it on your own.
Research more about obstacle avoidance systems in cars.
What are some interesting techniques that are used in these systems?
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
188
Delivery Truck
MIX 4 - Project 8
(Old Cards: MIX4B-03)
189
Delivery is the process of transporting goods from a source location to a predened destination. There are
dierent delivery types. Cargo (physical goods) are primarily delivered via roads and railroads on land,
shipping lanes on the sea and airline networks in the air. Certain specialized goods may be delivered via
other networks, such as pipelines for liquid goods, power grids for electrical power and computer net-
works such as the Internet or broadcast networks for electronic information.
The general process of delivering goods is known as distribution. The study of eective processes for
delivery and disposition of goods and personnel is called logistics. Firms that specialize in delivering com-
mercial goods from point of production or storage to point of sale are generally known as distributors,
while those that specialize in the delivery of goods to the consumer are known as delivery services. Post-
al, courier, and relocation services also deliver goods for commercial and private interests.
Source: https://en.wikipedia.org/wiki/Delivery_(commerce)
About Deliveries
190
Review of Modules
Usage
The Line Finder module is able to detect reectivity or gray level of a surface. In gen-
eral, darker or less reective surfaces produces a higher value. While a lighter or more
reective surface produces a lower value. It must be connected to a pin with an
“A” prex (A0/A1, A2/A3, A6/A7).
Line Finder
Analog Read block
This block returns the value that the Line Finder detects. A value between 0 and 1023 is
returned.
Pin that Line Finder
is connected to.
0 1023
Darker,
less reective surfaces.
Lighter,
more reective surfaces.
Surface Reectivity
Value Returned
191
Review of Modules
A Potentiometer is a input module which can be turned or rotated right or left. Based
on the position of the knob, it produces a dierent signal value for the Core module to
read. It must be connected to an analog port (ports denoted with “A”).
Analog Read block
This block returns the value from the Potentiometer. A value between 0 and 1023 is returned.
If the potentiometer is turned all the way to the left, it produces a 0 value. If it is turned all
the way tot the right, it produces a 1023 value. In the middle it produces a value around 512.
Potentiometer
Pin the Potentiometer
is connected to.
0 1023
192
Review of Modules
A Motor is a electrical machine which converts electrical energy to mechanical energy. It
is capable of rotating clockwise and counter clockwise. It is controlled by a Motor Con-
troller. A Motor Controller can control up to two Motors. A Wheel can be attached to the
Motor.
Motor
Motor Controller Wheel
Motor - Set Speed block
This block sets the speed of the motor. Positive values turns the Motor in one direc-
tion. While negative values turns the Motor in the opposite direction. A value of 0
stops the Motor. Values between -255 and 255 are accepted. 255 is maximum clock-
wise speed. -255 is maximum counter-clockwise speed.
Motor - Brake block
This block sets the speed to 0 to stop the Motor.
Motor to congure.
Motor to congure.
Speed to set.
193
Review of Blocks
Addition Block
This block returns the sum of the left input and right input.
Division Block
This block returns the value of dividing the left input by the right input.
Multiplication Block
This block returns the product of the left input and right input.
Subtraction Block
This block returns the value of subtracting the left input by the right input.
Less Than Block
This block returns TRUE if the left input is less than the right input.
Equal Block
This block returns TRUE if the left input is equal to the right input.
Greater Than Block
This block returns TRUE if the left input is greater than the right input.
194
Review of Concepts
Creating a Variable in Scratch
Click on the “Variable” category.
Then click on the “Create variable...” button.
Name your variable.
195
Review of Concepts
Creating a Variable in Scratch (continued)
Several new blocks will appear which allows
access and modication of the new variable.
Variable Value block
Variable Set block
Variable Change block
This block returns the current value of the variable.
This block sets the value of the variable. It will overwrite the current value.
This block changes the variable value by adding the input value (negative values or blocks are accepted).
input value
196
The Problem
How can we create a delivery truck with adjustable speeds?
Which waits for something to be loaded, then drives it to the destination, waits for
it to be unloaded, then drives back.
197
Project Worksheet
Complete the worksheet below to your best abilities.
1. A solution.
2. List all the parts needed for the solution. What is the purpose of each part?
3. What is a simple explanation of the logic for the solution?
4. Create a ow chart of the solution.
5. How do you assemble and connect the modules for the solution?
6. Congure blocks to relate to the ow chart.
Use two motors to move the delivery truck. Use a Line Finder to detect if cargo is present or not. Use a Line
Finder (front) to check if the destination is reached. Use a Line Finder (back) to check if returned back to base.
Use a Potentiometer to adjust the speed of the truck.
198
Project Worksheet - Answers
2. List all the parts needed for the solution. What is the purpose of each part?
1x mCenter+ - control the project, connect sensors and trinkets to project, and supply power.
5x Hub Connector Cable - connect sensors and trinkets to the mCenter+.
1x USB Cable - connect mCenter+ to a computer for charging and programming the project.
2x Motor - to move the delivery truck.
1x Motor Controller - controls and drives the attached motors.
2x Wheel - attach to motor.
3x Line Finder - detect objects in the front and back of the car and if cargo is present.
1x Potentiometer - to adjust speed of the truck.
1x IO Splitter - to connect two sensors/trinkets to a single connector.
199
Project Worksheet - Answers
3. What is a simple explanation of the logic for the solution?
Use two Motors to move the delivery truck.
Use a Line Finder in the cargo holding location to detect if cargo is loaded or unloaded.
Use a Line Finder in the front to detect if the destination has been reached.
Use a Line Finder in the back to detect if returned to base.
Use a Potentiometer to adjust the speed of the delivery truck.
Wait until cargo is loaded onto truck (Line Finder in cargo holding < 200).
Wait 1 second before driving to destination.
Move forward* until destination is reached (Line Finder in front < 700).
Wait until cargo is unloaded from truck (Line Finder in cargo holding > 400).
Wait 1 second before driving back to base.
Move backward* until back to base (Line Finder in back < 700).
*When setting the speed to move forward or backward, read the Potentiometer (value between 0 and 1023) rst,
then scale (divide by 4, 1023/4 =~ 255) and use that as the speed.
200
Project Worksheet - Answers
4. Create a ow chart of the solution.
Start
Set variable for motor speed
Set motors to off
(stop the truck)
Wait until cargo is loaded
Short delay before
driving to destination
Read and store Potentiometer
and scale it for motor speed
Set motors to move
truck forward
Set motors to move
truck backward
Wait until truck has
reach its destination
Set motors to off
(stop the truck)
Wait until cargo is unloaded
Short delay before
driving back to base
Wait until truck has
reach back to base
Loop
forever.
201
Project Worksheet - Answers
5. How do you assemble and connect the modules for the solution?
1. Stack the Motor Controller onto mCenter+.
2. Connect a Motor to connector “1A1B” on the Motor Controller.
3. Connect a Motor to connector “2A2B” on the Motor Controller.
4. Connect a Line Finder to pin A2/A3.
5. Connect a Potentiometer to pin A6/A7
6. Connect two Line Finders to an IO Splitter. Connect the IO Splitter to pin A0/A1.
7. Insert one end of the USB cable into mCenter+ and connect the other to a computer.
D2/D3
D8/D9
I2C A2/A3
I2C
D4/D5
A6/A7
A0/A1
I2C
D6/D7
IN B
202
Project Worksheet - Answers
6. Congure blocks to relate to the ow chart.
Start
Set variable for motor speed
Set motors to off
(stop the truck)
Wait until cargo is loaded
Short delay before
driving to destination
Read and store Potentiometer
and scale it for motor speed
Set motors to move
truck forward
Set motors to move
truck backward
Wait until truck has
reach its destination
Set motors to off
(stop the truck)
Wait until cargo is unloaded
Short delay before
driving back to base
Wait until truck has
reach back to base
Loop
forever.
203
The Script Block Locator
Loop.
Click Green Flag to activate script.
Wait until Line Finder (A2) in back detects returning to base.
(Create “motor_speed” variable
rst in the variable tab.)
Control
Events
mCookie
Data
Operators
Arduino
Set the variable for the speed of the motors.
Turn motors o (stop the truck).
Wait until Line Finder (A0) detects cargo (loaded).
Wait 1 second before driving to destination.
Wait 1 second before driving back to base.
Scale and store Potentiometer (pin A6) value to motor speed.
Scale and store Potentiometer (pin A6) value to motor speed.
Set motors to move truck forward.
Set motors to move truck backward.
Wait until Line Finder (A1) in front detects reaching destination.
Turn motors o (stop the truck).
Wait until Line Finder (A0) detects no cargo (unloaded).
204
Click the green
ag to activate
the script. 1. Load cargo into the truck.
2. The truck will then move forward until it research its
destination (object blocking its path in the front).
3. Truck will stop and wait until cargo is unloaded.
4. The truck will then move backward until it research
its base (object blocking its path in the back).
Line Finder (pin A0)
Line Finder (pin A1)
Line Finder (pin A2)
Testing the Program
Important:
Ensure that mCenter+ is on. Flip the switch on mCenter+
to turn it on. An LED on mCenter+ will light up to indicate
power is being supplied.
Ensure you have selected the Serial Port and ashed the
special rmware (mCookie) rst before testing (this only
needs to be done once unless the rmware is overwritten).
Ensure you have selected the Serial Port and connected
before testing.
Flash rmware
Select Serial Port
Testing:
Note: If the program is working correctly and you wish to use the program without being connected to your computer. Switch to Arduino mode
(top right), then “Flash Program” (this overrides the special rmware, you will need to reash to use Live mode).
205
Create a Structure
Create a structure for the project using building blocks, paper, or other crafting materials.
206
Project Challenge
Add a Color LED to your project.
Turn set the Color LED’s based on the state the delivery truck.
(Examples: loaded cargo, stopped, moving, unloaded cargo.)
207
Review
Motor Controller - controls and drives attached motors. Can control up to 2 motors.
Motor - converts electrical energy into mechanical energy in the form of torque.
Motor - Set Speed block - set the speed of a motor.
Wheel - attaches to the Motor.
Line Finder - detects the reective surface or distance of an object.
Analog Read block - returns the value of the reective surface from the Line Finder sensor.
Potentiometer - input module with an adjustable knob.
Analog Read block - reads the value from the potentiometer. Returns a value between 0 and
1023.
208
Expanding the Project
Research
Write down a way of expanding this project. Create it on your own.
Research more about delivery trucks.
What are some important things delivery trucks must accomplish to be protable?
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
209
Obstacle Avoidance Car
MIX 4 - Project 9
(Old Cards: MIX4B-04)
210
In robotics, obstacle avoidance is the task of satisfying some control objective subject to non-intersection
or non-collision position constraints.
What is critical about obstacle avoidance concept in this area is the growing need of usage of unmanned
aerial vehicles in urban areas for especially military applications where it can be very useful in city wars.
Normally obstacle avoidance is considered to be distinct from path planning in that one is usually imple-
mented as a reactive control law while the other involves the pre-computation of an obstacle-free path
which a controller will then guide a robot along.
Source: https://en.wikipedia.org/wiki/Obstacle_avoidance
About Obstacle Avoidance
211
Review of Modules
Usage
The Line Finder module is able to detect reectivity or gray level of a surface. In gen-
eral, darker or less reective surfaces produces a higher value. While a lighter or more
reective surface produces a lower value. It must be connected to a pin with an
“A” prex (A0/A1, A2/A3, A6/A7).
Line Finder
Analog Read block
This block returns the value that the Line Finder detects. A value between 0 and 1023 is
returned.
Pin that Line Finder
is connected to.
0 1023
Darker,
less reective surfaces.
Lighter,
more reective surfaces.
Surface Reectivity
Value Returned
212
Review of Modules
A Potentiometer is a input module which can be turned or rotated right or left. Based
on the position of the knob, it produces a dierent signal value for the Core module to
read. It must be connected to an analog port (ports denoted with “A”).
Analog Read block
This block returns the value from the Potentiometer. A value between 0 and 1023 is returned.
If the potentiometer is turned all the way to the left, it produces a 0 value. If it is turned all
the way tot the right, it produces a 1023 value. In the middle it produces a value around 512.
Potentiometer
Pin the Potentiometer
is connected to.
0 1023
213
Review of Modules
A Motor is a electrical machine which converts electrical energy to mechanical energy. It
is capable of rotating clockwise and counter clockwise. It is controlled by a Motor Con-
troller. A Motor Controller can control up to 2 Motors. A Wheel can be attached to the
Motor.
Motor
Motor Controller Wheel
Motor - Set Speed block
This block sets the speed of the motor. Positive values turns the Motor in one direc-
tion. While negative values turns the Motor in the opposite direction. A value of 0
stops the Motor. Values between -255 and 255 are accepted. 255 is maximum clock-
wise speed. -255 is maximum counter-clockwise speed.
Motor - Brake block
This block sets the speed to 0 to stop the Motor.
Motor to congure.
Motor to congure.
Speed to set.
214
Review of Blocks
Addition Block
This block returns the sum of the left input and right input.
Division Block
This block returns the value of dividing the left input by the right input.
Multiplication Block
This block returns the product of the left input and right input.
Subtraction Block
This block returns the value of subtracting the left input by the right input.
Less Than Block
This block returns TRUE if the left input is less than the right input.
Equal Block
This block returns TRUE if the left input is equal to the right input.
Greater Than Block
This block returns TRUE if the left input is greater than the right input.
215
Review of Concepts
Creating a Variable in Scratch
Click on the “Variable” category.
Then click on the “Create variable...” button.
Name your variable.
216
Review of Concepts
Creating a Variable in Scratch (continued)
Several new blocks will appear which allows
access and modication of the new variable.
Variable Value block
Variable Set block
Variable Change block
This block returns the current value of the variable.
This block sets the value of the variable. It will overwrite the current value.
This block changes the variable value by adding the input value (negative values or blocks are accepted).
input value
217
The Problem
How can we create a car which can detect and avoid objects in the way?
218
Project Worksheet
Complete the worksheet below to your best abilities.
1. A solution.
2. List all the parts needed for the solution. What is the purpose of each part?
3. What is a simple explanation of the logic for the solution?
4. Create a ow chart of the solution.
5. How do you assemble and connect the modules for the solution?
6. Congure blocks to relate to the ow chart.
Use two motors to move the car. Use 4 Line Finder positioned in front of the car to detect oncoming objects.
Use this information to steer the car away to avoid the object. Use a Potentiometer to set the motor speeds.
219
Project Worksheet - Answers
2. List all the parts needed for the solution. What is the purpose of each part?
1x mCenter+ - control the project, connect sensors and trinkets to project, and supply power.
7x Hub Connector Cable - connect sensors and trinkets to the mCenter+.
1x USB Cable - connect mCenter+ to a computer for charging and programming the project.
2x Motor - to move the car.
1x Motor Controller - controls and drives the attached motors.
2x Wheel - attach to motor.
4x Line Finder - detect objects oncoming objects.
1x Potentiometer - to adjust speed of the car.
2x IO Splitter - to connect two sensors/trinkets to a single connector.
220
Project Worksheet - Answers
3. What is a simple explanation of the logic for the solution?
Use two Motors to move the car.
Use 4 Line Finders placed in front of the car to detect oncoming objects.
Use a Potentiometer to set the speed of the car.
S0, S1, S2, S3 are the 4 Line Finders placed in front of the car.
If S0 detects an object, steer to the right.
Else if S1 detects an object, steer to the right.
Else if S2 detects an object, steer to the left.
Else if S3 detects an object, steer to the left.
Else no objects are detected, go forward.
S0 S1 S2 S3
S0 S1 S2 S3 S0 S1 S2 S3
221
Project Worksheet - Answers
4. Create a ow chart of the solution.
Start
Read and store Potentiometer
and scale it for motor speed
Read and store Line Finder
values
Is there an object detected on
the left most side (S0)?
Is there an object detected on
the left middle side (S1)?
Is there an object detected on
the right most side (S3)?
Is there an object detected on
the right middle side (S2)?
Set motors to
turn car right
Set motors to
turn car right
Set motors to
turn car left
Set motors to
turn car left
Otherwise there is no obstacle,
set motors to go forward
Short delay
Loop
forever.
True
True
True
True
False
False
False
False
222
Project Worksheet - Answers
5. How do you assemble and connect the modules for the solution?
1. Stack the Motor Controller onto mCenter+.
2. Connect a Motor to connector “1A1B” on the Motor Controller.
3. Connect a Motor to connector “2A2B” on the Motor Controller.
4. Connect a Potentiometer to pin A6/A7
5. Connect two Line Finders to an IO Splitter. Connect the IO Splitter to pin A0/A1.
6. Connect two Line Finders to an IO Splitter. Connect the IO Splitter to pin A2/A3.
7. Insert one end of the USB cable into mCenter+ and connect the other to a computer.
D2/D3
D8/D9
I2C A2/A3
I2C
D4/D5
A6/A7
I2C
D6/D7
A0/A1
IN B
IN B
223
Project Worksheet - Answers
6. Congure blocks to relate to the ow chart.
Start
Read and store Potentiometer
and scale it for motor speed
Read and store Line Finder
values
Is there an object detected on
the left most side (S0)?
Is there an object detected on
the left middle side (S1)?
Is there an object detected on
the right most side (S3)?
Is there an object detected on
the right middle side (S2)?
Set motors to
turn car right
Set motors to
turn car right
Set motors to
turn car left
Set motors to
turn car left
Otherwise there is no obstacle,
set motors to go forward
Short delay
Loop
forever.
True
True
True
True
False
False
False
False
224
The Script Block Locator
Loop.
Click Green Flag to activate script.
Short delay before looping.
(Create these variables
rst in the variable tab.)
Control
Events
mCookie
Data
Operators
Arduino
Scale and store Potentiometer (pin A6) value to motor speed.
Read and store Line Finder (pin A0) value.
Read and store Line Finder (pin A1) value.
Read and store Line Finder (pin A2) value.
Read and store Line Finder (pin A3) value.
Check if there is an oncoming object on the left most side.
Check if there is an oncoming object on the left center side.
Check if there is an oncoming object on the right center side.
Check if there is an oncoming object on the right most side.
Otherwise no coming objects.
Set motors to move car forward.
Set motors to turn car left.
Set motors to turn car left.
Set motors to turn car right.
Set motors to turn car right.
225
Click the green
ag to activate
the script.
Ensure Line Finders are
in the correct position.
S0 S1 S2 S3
Car will automatically
avoid oncoming objects
by steering left or right.
Speed can be adjusted
with the Potentiometer.
Testing the Program
Important:
Ensure that mCenter+ is on. Flip the switch on mCenter+
to turn it on. An LED on mCenter+ will light up to indicate
power is being supplied.
Ensure you have selected the Serial Port and ashed the
special rmware (mCookie) rst before testing (this only
needs to be done once unless the rmware is overwritten).
Ensure you have selected the Serial Port and connected
before testing.
Flash rmware
Select Serial Port
Testing:
Note: If the program is working correctly and you wish to use the program without being connected to your computer. Switch to Arduino mode
(top right), then “Flash Program” (this overrides the special rmware, you will need to reash to use Live mode).
226
Create a Structure
Create a structure for the project using building blocks, paper, or other crafting materials.
227
Project Challenge
Adjust the script so the car turns slightly right when it detects an object on the left
most side (S0).
Adjust the script so the car turns slightly left when it detects an object on the right
most side (S3).
228
Review
Motor Controller - controls and drives attached motors. Can control up to 2 motors.
Motor - converts electrical energy into mechanical energy in the form of torque.
Motor - Set Speed block - set the speed of a motor.
Wheel - attaches to the Motor.
Line Finder - detects the reective surface or distance of an object.
Analog Read block - returns the value of the reective surface from the Line Finder sensor.
Potentiometer - input module with an adjustable knob.
Analog Read block - reads the value from the potentiometer. Returns a value between 0 and
1023.
229
Expanding the Project
Research
Write down a way of expanding this project. Create it on your own.
Research more about obstacle avoidance.
Why is this an important feature in safe driving cars?
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
230
Edge Avoidance Car
MIX 4 - Project 10
(Old Cards: MIX4B-06)
231
If you put the a cleaning robot on a desk it won’t fall onto the ground, because
there are sensors detecting distance from the ground at the bottom of it.
Source: https://en.wikipedia.org/wiki/Obstacle_avoidance
About Edge Detection
232
Review of Modules
Usage
The Line Finder module is able to detect reectivity or gray level of a surface. In gen-
eral, darker or less reective surfaces produces a higher value. While a lighter or more
reective surface produces a lower value. It must be connected to a pin with an
“A” prex (A0/A1, A2/A3, A6/A7).
Line Finder
Analog Read block
This block returns the value that the Line Finder detects. A value between 0 and 1023 is
returned.
Pin that Line Finder
is connected to.
0 1023
Darker,
less reective surfaces.
Lighter,
more reective surfaces.
Surface Reectivity
Value Returned
233
Review of ModulesReview of Modules
A Motor is a electrical machine which converts electrical energy to mechanical energy. It
is capable of rotating clockwise and counter clockwise. It is controlled by a Motor Con-
troller. A Motor Controller can control up to 2 Motors. A Wheel can be attached to the
Motor.
Motor
Motor Controller Wheel
Motor - Set Speed block
This block sets the speed of the motor. Positive values turns the Motor in one direc-
tion. While negative values turns the Motor in the opposite direction. A value of 0
stops the Motor. Values between -255 and 255 are accepted. 255 is maximum clock-
wise speed. -255 is maximum counter-clockwise speed.
Motor - Brake block
This block sets the speed to 0 to stop the Motor.
Motor to congure.
Motor to congure.
Speed to set.
234
Review of Blocks
Addition Block
This block returns the sum of the left input and right input.
Division Block
This block returns the value of dividing the left input by the right input.
Multiplication Block
This block returns the product of the left input and right input.
Subtraction Block
This block returns the value of subtracting the left input by the right input.
Less Than Block
This block returns TRUE if the left input is less than the right input.
Equal Block
This block returns TRUE if the left input is equal to the right input.
Greater Than Block
This block returns TRUE if the left input is greater than the right input.
235
Review of Concepts
Booleans
Booleans are a type of data, which can only either be TRUE or FALSE. Booleans are used in the condition checks
on blocks such as the “If, “If Else”, “Wait Until” blocks.
Boolean Operators
Boolean Operators enables performing operations on boolean types. It is useful for testing multiple conditions.
Left Input Right Input Result
True True True
True False False
False True False
False False False
Left Input Right Input Result
True True True
True False True
False True True
False False False
Input Result
True False
False True
And block
Or block
Not block
This block returns TRUE, when both the inputs
are TRUE. Otherwise, it returns FALSE.
This block returns TRUE, when one of the input is
TRUE. Otherwise, it returns FALSE.
This block returns the opposite of the input. If in-
put is TRUE, this returns FALSE. If input is FALSE,
this returns TRUE.
236
Review of Concepts
Creating a Variable in Scratch
Click on the “Variable” category.
Then click on the “Create variable...” button.
Name your variable.
237
Review of Concepts
Creating a Variable in Scratch (continued)
Several new blocks will appear which allows
access and modication of the new variable.
Variable Value block
Variable Set block
Variable Change block
This block returns the current value of the variable.
This block sets the value of the variable. It will overwrite the current value.
This block changes the variable value by adding the input value (negative values or blocks are accepted).
input value
238
The Problem
How can we create a car which detects and avoids edges (clis)?
239
Project Worksheet
Complete the worksheet below to your best abilities.
1. A solution.
2. List all the parts needed for the solution. What is the purpose of each part?
3. What is a simple explanation of the logic for the solution?
4. Create a ow chart of the solution.
5. How do you assemble and connect the modules for the solution?
6. Congure blocks to relate to the ow chart.
Use two motors to move the car. Use 2 Line Finders in front of the car pointed downwards to detect if there is
an edge (cli). Steer away if there is a cli.
240
Project Worksheet - Answers
2. List all the parts needed for the solution. What is the purpose of each part?
1x mCenter+ - control the project, connect sensors and trinkets to project, and supply power.
2x Hub Connector Cable - connect sensors and trinkets to the mCenter+.
1x USB Cable - connect mCenter+ to a computer for charging and programming the project.
2x Motor - to move the car.
1x Motor Controller - controls and drives the attached motors.
2x Wheel - attach to motor.
2x Line Finder - detect if there is an edge / cli.
241
Project Worksheet - Answers
3. What is a simple explanation of the logic for the solution?
Use two Motors to move the car.
Use 2 Line Finders in front of the car pointing downwards to detect edges (cli).
Read the left Line Finder to see if there is ground ahead (no cli).
Read the right Line Finder to see if there is a ground ahead (no cli).
If there is ground in both Left and Right sides, proceed forward.
If there is no ground on the Left and ground on the Right, turn right. (Right side is safe to drive on).
If there is ground on the Left and no ground on the Right, turn left. (Left side is safe to drive on).
If there is no ground in both Left and Right sides, stop car.
242
Project Worksheet - Answers
4. Create a ow chart of the solution.
Start
Set variable for motor speed
Read and store left and right
Line Finder values
Is both left and right
sides safe (no cliff)?
Is only the right
side safe (no cliff)?
Is only the left
side safe (no cliff)?
Otherwise, unsafe
Turn off motors
(stop car)
Set motors to
move car forward
Set motors to
turn car right
Set motors to
turn car left
Short delay
Loop
forever.
True
True
True
False
False
False
243
Project Worksheet - Answers
5. How do you assemble and connect the modules for the solution?
1. Stack the Motor Controller onto mCenter+.
2. Connect a Motor to connector “1A1B” on the Motor Controller.
3. Connect a Motor to connector “2A2B” on the Motor Controller.
4. Connect a Line Finder to pin A0/A1.
5. Connect a Line Finder to pin A2/A3.
6. Insert one end of the USB cable into mCenter+ and connect the other to a computer.
D2/D3
D8/D9
I2C A2/A3
I2C
D4/D5
A6/A7
A0/A1
I2C
D6/D7
244
Project Worksheet - Answers
6. Congure blocks to relate to the ow chart.
Start
Set variable for motor speed
Read and store left and right
Line Finder values
Is both left and right
sides safe (no cliff)?
Is only the right
side safe (no cliff)?
Is only the left
side safe (no cliff)?
Otherwise, unsafe
Turn off motors
(stop car)
Set motors to
move car forward
Set motors to
turn car right
Set motors to
turn car left
Short delay
Loop
forever.
True
True
True
False
False
False
245
The Script Block Locator
Loop.
Click Green Flag to activate script.
Short delay before looping.
(Create these variables
rst in the variable tab.)
Set the variable for the speed of the motors.
Read and store the left Line Finder (pin A0) value.
Read and store the right Line Finder (pin A2) value.
Check if it is safe on both Left and Right sides.
Check if there is a cli on Left side and safe on Right side.
Check if it is safe on Left side and a cli on Right side.
Otherwise both sides are unsure,
set motors to stop car.
Set motors to proceed forward.
Set motors turn right.
Set motors turn left.
Control
Events
mCookie
Data
Operators
Arduino
246
Click the green
ag to activate
the script.
1. The car will automatically move forward if left and right sides are safe (no cli).
2. The car will turn right if only the right side is safe.
3. The car will turn left is only the left side is safe.
4. The car will stop if both left and right sides are unsafe (cli).
Left Line Finder (pin A0)
Right Line Finder (pin A2)
Testing the Program
Important:
Ensure that mCenter+ is on. Flip the switch on mCenter+
to turn it on. An LED on mCenter+ will light up to indicate
power is being supplied.
Ensure you have selected the Serial Port and ashed the
special rmware (mCookie) rst before testing (this only
needs to be done once unless the rmware is overwritten).
Ensure you have selected the Serial Port and connected
before testing.
Flash rmware
Select Serial Port
Testing:
Note: If the program is working correctly and you wish to use the program without being connected to your computer. Switch to Arduino mode
(top right), then “Flash Program” (this overrides the special rmware, you will need to reash to use Live mode).
247
Create a Structure
Create a structure for the project using building blocks, paper, or other crafting materials.
248
Project Challenge
The car stops when the left and right sides are unsafe.
What other options can the car makes in this situation?
Implement it on your own.
249
Review
Motor Controller - controls and drives attached motors. Can control up to 2 motors.
Motor - converts electrical energy into mechanical energy in the form of torque.
Motor - Set Speed block - set the speed of a motor.
Wheel - attaches to the Motor.
Line Finder - detects the reective surface or distance of an object.
Analog Read block - returns the value of the reective surface from the Line Finder sensor.
250
Expanding the Project
Research
Write down a way of expanding this project. Create it on your own.
Research more about edge/cli detection. What things implement this feature?
Why is it important for each of those things to have this feature?
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
251
Line Finding Car 1
MIX 4 - Project 11
(Old Cards: MIX4B-07)
252
Some of the earliest Automated Guided Vehicles (AGVs) were line following mobile robots. They might
follow a visual line painted or embedded in the oor or ceiling or an electrical wire in the oor.
Most of these robots operated a simple “keep the line in the center sensor” algorithm. They could not cir-
cumnavigate obstacles; they just stopped and waited when something blocked their path.
Source: https://en.wikipedia.org/wiki/Mobile_robot#Line-following_Car
About Line Following Cars
253
Review of Modules
Usage
The Line Finder module is able to detect reectivity or gray level of a surface. In gen-
eral, darker or less reective surfaces produces a higher value. While a lighter or more
reective surface produces a lower value. It must be connected to a pin with an
“A” prex (A0/A1, A2/A3, A6/A7).
Line Finder
Analog Read block
This block returns the value that the Line Finder detects. A value between 0 and 1023 is
returned.
Pin that Line Finder
is connected to.
0 1023
Darker,
less reective surfaces.
Lighter,
more reective surfaces.
Surface Reectivity
Value Returned
254
Review of Modules
A Motor is a electrical machine which converts electrical energy to mechanical energy. It
is capable of rotating clockwise and counter clockwise. It is controlled by a Motor Con-
troller. A Motor Controller can control up to 2 Motors. A Wheel can be attached to the
Motor.
Motor
Motor Controller Wheel
Motor - Set Speed block
This block sets the speed of the motor. Positive values turns the Motor in one direc-
tion. While negative values turns the Motor in the opposite direction. A value of 0
stops the Motor. Values between -255 and 255 are accepted. 255 is maximum clock-
wise speed. -255 is maximum counter-clockwise speed.
Motor - Brake block
This block sets the speed to 0 to stop the Motor.
Motor to congure.
Motor to congure.
Speed to set.
255
Review of Blocks
Addition Block
This block returns the sum of the left input and right input.
Division Block
This block returns the value of dividing the left input by the right input.
Multiplication Block
This block returns the product of the left input and right input.
Subtraction Block
This block returns the value of subtracting the left input by the right input.
Less Than Block
This block returns TRUE if the left input is less than the right input.
Equal Block
This block returns TRUE if the left input is equal to the right input.
Greater Than Block
This block returns TRUE if the left input is greater than the right input.
256
Review of Concepts
Booleans
Booleans are a type of data, which can only either be TRUE or FALSE. Booleans are used in the condition checks
on blocks such as the “If, “If Else”, “Wait Until” blocks.
Boolean Operators
Boolean Operators enables performing operations on boolean types. It is useful for testing multiple conditions.
Left Input Right Input Result
True True True
True False False
False True False
False False False
Left Input Right Input Result
True True True
True False True
False True True
False False False
Input Result
True False
False True
And block
Or block
Not block
This block returns TRUE, when both the inputs
are TRUE. Otherwise, it returns FALSE.
This block returns TRUE, when one of the input is
TRUE. Otherwise, it returns FALSE.
This block returns the opposite of the input. If in-
put is TRUE, this returns FALSE. If input is FALSE,
this returns TRUE.
257
Review of Concepts
Creating a Variable in Scratch
Click on the “Variable” category.
Then click on the “Create variable...” button.
Name your variable.
258
Review of Concepts
Creating a Variable in Scratch (continued)
Several new blocks will appear which allows
access and modication of the new variable.
Variable Value block
Variable Set block
Variable Change block
This block returns the current value of the variable.
This block sets the value of the variable. It will overwrite the current value.
This block changes the variable value by adding the input value (negative values or blocks are accepted).
input value
259
The Problem
How can we create a car which follows a black line (track)?
260
Project Worksheet
Complete the worksheet below to your best abilities.
1. A solution.
2. List all the parts needed for the solution. What is the purpose of each part?
3. What is a simple explanation of the logic for the solution?
4. Create a ow chart of the solution.
5. How do you assemble and connect the modules for the solution?
6. Congure blocks to relate to the ow chart.
Use two motors to move the car. Use Two Line Finders pointed downward to detect the black line. Adjust ac-
cordingly to stay on the black line.
261
Project Worksheet - Answers
2. List all the parts needed for the solution. What is the purpose of each part?
Project Worksheet - Answers
2. List all the parts needed for the solution. What is the purpose of each part?
1x mCenter+ - control the project, connect sensors and trinkets to project, and supply power.
2x Hub Connector Cable - connect sensors and trinkets to the mCenter+.
1x USB Cable - connect mCenter+ to a computer for charging and programming the project.
2x Motor - to move the car.
1x Motor Controller - controls and drives the attached motors.
2x Wheel - attach to motor.
2x Line Finder - to detect the black line.
262
Project Worksheet - Answers
3. What is a simple explanation of the logic for the solution?
Use two Motors to move the car.
Use 2 Line Finders pointed downward to detect the black line.
When the both Line Finders do not detect the black line, proceed forward.
When the left Line Finder detects the black line, steer left.
When the right Line Finder detects the black line, steer right.
*Note: The Line Finders must be spaced apart slightly more than the width of the black line (as illustrated).
Left
Line Finder
Right
Line Finder
Black line
263
Project Worksheet - Answers
4. Create a ow chart of the solution.
Start
Set variable for motor speed
Read and store if left and right
Line Finder detects a line
Is both left and right
sides not detecting a line?
Is only the left
detecting a line?
Is only the right
detecting a line?
Otherwise, on the line
turn off motors
(stop car)
Set motors to
move car forward
Set motors to
turn car left
Set motors to
turn car right
Short delay
Loop
forever.
True
True
True
False
False
False
264
Project Worksheet - Answers
5. How do you assemble and connect the modules for the solution?
1. Stack the Motor Controller onto mCenter+.
2. Connect a Motor to connector “1A1B” on the Motor Controller.
3. Connect a Motor to connector “2A2B” on the Motor Controller.
4. Connect a Line Finder to pin A0/A1.
5. Connect a Line Finder to pin A2/A3.
6. Insert one end of the USB cable into mCenter+ and connect the other to a computer.
D2/D3
D8/D9
I2C A2/A3
I2C
D4/D5
A6/A7
A0/A1
I2C
D6/D7
D2/D3
D8/D9
I2C A2/A3
I2C
D4/D5
A6/A7
A0/A1
I2C
D6/D7
265
Project Worksheet - Answers
6. Congure blocks to relate to the ow chart.
Start
Set variable for motor speed
Read and store if left and right
Line Finder detects a line
Is both left and right
sides not detecting a line?
Is only the left
detecting a line?
Is only the right
detecting a line?
Otherwise, on the line
turn off motors
(stop car)
Set motors to
move car forward
Set motors to
turn car left
Set motors to
turn car right
Short delay
Loop
forever.
True
True
True
False
False
False
266
The Script Block Locator
Loop.
Click Green Flag to activate script.
Short delay before looping.
(Create these variables
rst in the variable tab.)
Set the variable for the speed of the motors.
Read and store Left side if detects a black line.
0 (false) = detecting line. 1 (true) = not detecting line.
Read and store Right side if detects a black line.
0 (false) = detecting line. 1 (true) = not detecting line.
Check if not detecting line on both sides.
Set motors to move car forward.
Otherwise car is on the black line,
Set motors to o (stop the car).
Set motors to turn car right.
Set motors to turn car left.
Check if detecting line on left side.
Check if detecting line on right side.
Control
Events
mCookie
Data
Operators
Arduino
267
Click the green
ag to activate
the script.
1. Create a straight line with black tape.
2. Make sure your Line Finders are spaced apart SLIGHTLY larger than the width of the black tape.
3. Place the car on the black tape. The car will trace the black tape.
Left Line Finder (pin A0)
Right Line Finder (pin A1)
Testing the Program
Important:
Ensure that mCenter+ is on. Flip the switch on mCenter+
to turn it on. An LED on mCenter+ will light up to indicate
power is being supplied.
Ensure you have selected the Serial Port and ashed the
special rmware (mCookie) rst before testing (this only
needs to be done once unless the rmware is overwritten).
Ensure you have selected the Serial Port and connected
before testing.
Flash rmware
Select Serial Port
Testing:
Note: If the program is working correctly and you wish to use the program without being connected to your computer. Switch to Arduino mode
(top right), then “Flash Program” (this overrides the special rmware, you will need to reash to use Live mode).
268
Create a Structure
Create a structure for the project using building blocks, paper, or other crafting materials.
269
Project Challenge
The car stops if it detects the line on both Line Finders (left and right).
What is another solution for this scenario?
Implement it on your own.
270
Review
Motor Controller - controls and drives attached motors. Can control up to 2 motors.
Motor - converts electrical energy into mechanical energy in the form of torque.
Motor - Set Speed block - set the speed of a motor.
Wheel - attaches to the Motor.
Line Finder - detects the reective surface or distance of an object.
Analog Read block - returns the value of the reective surface from the Line Finder sensor.
271
Expanding the Project
Research
Write down a way of expanding this project. Create it on your own.
Research more about Line Finding cars.
What are some other features and advancements have been added to this type of car?
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
272
Line Finding Car 2
MIX 4 - Project 12
(Old Cards: MIX4B-08)
273
Some of the earliest Automated Guided Vehicles (AGVs) were line following mobile robots. They might
follow a visual line painted or embedded in the oor or ceiling or an electrical wire in the oor.
Most of these robots operated a simple “keep the line in the center sensor” algorithm. They could not cir-
cumnavigate obstacles; they just stopped and waited when something blocked their path.
Source: https://en.wikipedia.org/wiki/Mobile_robot#Line-following_Car
About Line Following Cars
274
Review of Modules
Usage
The Line Finder module is able to detect reectivity or gray level of a surface. In gen-
eral, darker or less reective surfaces produces a higher value. While a lighter or more
reective surface produces a lower value. It must be connected to a pin with an
“A” prex (A0/A1, A2/A3, A6/A7).
Line Finder
Analog Read block
This block returns the value that the Line Finder detects. A value between 0 and 1023 is
returned.
Pin that Line Finder
is connected to.
0 1023
Darker,
less reective surfaces.
Lighter,
more reective surfaces.
Surface Reectivity
Value Returned
275
Review of Modules
A Potentiometer is a input module which can be turned or rotated right or left. Based
on the position of the knob, it produces a dierent signal value for the Core module to
read. It must be connected to an analog port (ports denoted with “A”).
Analog Read block
This block returns the value from the Potentiometer. A value between 0 and 1023 is returned.
If the potentiometer is turned all the way to the left, it produces a 0 value. If it is turned all
the way tot the right, it produces a 1023 value. In the middle it produces a value around 512.
Potentiometer
Pin the Potentiometer
is connected to.
0 1023
276
Review of Modules
A Motor is a electrical machine which converts electrical energy to mechanical energy. It
is capable of rotating clockwise and counter clockwise. It is controlled by a Motor Con-
troller. A Motor Controller can control up to 2 Motors. A Wheel can be attached to the
Motor.
Motor
Motor Controller Wheel
Motor - Set Speed block
This block sets the speed of the motor. Positive values turns the Motor in one direc-
tion. While negative values turns the Motor in the opposite direction. A value of 0
stops the Motor. Values between -255 and 255 are accepted. 255 is maximum clock-
wise speed. -255 is maximum counter-clockwise speed.
Motor - Brake block
This block sets the speed to 0 to stop the Motor.
Motor to congure.
Motor to congure.
Speed to set.
277
Review of Blocks
Addition Block
This block returns the sum of the left input and right input.
Division Block
This block returns the value of dividing the left input by the right input.
Multiplication Block
This block returns the product of the left input and right input.
Subtraction Block
This block returns the value of subtracting the left input by the right input.
Less Than Block
This block returns TRUE if the left input is less than the right input.
Equal Block
This block returns TRUE if the left input is equal to the right input.
Greater Than Block
This block returns TRUE if the left input is greater than the right input.
278
Review of Concepts
Booleans
Booleans are a type of data, which can only either be TRUE or FALSE. Booleans are used in the condition checks
on blocks such as the “If, “If Else”, “Wait Until” blocks.
Boolean Operators
Boolean Operators enables performing operations on boolean types. It is useful for testing multiple conditions.
Left Input Right Input Result
True True True
True False False
False True False
False False False
Left Input Right Input Result
True True True
True False True
False True True
False False False
Input Result
True False
False True
And block
Or block
Not block
This block returns TRUE, when both the inputs
are TRUE. Otherwise, it returns FALSE.
This block returns TRUE, when one of the input is
TRUE. Otherwise, it returns FALSE.
This block returns the opposite of the input. If in-
put is TRUE, this returns FALSE. If input is FALSE,
this returns TRUE.
279
Review of Concepts
Creating a Variable in Scratch
Click on the “Variable” category.
Then click on the “Create variable...” button.
Name your variable.
280
Review of Concepts
Creating a Variable in Scratch (continued)
Several new blocks will appear which allows
access and modication of the new variable.
Variable Value block
Variable Set block
Variable Change block
This block returns the current value of the variable.
This block sets the value of the variable. It will overwrite the current value.
This block changes the variable value by adding the input value (negative values or blocks are accepted).
input value
281
The Problem
How can enhance the line following robot to increase its accuracy?
282
Project Worksheet
Complete the worksheet below to your best abilities.
1. A solution.
2. List all the parts needed for the solution. What is the purpose of each part?
3. What is a simple explanation of the logic for the solution?
4. Create a ow chart of the solution.
5. How do you assemble and connect the modules for the solution?
6. Congure blocks to relate to the ow chart.
Add two additional Line Finders to increase the line detection accuracy.
283
Project Worksheet - Answers
2. List all the parts needed for the solution. What is the purpose of each part?
1x mCenter+ - control the project, connect sensors and trinkets to project, and supply power.
6x Hub Connector Cable - connect sensors and trinkets to the mCenter+.
1x USB Cable - connect mCenter+ to a computer for charging and programming the project.
2x Motor - to move the car.
1x Motor Controller - controls and drives the attached motors.
2x Wheel - attach to motor.
4x Line Finder - to detect the black line.
1x Potentiometer - to adjust speed of the truck.
284
Project Worksheet - Answers
2. List all the parts needed for the solution. What is the purpose of each part? (Continued)
1x IO Splitter - to connect two sensors/trinkets to a single connector.
285
Project Worksheet - Answers
3. What is a simple explanation of the logic for the solution?
Use two Motors to move the car.
Use 4 Line Finders pointed downward to detect the black line.
Use a Potentiometer to congure the Line Finder threshold for registering a black line.
Leftmost Line Finder is labeled S0, Left middle is S1, Right middle is S2, Rightmost is S3.
A. Only Leftmost detects line - turn sharply to the left to get back on line.
B. Only Leftmost and Left middle detects line - turn slightly to left to get back on line.
C. Only Left middle and Right middle detects line - already on the line, go straight.
D. Only Right middle and Rightmost detects line - turn slightly to the right to get back on line.
E. Only Rightmost detects line - turn sharply to the right to get back on line.
A B C ED
286
Project Worksheet - Answers
4. Create a ow chart of the solution.
Start
Set variable for motor speed
Read and store if the 5
Line Finders detects a line
Loop
forever.
Is condition A present?
Is condition B present?
Is condition C present?
Is condition D present?
Is condition E present?
Read and store Potentiometer
value for line threshold
Set motors to
move car forward
Set motors to
sharply turn left
Set motors to
sharply turn right
Set motors to
slight turn left
Set motors to
slight turn right
Otherwise, unknown condition
turn off motors
(stop car)
Short delay
True
True
True
True
True
False
False
False
False
False
287
Project Worksheet - Answers
5. How do you assemble and connect the modules for the solution?
1. Stack the Motor Controller onto mCenter+.
2. Connect a Motor to connector “1A1B” on the Motor Controller.
3. Connect a Motor to connector “2A2B” on the Motor Controller.
4. Connect a Potentiometer to pin A6/A7
5. Connect two Line Finders to an IO Splitter. Connect the IO Splitter to pin A0/A1.
6. Connect two Line Finders to an IO Splitter. Connect the IO Splitter to pin A2/A3.
7. Insert one end of the USB cable into mCenter+ and connect the other to a computer.
D2/D3
D8/D9
I2C A2/A3
I2C
D4/D5
A6/A7
I2C
D6/D7
A0/A1
IN B
IN B
288
Project Worksheet - Answers
6. Congure blocks to relate to the ow chart.
Start
Set variable for motor speed
Read and store if the 5
Line Finders detects a line
Loop
forever.
Is condition A present?
Is condition B present?
Is condition C present?
Is condition D present?
Is condition E present?
Read and store Potentiometer
value for line threshold
Set motors to
move car forward
Set motors to
sharply turn left
Set motors to
sharply turn right
Set motors to
slight turn left
Set motors to
slight turn right
Otherwise, unknown condition
turn off motors
(stop car)
Short delay
True
True
True
True
True
False
False
False
False
False
289
The Script Block Locator
Loop.
Click Green Flag to activate script.
Short delay before looping.
(Create these variables
rst in the variable tab.)
Set the variable for the speed of the motors.
Read and store Potentiometer (pin A6) for line threshold.
Read and store Line Finder values.
Check for condition A.
Set motors to turn sharply to the left.
Check for condition B.
Set motors to turn slightly to the left.
Check for condition C.
Set motors to move quickly forward.
Check for condition D.
Set motors to turn sharply to the right.
Check for condition E.
Set motors to turn slightly to the right.
Unknown condition,
turn motors o (stop car).
Control
Events
mCookie
Data
Operators
Arduino
“1” represents detecting line.
“0” represents” no line detected.
290
Click the green
ag to activate
the script.
1. Create a straight line with black tape.
2. Make sure the Line Finders are side by side with the correct positions.
The two middle Line Finders should be detecting the black tape.
3. Place the car on the black tape. The car will trace the black tape.
Line Finder (pin A0)
Line Finder (pin A1)
Line Finder (pin A2)
Line Finder (pin A3) Adjust the sensitivity of line
detection by adjusting the
Potentiometer.
Testing the Program
Important:
Ensure that mCenter+ is on. Flip the switch on mCenter+
to turn it on. An LED on mCenter+ will light up to indicate
power is being supplied.
Ensure you have selected the Serial Port and ashed the
special rmware (mCookie) rst before testing (this only
needs to be done once unless the rmware is overwritten).
Ensure you have selected the Serial Port and connected
before testing.
Flash rmware
Select Serial Port
Testing:
Note: If the program is working correctly and you wish to use the program without being connected to your computer. Switch to Arduino mode
(top right), then “Flash Program” (this overrides the special rmware, you will need to reash to use Live mode).
291
Create a Structure
Create a structure for the project using building blocks, paper, or other crafting materials.
292
Project Challenge
Add a Color LED to the project.
Have it indicate the current condition detected (A~E).
Modify the behavior to your liking when it reaches the unknown conditiion.
293
Review
Motor Controller - controls and drives attached motors. Can control up to 2 motors.
Motor - converts electrical energy into mechanical energy in the form of torque.
Motor - Set Speed block - set the speed of a motor.
Wheel - attaches to the Motor.
Line Finder - detects the reective surface or distance of an object.
Analog Read block - returns the value of the reective surface from the Line Finder sensor.
294
Expanding the Project
Research
Write down a way of expanding this project. Create it on your own.
Research new technologies for self driving vehicles.
What kind of methods are used in these self-driving cars?
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
FCC Warning
This device complies with part 15 of the FCC Rules. Operation is subject to the following two conditions:
(1) This device may not cause harmful interference, and
(2) this device must accept any interference received, including interference that may cause undesired operation.
Any Changes or modifications not expressly approved by the party responsible for compliance could void the user's authority
to operate the equipment. This equipment has been tested and found to comply with the limits for a Class B digital device,
pursuant to part 15 of the FCC Rules. These limits are designed to provide reasonable protection against harmful interference
in a residential installation. This equipment generates uses and can radiate radio frequency energy and,
if not installed and used in accordance with the instructions, may cause harmful interference to radio communications.
However, there is no guarantee that interference will not occur in a particular installation.
If this equipment does cause harmful interference to radio or television reception, which can be determined by turning
the equipment off and on, the user is encouraged to try to correct the interference by one or more of the following measures:
-Reorient or relocate the receiving antenna.
-Increase the separation between the equipment and receiver.
-Connect the equipment into an outlet on a circuit different from that to which the receiver is connected.
-Consult the dealer or an experienced radio/TV technician for help.
The device has been evaluated to meet general RF exposure requirement.
The device can be used in portable exposure condition without restriction.
295

Navigation menu