Advertisement
Guest User

SoundCtrCarEscapeButton

a guest
Mar 2nd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. import lejos.nxt.*;
  2. /**
  3.  * The locomotions of a  LEGO 9797 car is controlled by
  4.  * sound detected through a microphone on port 1.
  5.  *
  6.  * @author  Ole Caprani
  7.  * @version 23.08.07
  8.  */
  9. public class SoundCtrCar
  10. {
  11.     private static int soundThreshold = 90;
  12.     private static SoundSensor sound = new SoundSensor(SensorPort.S1);
  13.     private static boolean running = true;
  14.    
  15.     private static  void waitForLoudSound() throws Exception
  16.     {
  17.         if (running) {
  18.             int soundLevel;
  19.             Thread.sleep(500);
  20.             do
  21.             {
  22.                 soundLevel = sound.readValue();
  23.                 LCD.drawInt(soundLevel,4,10,0);
  24.             }
  25.             while ( soundLevel < soundThreshold && running);
  26.         }
  27.     }
  28.  
  29.     public static void main(String [] args) throws Exception
  30.     {
  31.         Button.ESCAPE.addButtonListener(new ButtonListener() {
  32.             public void buttonReleased(Button b) {
  33.                 LCD.drawString("ESCAPE pressed", 0, 0);    
  34.             }
  35.             public void buttonPressed(Button b) {
  36.                 running = false;
  37.             }
  38.         });
  39.        
  40.         LCD.drawString("dB level: ",0,0);
  41.         LCD.refresh();
  42.            
  43.         while (! Button.ESCAPE.isDown() && running)
  44.         {
  45.             waitForLoudSound();                        
  46.             LCD.drawString("Forward ",0,1);
  47.             Car.forward(100, 100);
  48.  
  49.             waitForLoudSound();                        
  50.             LCD.drawString("Right   ",0,1);
  51.             Car.forward(100, 0);
  52.  
  53.             waitForLoudSound();                        
  54.             LCD.drawString("Left    ",0,1);
  55.             Car.forward(0, 100);
  56.  
  57.             waitForLoudSound();                        
  58.             LCD.drawString("Stop    ",0,1);
  59.             Car.stop();
  60.        }
  61.        Car.stop();
  62.        LCD.clear();
  63.        LCD.drawString("Program stopped", 0, 0);
  64.        Thread.sleep(2000);
  65.    }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement