Writing Line Sensor Program    

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.
Ultrasonic Sensor & Light Sensor: Guide Robot Thru Maze
The Line Sensor 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 enter the color you want into your program and the sensor will follow it. Our Goal: We want the robot to move forward when it senses black.   Below are 2 block codes. These block codes are simple and will let you program the robots responses based on whether or not the sensor sees Black or White.


Line Sensor

Before we invest time into writing code,  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, you do not need to 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.

Task #1: Test The Line Sensor
  1. Sit the robot on its battery pack.
  2. Based upon the code you just duplicated so that the robot will move forward for 3 seconds, write a program that when the line sensor sees the color white on both sensors at the same time ("all"), the robots wheels begin to move forward.  

  3. Upload it to your robot and test it. To test it, please ask me for white and black objects that you can hold in front of the sensor.
  4. Check This Out...Before you approach me for credit, I want you to try something: Change "white" to "black" (remember to "Save" and "Upload".). Turn the robot "off" and then "on" inorder to run the new program. There is a good chance that when you turn your robot on, the the wheels will immediately spin and won't wait for you to place the color black in front of the sensor. Why? The Line Follower is based on reflectivity of light: "white" = high reflectivity of light and "black" is low reflectivity of light. The sensor actually is not looking for the colors but an amount of light being reflected, thus if this cheap little sensor does not see "white"...then it tells itself that it must be looking at "black." So, if you write a line follower program with the word "black" and it fails to work, just let me know and I will understand.
  5. Now switch the code back to "white", test it to see that it works and bring it up to me for credit.

Look at this image. I find it confusing!

Place your robot on your desk and sit behind it. According to this image, the wheel on the left is referred to as the "Left Wheel" when you are programming and the wheel on the right is referred to as the "Right Wheel" when you are programming. That is not confusing.

But, according to this image, the left sensor is actually called the "Right" line sensor when programming and right sensor is actually called the "Left" line sensor when programming.

So what does this mean for you?
Just remember this when you are programming Task #1A below, so that you don't get frustrated and confused. And more importantly, remember to remind me when you approach me to get your points!

Note: This 'Confusion' no longer occurs when you begin using "Operatives" starting with Task #1B below.

Good Luck!



Task #1A: The Left Sensor is Triggered by the Color White and the Left Wheel Spins.
Try writing a program that triggers the left wheel to spin, when the color white is seen by the left sensor.
Just have the robot sit on its power pack and pass a white object past the left sensor.
Note: When you rely on blocks with colors (white and black) you will notice that the program will have the robot make a very sharp turn by having the left wheel spin forward while the right wheel spins backwards.
Model Video of Test Result: Mobile or You Tube.


Programming A Line Sensor Using A "Color Chart"
Here is a different way to program the Line Follower Sensor. Instead of entering "black" or "white," enter numbers (values) instead.  Below is a Color Chart that states a number (value) that represents each color scenario.

Example: 


Task #1B: When the Left Sensor is Triggered by the Color White - the Left Wheel Spins - You Must Use An Operator (Hint:Use Value #1).
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 Value found in the chart above. Also, look above and find a block code that will allow only the Left wheel to spin...Not both wheels (Hint: Change "detects All" to "detects Left.").

Note 1: An Operator is the green background seen below. The Operator seen here with the #1 is waiting for the Left Sensor to see White.
Note 2: The left and right sensor 'Confusion' stated above does not occur when using "Operatives".

I usually have my robot sit on its power pack and then I pass a white object past the sensor to trigger just one wheel to turn.
Hint: Duplicate the #1A program. Then delete the line of code that is looking for the color "White" and insert the 'Operator =1' line of code shown above. Then select a new motion block that allows the "left wheel" to turn at "50%" and the "right wheel" to turn at "0%"...now test it!
Notice the blue light that appears on the robot's brain - it tells you when a sensor has been activated.
Model Video of Test Result: Mobile or You Tube.


Task #2A: Black Line Trigger Robot To Move Forward (Using An 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 #1: Operator number is "0."
Hint #2: Look at the Task#1B program you wrote above 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 An Operator).
Write a program that uses an Operator so 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 (If "0" Then "Move Forward", Else "Stop Moving")
Model Video: Mobile or You Tube.


Task #3A: Robot Will Move Directly Down The Black Line With The Aid Of The Line Sensor. (Use An Operator.)
Model Video: Mobile or You Tube.
Write a program , with an Operator, 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 the next Task #3B: Figure 8.
Hint1: Duplicate Task 2B so that: If "0" - Then "Move Forward."
Instead of having the robot stop when it touches white, have it turn away from the white towards the black. To do this... Place two If statements within the Else. Program one 'If' statement to turn left if the right sensor sees black. Program the other 'If' statement to turn right if the left sensor sees black.
Hint2: Minimize the speed. I set my robot to 40%.

Task #3B: Figure 8 (Use An Operator.)
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 #3A should work well for this Task.