DriveTowardsLight
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;
import lejos.nxt.LightSensor;
import lejos.nxt.addon.RCXLightSensor;
 
/**
 * The locomotions of a LEGO 9797 car is controlled by sound detected through a
 * microphone on port 1.
 * 
 * @author Ole Caprani
 * @version 23.08.07
 */
public class DriveTowardsLight extends Behavior {
 
    private RCXLightSensor leftSensor;
    private RCXLightSensor rightSensor;
    private int lightThreshold = 350;
 
    public DriveTowardsLight(String name, int LCDrow, Behavior subsumedBehavior) {
        super(name, LCDrow, subsumedBehavior);
        leftSensor = new RCXLightSensor(SensorPort.S4);
        rightSensor = new RCXLightSensor(SensorPort.S3);
    }
 
    public void run() {
        leftSensor.activate();
        rightSensor.activate();
 
        while (true) {
            int leftLight = 0;
            int rightLight = 0;
 
            while (leftLight < lightThreshold && rightLight < lightThreshold) {
                leftLight = 1023 - SensorPort.S4.readRawValue();
                rightLight = 1023 - SensorPort.S3.readRawValue();
 
                LCD.drawInt(leftLight, 0, 5);
                LCD.drawInt(rightLight, 10, 5);
            }
 
            suppress();
 
            drawString("f");
            forward(50 + leftLight/5, 50 + rightLight/5);
            delay((int)(500+500*Math.random()));
 
            drawString("s");
            stop();
            delay((int)(1500+1000*Math.random()));
 
            release();
 
        }
    }
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License