Lab 2, CS 106

Background and Motivation: In this lab you will use the light sensors on the Scribbler to control the robot's behavior. Your programs will use if, if-else, and nested if statements to make decisions.

Step 1: Move Straight Away From the Light
Download the files from here and save them in a new directory in your account.

Class Part1 in project Lab2 contains the skeleton code for a program that senses the amount of light hitting the center light sensor. If bright light is detected (e.g., from a flashlight) the robot should move straight away from the light; otherwise it should stop. Also, the back LED on the Fluke should be turned on when the robot is moving and turned off when it's stopped.

There are several portions of code that you'll need to write in Part1, all marked with comments beginning with TODO. (Note that the method setLEDBack is used to turn the Fluke LED on or off.) Make these changes and test the program.

Step 2: Move Directionally Away From the Light

Create a new class named Part2 in project Lab2. This program will move away from the light based on the direction from which the light is coming. The pseudocode describing the behavior is:

Create a Scribbler object and connect it to your robot
Ask the user for the number of seconds to run
For that amount of time do the following
  if bright light is detected from the center sensor
    start moving forward at full speed
    turn on only the Fluke LED
  else if bright light is detected from the left sensor
    start moving toward the left
    turn on only the left LED on the Scribbler
  else if bright light is detected from the right sensor
    start moving toward the right
    turn on only the right LED on the Scribbler
  else
    stop the robot
    turn off all LEDs
Stop the robot
Turn off all LEDs
Close the connection
The Java statements you'll need in this program to make the decisions will be of the following form:
if ( condition-1 )
{
  // statements executed if condition-1 is true
}
else if ( condition-2 )
{
  // statements executed if condition-1 is false and condition-2 is true
}
else if( condition-3 )
{
  // statements executed if condition-1 and condition-2 are both false, and
  // condition-3 is true
}
else
{
  // statements executed if condition-1, condition-2, and condition-3
  // are all false
}

You should use either method motors or method move to turn the robot left and right. (For example, motors(1.0, 0.3); or move(1.0, -1.0); will move the robot toward the right.) Also, method setLED should be used to turn the Scribbler LEDs on and off. Look at the documentation if you have questions about parameters for these methods.

When done, please demonstrate steps 1 and 2 to Matt.

Submit your java (Part1.java and Part2.java) code via moodle.  
 
Additional Challenges
Have the robot move faster the longer the light is shining on any of the sensors. For example, when the robot starts moving right after it's been stopped, it might move at half speed, but as it continues to move the speed gradually increases until it moves as fast as it can. Also, change the behavior of the LEDs to indicate how fast the robot is moving (either more leds = faster, or try showing the speed in binary, counting from 0 to 7)!