SoundCtrCarModified
import lejos.nxt.*;
 
/**
 * 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 SoundCtrCar implements ButtonListener {
    private static int soundThreshold = 90;
    private static SoundSensor sound = new SoundSensor(SensorPort.S1);
    private static boolean running;
 
    public SoundCtrCar() {
        Button.ESCAPE.addButtonListener(this);
    }
 
    private static void waitForLoudSound() throws Exception {
        int soundLevel;
 
        Thread.sleep(500);
        do {
            if (!running) {
                break;
            }
            soundLevel = sound.readValue();
            LCD.drawInt(soundLevel, 4, 10, 0);
        } while (soundLevel < soundThreshold);
    }
 
    public static void main(String[] args) throws Exception {
        SoundCtrCar scc = new SoundCtrCar();
        running = true;
        LCD.drawString("dB level: ", 0, 0);
        LCD.refresh();
 
        while (running) {
            waitForLoudSound();
            LCD.drawString("Forward ", 0, 1);
            Car.forward(100, 100);
            if (!running) {
                break;
            }
 
            waitForLoudSound();
            LCD.drawString("Right   ", 0, 1);
            Car.forward(100, 0);
            if (!running) {
                break;
            }
 
            waitForLoudSound();
            LCD.drawString("Left    ", 0, 1);
            Car.forward(0, 100);
            if (!running) {
                break;
            }
 
            waitForLoudSound();
            LCD.drawString("Stop    ", 0, 1);
            Car.stop();
            if (!running) {
                break;
            }
        }
        Car.stop();
        LCD.clear();
        LCD.drawString("Program stopped", 0, 0);
        Thread.sleep(2000);
    }
 
    @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