Skip to main content Skip to navigation

CS313 Robot Lab 2

In Lab 1 we looked at how to control motors and avoid obstacles using the ultrasonic sensor. In this lab we will be using the light sensor.

The light sensor measures the intensity of the light captured. Light sensors use the LightSensor class.

Extract and open the RobotLabLight Project in Atom. Open the file RobotLabLight.java. To compile and run your code you can press F9 (make sure your robot is connected via the USB 2.0 port and is turned on).

The code we have given you contains the following lines:

import lejos.nxt.*;

public class RobotLabLight {
  public static void main(String[] args) throws Exception {
      LightSensor light = new LightSensor(SensorPort.S1);

      while (true) {
        LCD.drawInt(light.getLightValue(), 4, 0, 0);
      } //end while

    }//end class main
}//end RobotLabLight

To use a light sensor, you create an instance of it using the constructor. We can then use the method getLightValue() to return a number between 0 and 100 (the brighter the light the higher the light value) and print it to the LCD.

Remember that ambient (surrounding) conditions can affect the performance of light sensors. Therefore, it is important to calibrate these sensors for a particular environment.


Line following using light sensors

There are many ways to program your robot to follow a line and a quick Google search with turn up many resourses.

One of the simplest approaches is illustrated below:

  1. When both sensors 1 & 2 see white the robot should move forward
  2. When sensor 1 sees black and sensor 2 sees white, the robot should turn to the left
  3. When sensor 2 sees black and sensor 1 sees white, the robot should turn to the right
  4. When both sensors see black, the robot is at a junction

Remember that the light sensor returns a reading between 0 and 100. You will need to investigate what is an appropriate threshold between black and white.

linefollow


Task 2.1 Show that your robot is able to follow a line and detect a junction. Can you make the motion smooth? Look ahead to the final task and think about how you will detect each of the scenarios your robot might encounter


In this lab, you have learned about how the light sensor works and have applied that to the robot.

If you have finished this lab early, feel free to move on to Lab 3.