Now, let's give the robot a restricted space to move. The robot here is the small car which we can build by following the small booklet provided with the NXT kit. Let us draw a black line around the robot's space, to identify its boundary. When the robot touches the black line, it is supposed to take reverse and try another time to escape the boundary. The question is how the robot notices the black boundary? Yes, we have to use a Light Sensor. Light Sensor measures the light intensity in a room and converts it into meaningful values. It passes the values to the NXT. NXT can then use a calibration program to convert the light sensor values into minimum and maximum. Calibration process allows the sensor to get the readings within an acceptable range which then makes programming easier. Please take a look at below video to see the different ways provided by NXT to calibrate the light sensor. You can go ahead with any one way and calibrate your sensor.
Let's start to write a program which then uses the calibrated values to understand robot's boundary. It is going to be a two-step program.
- Keep moving forward until the robot hits a black boundary line (Light Sensor helps to identify the black boundary line)
- When robot hits the black boundary line, drive backwards for 2 seconds
Keep repeating the above steps and observe robot’s behavior. Now let's program these steps with the help of NXT-G programming language. Drag and Drop the Switch Block from the left Palette to the Work Area. This Switch has two parts, the upper part and the lower part. The upper part controls the unusual behavior of the robot. The lower part controls the default behavior of the robot. Now, click on the Switch and see its configuration panel at the bottom of the screen. Please select the below configuration fields and their values.
Control - Sensor
Sensor - Light Sensor
Port - The port number where light sensor is connected (in my case port 3)
Light - Greater than 20 (which means when the light intensity exceeds 20% of the maximum value)
This configuration means that the light sensor will consider darkness when the illumination is about 20% of maximum and it will consider brightness when the illumination exceeds 20% of maximum. Please take a look at the below figure to see my configuration of Switch Block.
As a next step, let us drag and drop a Motor Block in the upper part of the Switch. This Motor Block will get activated when the robot finds brightness. Click on the Motor Block and see its configuration panel at the bottom of the screen. I would like the robot to keep moving forward until it is in brightness. I selected the motors B and C in order for robot to move forward for unlimited duration. Keep rest all the configuration the same. Please see the below configuration of my Motor Block on detection of light.
Next, let us drag and drop another Motor Block in the lower part of the Switch. This motor block will get activated when the robot encounters the black line. Click on this Motor Block and see its configuration panel at the bottom of the screen. Since I would like the motor to go reverse for 2 seconds on occurrence of the black boundary, I have selected Motor B and C of the robot, with Direction field to be reverse and Duration to be 2 seconds. I kept rest all the fields to be default. Please take a look at the figure below to check my configuration of the Motor Block.
Further, configure the robot to keep moving between light and dark indefinitely. In order to achieve this please put the entire configuration above in a Loop. Click on this loop and see its configuration at the bottom of the screen below. Please select the Control field to be Forever. Please see my final NXT-G program below.
Download this program on the NXT brick and see the movement of the robot. The below video clip captures the movement of my robot.
As seen in the video, the light sensor is mounted on the front part of the robot, about 2 mm above from the surface and has a red light emitted out of it. The robot touches the black boundary line twice and then reverses back for 2 seconds.
For learning about Lego NXT blocks please visit the site below.
Hope you enjoyed the article!