Task #1: Line Sensor_ Test the Sensor
Before we invest time into writing code to have a sensor make our robot react to different situations, lets test the sensor to make sure it works.
I recommend we write a basic program based on code we have already learned. This way, if it doesn't work, we know it is the sensor that is having a problem and not our code.
Open the code that you recently wrote that had the robot move forward in a straight line.
Lets Duplicate it: (Chromebook) Hold down the 'alt' key and click on the code. Select 'Duplicate."
Note: From this point forward, do not rewrite code you have already successfully written. Just copy or duplicate it when creating new programs. This process will not only save you time, but decrease the chance you will make an error when creating new programs.
A sensor is a device that when triggered causes a reaction. Example #1: When you open a refrigerator door, a sensor (in the form of a button) notices the door opening and a light goes on. Example #2: When you push a door bell, a chime rings.
The first sensor we will be using is a Line Sensor. Pick up your robot and look at the line sensor - there are two of them facing toward the ground.
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.
You tell it the color you want, or don't want and the sensor will follow it. Our Goal: We want the robot to move forward when it senses black. There are new block codes that are being supplied to you for 'Line Followers':
These block codes are simple and will let you base the programs responses based on whether or not the sensor sees Black or White.
Now look at the table directly below. It allows you to write a more precise program. This Block Code functions based on what the color the left sensor is recognizing and the color the right sensor is recognizing. Thus, if you read this table, you can write a program in which the sensor responds to seeing "3", and that means both sensors see white, whereas if you want the program to react when the sensor only sees black...you would use the "0".
In order to use the values "0" (black) or "3" (white), the code will be written a little differently - it will use an Operator. Operators can be found in the green section of the codes and allows you to establish either a range of numbers or an exact number based on a comparison. Example: Below, the first command is asking the Line Sensor if it sees White and the second command is asking the Line Sensor if it sees Black.
Operators
Select and view the block code in the "Operators" (Green) section of code. Operators are extremely useful tools when working with sensors because they allow you to make comparisons. Example: When the thermostat in my house senses 85 degrees, the air conditioning unit turns on. To program this action, I would use the operator that has an "=" and an "If/Then" statement." If" temperature "=" 80 degrees, "Then" turn on air conditioning. There are thousands of examples like these that surround you each day. The street lights are triggered by darkness. When I back up my car, a warning sensor beeps if their is an object that I might bang into. These are all 'If/Then" statements that use operators that help provide a measurement as to when the action should take place.
Controls:
It is time to expand your programming knowledge beyond a line of blocks instructing a robot to move forward or backward.
If/Then & If/Then/Else block commands are excellent commands to use when wanting to program a reation to an action seen by the sensor. Example: You create a program that when a sensor sees the color black or the color white, the robots wheels begin to move or stop
The Forever block command acts as a Loop so that the sensing action is endless and as long as the sensor sees the color you are requesting, it will continue to react with motion.
Task #1A: Color White Triggers The Wheel To Spin
Try writing a program that triggers the right wheel to move when the color white is seen by the sensor. It is important to try! It may be a struggle at first, but unless you try...you will never get good at it.
This is just a test, so I usually have my robot sit on its power pack and I pass black and/or white past the sensor to trigger just one wheel to turn.
Model Video of Test Result: Mobile or You Tube.
After you try writing a program, take a look at my program : Click Here.
Task #1B: Operator With Measurement #3 Triggers The Wheel To Spin
Write a program that does the same action seen in Task #1A (above), but instead of using the block command that contained the name of a color like "Black" or "White", you will use an "=" "Operator" that will use a number from the chart above that will sense only pure white - so look above at the color table to find the number (hint: 3),
This is just a test, so I usually have my robot sit on its power pack and I pass black and/or white past the sensor to trigger just one wheel to turn.
Model Video of Test Result: Mobile or You Tube.
After you try writing a program, take a look at my program : Click Here.
Task #2A: Black Line Trigger Robot To Move Forward (Using Operator)
Write a program that uses an Operator so that when the sensor sees White, it does not move, but when the sensor is placed over the black line, the sensor is triggered and the robot moves forward and does not stop (it doesn't need to follow the black line...it just gets triggered by the black line.). Hint: Operator number is "0." Hint #2: Look at the Task#1 program to see how an Operator/IfThen/Forever should be programmed together.
Model Video: Mobile or You Tube.
Task #2B: Black Line Trigger Robot To Move Forward, But it Stops When It Touches White(Using Operator)
Write a program that uses an Operator son that when the sensor sees black, it begins to move forward, but when it senses white, its motion stops.
Hint: Copy the code from Task #2A. Switch the "If/Then" statement with an "If/Then/Else statement. Edit the program so that the motors stop when the sensor touches white.
Model Video: Mobile or You Tube.
Task #3A: Robot Will Move Directly Down The Black Line With The Aid Of The Line Sensor.
Model Video: Mobile or You Tube.
Write a program that has your robot move down a straight black line using the line sensor to stop it from turning left or right into the white regions.
Note: The program you write for this Task should also solve Task #3B: Figure 8.
Hint1: As you did previoulsy when writing Motion Code, begin by setting both motors to "0".
Hint2: Create a 1 second delay.
Hint3: Create a "Forever" loop, and insert an "If/Then/Else" into it. Then insert to "If/Then" commands into the "Else" section of this command..
Hint4: Get the robot moving by inserting a "Line Follower = 0" in an Operator in the "If". Insert in the "Then", two "Set Motor" commands...one for the right motor (M2) and one for the left motor (M1). This will allow the robot to move when it sees black. (Note: I slowed my motors to 70.)
Hint5: As stated above, slide two "If/Then" commands into the "Else" section of the previous "If/Then/Else".
These "If/Then" commands are designed to push the robot off the white and back onto the black. It does this by stopping the left motor and turning on the right motor so that the robot immediately turns left. The second "If/Then" statement turns off the right motor and turns on the left motor so that the robot immediatly turns right. Eg: if the robot touches white on the right, the left motor will be set for zero and the right motor will will continue to turn so that the right wheel will turn the robot to the left back onto the black. Once the robot senses black, both motors will continue to rotate forward - this is a result of the initial "If/Then" statement you wrote in "Hint4". Also...I did not use an operator in the "If" portion of these two commands - I used the "LineFollower/Port2/RightOrLeft/is White!
Task #3B: Figure 8
Model Video- Lg.Fig 8: Mobile or You Tube.
Model Video- Small Fig 8: Mobile or You Tube.
Write a program that has the robot successfully drive around a Figure 8.
Hint1: Slow the motor's speed down.
Hint2: The code you wrote for Task #3 should work well for this Task.