Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import josx.platform.rcx.*;
- import java.util.Random;
- /**
- * Team 5 ListenerTest.java
- * 2005-03-02
- * ELEG 467-015
- */
- public class ListenerTest
- {
- public static void main (String[] aArg) throws Exception
- {
- // Reggie generates random numbers so that he can turn
- // when he senses touch and not become stuck in a corner or
- // a box-like structure such as chair legs.
- final Random generator = new Random(19580427);
- Motor.A.forward();
- Motor.B.forward();
- Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_BOOL);
- Sensor.S2.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_BOOL);
- // Listener for sensor 1
- Sensor.S1.addSensorListener(new SensorListener()
- {
- public void stateChanged( Sensor sensor1, int oldValue, int newValue) {
- // If the sensor receives input...
- if (newValue == 1) {
- Motor.A.stop();
- Motor.B.stop();
- try {
- Thread.sleep(500);
- }
- catch(Exception e) {}
- Motor.A.backward();
- Motor.B.backward();
- try {
- Thread.sleep(generator.nextInt(500)+75);
- }
- catch(Exception e) {}
- // Stop the first motor and make it go forward
- // while the second motor is still backing up
- Motor.A.stop();
- Motor.A.forward();
- try {
- Thread.sleep(generator.nextInt(600)+75);
- }
- catch(Exception e) {}
- Motor.B.stop();
- Motor.B.forward();
- }
- }
- });
- Sensor.S2.addSensorListener(new SensorListener()
- {
- public void stateChanged(Sensor sensor2, int oldValue, int newValue) {
- if (newValue == 1) {
- Motor.B.stop();
- Motor.A.stop();
- try {
- Thread.sleep(500);
- }
- catch(Exception e) {}
- Motor.B.backward();
- Motor.A.backward();
- try {
- Thread.sleep(generator.nextInt(500)+75);
- }
- catch(Exception e) {}
- // Stop the second motor and make it go forward
- // while the first motor is still backing up
- Motor.B.stop();
- Motor.B.forward();
- try {
- Thread.sleep(generator.nextInt(600)+75);
- }
- catch(Exception e) {}
- Motor.A.stop();
- Motor.A.forward();
- }
- }
- });
- try {
- Button.VIEW.waitForPressAndRelease();
- }
- catch(InterruptedException e) {
- // maybe do something here
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement