Writing Line Sensor Program  
 
Gradesheet

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 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.


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. Create a new program (File/New).

Task #1: Test The Line Sensor

  1. Sit the robot on its battery pack.
  2. Write a program so that when the line sensor sees the color white on both sensors at the same time ("all"), the robots wheels begin to move forward for 3 seconds. 

  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 to me for credit.

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 #1A: 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 "White", you will use an "=" "Operator" that will use a Value found in the chart above. I am providing you with the "Operator" for this Task below, but look for a block code in which you can insert this Operator and it will allow only the Left wheel to spin...Not both wheels.

Note 1: An Operator is the green background seen below. The Operator seen here with the value of "1" is waiting for the Left Sensor to see White.

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 "Task1_TestTheSensor". 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 at power 40%"
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"statement.

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.