Advertisement
Guest User

SoundCtrCarClap

a guest
Mar 2nd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 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 SoundSensor sound = new SoundSensor(SensorPort.S1);
  12.     private static boolean running = true;
  13.     private static DataLogger dl = new DataLogger("Sample.txt");
  14.    
  15.     private static  void waitForLoudSound() throws Exception
  16.     {
  17.         if (running) {
  18.             int soundLevel1;
  19.             int soundLevel2;
  20.             int soundLevel3;
  21.             do
  22.             {
  23.                 soundLevel1 = sound.readValue();
  24.                 dl.writeSample(soundLevel1);
  25.                 Thread.sleep(25);
  26.                 soundLevel2 = sound.readValue();
  27.                 dl.writeSample(soundLevel2);
  28.                 Thread.sleep(250);
  29.                 soundLevel3 = sound.readValue();
  30.                 dl.writeSample(soundLevel3);
  31.                 LCD.drawString("" + soundLevel1 + " " + soundLevel2 + " " + soundLevel3, 0, 0);
  32.             }
  33.             while ((soundLevel1 >= 50 || soundLevel2 <= 85 || soundLevel3 >= 50) && running);
  34.         }
  35.     }
  36.  
  37.     public static void main(String [] args) throws Exception
  38.     {
  39.         Button.ESCAPE.addButtonListener(new ButtonListener() {
  40.             public void buttonReleased(Button b) {
  41.                 LCD.drawString("ESCAPE pressed", 0, 0);    
  42.             }
  43.             public void buttonPressed(Button b) {
  44.                 running = false;
  45.             }
  46.         });
  47.        
  48.         LCD.drawString("dB level: ",0,0);
  49.         LCD.refresh();
  50.            
  51.         while (! Button.ESCAPE.isDown() && running)
  52.         {
  53.             waitForLoudSound();                        
  54.             LCD.drawString("Forward ",0,1);
  55.             Car.forward(75, 75);
  56.  
  57.             waitForLoudSound();                        
  58.             LCD.drawString("Right   ",0,1);
  59.             Car.forward(75, 0);
  60.  
  61.             waitForLoudSound();                        
  62.             LCD.drawString("Left    ",0,1);
  63.             Car.forward(0, 75);
  64.  
  65.             waitForLoudSound();                        
  66.             LCD.drawString("Stop    ",0,1);
  67.             Car.stop();
  68.        }
  69.        dl.close();
  70.        Car.stop();
  71.        LCD.clear();
  72.        LCD.drawString("Program stopped", 0, 0);
  73.        Thread.sleep(2000);
  74.    }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement