PartyFinder
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;
import lejos.nxt.SoundSensor;
 
/**
 * 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 PartyFinder implements ButtonListener {
    private static SoundSensor leftSensor = new SoundSensor(SensorPort.S1, false);
    private static SoundSensor rightSensor = new SoundSensor(SensorPort.S2, false);
    private static int rightCali = 0;
    private static int leftCali = 0;
    private static int minPower = 60;
 
    private static boolean running;
 
    public PartyFinder() {
        Button.ESCAPE.addButtonListener(this);
    }
 
    public static void main(String[] args) throws Exception {
        PartyFinder pf = new PartyFinder();
        running = true;
        LCD.drawString("dB level: ", 0, 0);
        LCD.refresh();
        int leftNoise, rightNoise;
 
        waitForCalibrate();
 
        while (running) {
            rightNoise = rightSensor.readValue() + rightCali;
            leftNoise = leftSensor.readValue() + leftCali;
 
            LCD.drawString("Leftsensor:  " + leftNoise, 0, 0);
            LCD.drawString("Rightsensor: " + rightNoise, 0, 1);
 
            if (rightNoise > leftNoise) {
                Car.forward(100, 50);
            } else {
                Car.forward(50, 100);
            }
 
            LCD.drawString("Leftsensor:  " + leftNoise, 0, 0);
            LCD.drawString("Rightsensor: " + rightNoise, 0, 1);
            /*
            int leftPower = minPower + Math.max(rightNoise - 10, 0);
            int rightPower = minPower +  Math.max(leftNoise - 10, 0);
 
            LCD.drawString("Leftpower: " + leftPower + " ", 0, 4);
            LCD.drawString("Rightpower: " + rightPower +  " ", 0, 5);
 
            Car.forward(leftPower, rightPower);
            */
            Thread.sleep(50);
        }
        Car.stop();
        LCD.clear();
        LCD.drawString("Program stopped", 0, 0);
        Thread.sleep(2000);
    }
 
    public static void waitForCalibrate() throws Exception {
        Thread.sleep(2000);
 
        int rightNoise = rightSensor.readValue();
        int leftNoise = leftSensor.readValue();
 
        int diff = (rightNoise - leftNoise) / 2;
 
        rightCali = -diff;
        leftCali = diff;
 
        LCD.drawString("Leftcali:  " + leftCali + " " + leftNoise, 0, 2);
        LCD.drawString("Rightcali: " + rightCali + " " + rightNoise, 0, 3);
 
        return;
    }
 
    @Override
    public void buttonPressed(Button arg0) {
        // TODO Auto-generated method stub
 
    }
 
    @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