Braitenberg2a.java
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 Braitenberg2a implements ButtonListener {
    private static RCXLightSensor leftSensor = new RCXLightSensor(SensorPort.S2);
    private static RCXLightSensor rightSensor = new RCXLightSensor(SensorPort.S1);
 
    private static boolean running;
 
    public Braitenberg2a() {
        Button.ESCAPE.addButtonListener(this);
        leftSensor.activate();
        rightSensor.activate();
    }
 
    public static void main(String[] args) throws Exception {
        Braitenberg2a b = new Braitenberg2a();
        SensorConverter sc = new SensorConverter(300, 301);
        running = true;
        LCD.drawString("dB level: ", 0, 0);
        LCD.refresh();
 
        while (running) {
            int leftLight = 1023 - SensorPort.S2.readRawValue();
            int rightLight = 1023 - SensorPort.S1.readRawValue();
            int lp = sc.normalize(leftLight);
            int rp = sc.normalize(rightLight);
            Car.forward(lp, rp);
            Thread.sleep(50);
        }
        Car.stop();
        LCD.clear();
        LCD.drawString("Program stopped", 0, 0);
        Thread.sleep(2000);
    }
 
    @Override
    public void buttonPressed(Button arg0) {
    }
 
    @Override
    public void buttonReleased(Button arg0) {
        running = false;
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License