Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- {
- private static SoundSensor sound = new SoundSensor(SensorPort.S1);
- private static boolean running = true;
- private static DataLogger dl = new DataLogger("Sample.txt");
- private static void waitForLoudSound() throws Exception
- {
- if (running) {
- int soundLevel1;
- int soundLevel2;
- int soundLevel3;
- do
- {
- soundLevel1 = sound.readValue();
- dl.writeSample(soundLevel1);
- Thread.sleep(25);
- soundLevel2 = sound.readValue();
- dl.writeSample(soundLevel2);
- Thread.sleep(250);
- soundLevel3 = sound.readValue();
- dl.writeSample(soundLevel3);
- LCD.drawString("" + soundLevel1 + " " + soundLevel2 + " " + soundLevel3, 0, 0);
- }
- while ((soundLevel1 >= 50 || soundLevel2 <= 85 || soundLevel3 >= 50) && running);
- }
- }
- public static void main(String [] args) throws Exception
- {
- Button.ESCAPE.addButtonListener(new ButtonListener() {
- public void buttonReleased(Button b) {
- LCD.drawString("ESCAPE pressed", 0, 0);
- }
- public void buttonPressed(Button b) {
- running = false;
- }
- });
- LCD.drawString("dB level: ",0,0);
- LCD.refresh();
- while (! Button.ESCAPE.isDown() && running)
- {
- waitForLoudSound();
- LCD.drawString("Forward ",0,1);
- Car.forward(75, 75);
- waitForLoudSound();
- LCD.drawString("Right ",0,1);
- Car.forward(75, 0);
- waitForLoudSound();
- LCD.drawString("Left ",0,1);
- Car.forward(0, 75);
- waitForLoudSound();
- LCD.drawString("Stop ",0,1);
- Car.stop();
- }
- dl.close();
- Car.stop();
- LCD.clear();
- LCD.drawString("Program stopped", 0, 0);
- Thread.sleep(2000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement