Advertisement
Guest User

reggie

a guest
Apr 20th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. import josx.platform.rcx.*;
  2. import java.util.Random;
  3.  
  4. /**
  5.  * Team 5 ListenerTest.java
  6.  * 2005-03-02
  7.  * ELEG 467-015
  8.  */
  9. public class ListenerTest
  10. {
  11.    public static void main (String[] aArg) throws Exception
  12.    {
  13.       // Reggie generates random numbers so that he can turn
  14.       // when he senses touch and not become stuck in a corner or
  15.       // a box-like structure such as chair legs.
  16.       final Random generator = new Random(19580427);
  17.      
  18.       Motor.A.forward();
  19.       Motor.B.forward();
  20.      
  21.       Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_BOOL);
  22.       Sensor.S2.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_BOOL);
  23.  
  24.       // Listener for sensor 1
  25.       Sensor.S1.addSensorListener(new SensorListener()
  26.       {
  27.          public void stateChanged( Sensor sensor1, int oldValue, int newValue) {
  28.  
  29.             // If the sensor receives input...
  30.             if (newValue == 1) {
  31.                Motor.A.stop();
  32.                Motor.B.stop();  
  33.                
  34.                try {
  35.                   Thread.sleep(500);
  36.                }
  37.                catch(Exception e) {}
  38.                
  39.                Motor.A.backward();
  40.                Motor.B.backward();
  41.                
  42.                try {
  43.                   Thread.sleep(generator.nextInt(500)+75);
  44.                }
  45.                catch(Exception e) {}
  46.                
  47.                // Stop the first motor and make it go forward
  48.                // while the second motor is still backing up
  49.                Motor.A.stop();
  50.                Motor.A.forward();
  51.  
  52.                try {
  53.                   Thread.sleep(generator.nextInt(600)+75);
  54.                }
  55.                catch(Exception e) {}
  56.                
  57.                Motor.B.stop();
  58.                Motor.B.forward();
  59.             }
  60.          }
  61.       });
  62.      
  63.       Sensor.S2.addSensorListener(new SensorListener()
  64.       {
  65.          public void stateChanged(Sensor sensor2, int oldValue, int newValue) {
  66.  
  67.             if (newValue == 1) {
  68.                Motor.B.stop();
  69.                Motor.A.stop();  
  70.                  
  71.                try {
  72.                   Thread.sleep(500);
  73.                }
  74.                catch(Exception e) {}
  75.                  
  76.                Motor.B.backward();
  77.                Motor.A.backward();
  78.                
  79.                try {
  80.                   Thread.sleep(generator.nextInt(500)+75);
  81.                }
  82.                catch(Exception e) {}
  83.                
  84.                // Stop the second motor and make it go forward
  85.                // while the first motor is still backing up
  86.                Motor.B.stop();
  87.                Motor.B.forward();
  88.  
  89.                try {
  90.                   Thread.sleep(generator.nextInt(600)+75);
  91.                }
  92.                catch(Exception e) {}
  93.                
  94.                Motor.A.stop();
  95.                Motor.A.forward();
  96.             }
  97.          }
  98.       });
  99.      
  100.       try {
  101.          Button.VIEW.waitForPressAndRelease();
  102.       }
  103.       catch(InterruptedException e) {
  104.       // maybe do something here
  105.       }
  106.    }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement