LEGO Mindstorms Class: Lesson 4
The Light Sensor:
The light sensor allows a Mindstorms robot to "see" the difference between light and dark. It detects light and dark surfaces by shining a light on the surface, and seeing how much of the light bounces back to the detector. Light surfaces reflect more light, and dark surfaces reflect less light. You can also turn off the light bulb in the light sensor, and then the sensor will detect the amount of light around it. We'll work with reflected light, and use the light sensor attached to 5-Minute Bot to detect dark and light surfaces that it drives over.
Because lighting conditions differ depending on where you are or what time of day it is, the light sensor needs to be calibrated every time you use it under different lighting conditions (for example if you move from a dark room to a brightly lit room, or take your robot outside). What looks to be one color in a dark room may look different in bright sunlight.
You have to tell the light sensor what you mean by "dark" and what you mean by "light". When you write a program that checks the light sensor, the sensor returns a value between 0 and 100. 0 is the darkest and 100 is the lightest. When you calibrate the sensor, it asks you to show it the darkest color it will be detecting, and it sets that as the 0 value. Similarly, it asks to see the lightest color you will be detecting, and it sets that value to 100. We'll see how to calibrate the sensor later on.
Let's attach a light sensor to our robot.
Attaching the Light Sensor to the 5-minute Bot:
Take off the touch sensor from the last lesson so you are only left with the 5-minute Bot (below)
You will need these parts:
Now we can assemble and attach the light sensor:
Attach the light sensor to the underside of 5-minute-Bot
Connect the light sensor to Port 3 on the Brick. This is the default light sensor port.
That's it! You're ready to follow the light!
Calibrating the Light Sensor:
You can calibrate the light sensor directly from the NXT-G programming environment, or write your own program to do it. If you use the NXT programming environment directly, your brick needs to be plugged in to the computer for calibration. Since we're using laptops, and can move them around easily, we'll use that method.
Exercise 1: Calibrating the light sensor in the NXT-G programming environment
Exercise 2: Write a program to display the final light sensor readings
Let's write a program to view the output from the calibrated light sensor. We're using some advanced concepts here, including data wires, which carry information from one NXT programming block to another. Data wires will let us take the numbers that are output by the light sensor block, and use them as input to the display block. To input and output data to a NXT-G programming block, many blocks have what is called a data "hub" which clicks in and out of the data block. You can click on the hub to make it slide in and out. Below is a picture of the Display Block with the data hub closed, and opened. The ports on the data hub are for taking in and putting out different kinds of data, such as numbers, text and images.
To open the Data Hub, click on the line at the lower left of the Display Block.
A Display Block with the data hub open:
You will need to know how to display written information on the NXT brick's screen. The screen can write almost anything you tell it to, but it only understands numbers in "text" format. Letters and words are automatically "text", but numbers are not. To put the numerical (number) output from the light sensor into a form that the display block can understand, we will have to convert it to text. Create a program named "showlight.rbt" in the NXT-G programming window (be sure to save it to the shared drive!), and follow the steps below to write it.
Set the Display block to display the input Text:
Challenge 1: Write a program to make the robot drive until it detects a dark line and then stop. Use the paper that your instructors have provided, with electrical tape to make the dark lines.
Before you attempt this challenge, think about what it means for your robot to detect a dark line. Think about what the sensor readings will be as the sensor approaches the line. When the light sensor is over the white paper, the readings will be pretty high. When the sensor is just a little bit over the black line, less light will get reflected, so the light sensor values will start to drop. The more of the sensor that is over the black line, the lower the light sensor values will be.
Hint 1: Use the Wait for Sensor Block to let your robot keep moving until the value of the Light Sensor has dropped past a certain value. Think about what a reasonable value might be to indicate that the sensor is over the black line.
Hint 2: Make sure that the first Move Block in your program uses the unlimited setting, so that your program will check the light sensor while the robot is still driving. Otherwise, the robot will finish running the move block before it checks the light sensor.
Challenge 2: Your instructors have created a "ring" out of dark electrical tape on white paper. The ring has a hole in it, large enough for your robot to drive through. Write a program that allows your robot to find its way out of the ring without crossing over a black line. Hint: When you encounter a black line, you will want the robot to stop, turn, and travel in a different direction. So the pseudocode (description of your program in words) would look something like this:
Remember, each line in pseudocode corresponds to one instruction for the robot (and one programming "block" in NXT-G) Once you get the program working, experiment with different values for the turn. See which value works best for getting your robot out of the "ring" the fastest.
Challenge 3: (Advanced!) Your instructor has provided paper with dark parallel lines. Write a program that lets the robot drive over two black lines, and stop on the third. There are many ways to solve this problem. See how creative you can be! Talk to your instructors and see if your solution matches theirs.