import lejos.nxt.*; public class SoundCtrCar { private static boolean exit = false; private static void display(int dB) { LCD.drawInt((int) (Runtime.getRuntime().freeMemory()), 0, 0); LCD.drawInt(dB, 4, 0, 1); LCD.refresh(); } // STOP = 0, LEFT = 1, RIGHT = 2, FORWARD = 3; private final static int SPEED = 600; private final static int soundThreshold = 50; public static void main(String[] args) throws Exception { int state = 3; SoundSensor sound = new SoundSensor(SensorPort.S4); Button.ESCAPE.addButtonListener(new ButtonListener() { public void buttonPressed(Button arg0) { if (arg0 == Button.ESCAPE) { exit = true; } } public void buttonReleased(Button arg0) { } }); while (!exit) { Thread.sleep(500); while ((sound.readValue() < soundThreshold) && !exit) display(sound.readValue()); LCD.drawInt(state + 1, 4, 0, 2); if (!exit) Locomotion.drive((state & 1) != 0 ? SPEED : 0, (state & 2) != 0 ? SPEED : 0); state = (state + 1) % 4; } Locomotion.stop(); LCD.clear(); LCD.drawString("Program stopped", 0, 0); LCD.refresh(); } }