Hi !

I'm just starting out in the world of programming, lejos/java being my
first language. With the help of some nice people on another message
board, i've got some code together to get my wheeled robot cruising
around and reacting differently when it hits a left or right bumper.

However, i'd like to implement an exception during the "Thread.sleep"
which, if a touch sensor is pressed, it reacts again. As it is now, it
just continues what it's doing until the end of the "Thread.sleep"
regardless of any other input. Sorry, i'm not really wording it well,
am I?

Can someone with some experience of what i'm trying to do please help?

Also, can someone give me some ideas of where to take this project next?

<<Here's my Code>>

import josx.platform.rcx.*;
import josx.robotics.*;

public class React {
public static void main(String[] args) {
        // The robot will run forever (i.e. this loop will run forever)
        while(true) {// This loop will run forever

                // Robot will move forward
                Motor.A.forward();
                Motor.C.forward();

        // Test if robot has hit RHS sensor
        if( hitRightSensor() ) {
                backUpAndTurnLeft();
                }

        // Test if robot has hit LHS sensor
        if( hitLeftSensor() ) {
                backUpAndTurnRight();
                }
        }

}


// Return true if the right sensor is touching an object
public static boolean hitRightSensor() {
        return  Sensor.S3.readBooleanValue(); //the sensor returns a false if
it's pressed.
        }
        


// Return true if the left sensor is touching an object
public static boolean hitLeftSensor() {
        return  Sensor.S1.readBooleanValue(); //the sensor returns a false if
it's pressed.
        }

public static void backUp() {
// Backup for 400. This gives some time to clear the object
        try {
                Motor.A.backward();
                Motor.C.backward();
                Thread.sleep(400);
                } catch(InterruptedException e) {}
        }

public static void backUpAndTurnRight() {

        do {
                backUp();

        // Turn right for 1800.
        try {
                Motor.A.forward();
                Thread.sleep(1800);
                } catch(InterruptedException e) {}

                } while( hitLeftSensor() );
        }


public static void backUpAndTurnLeft() {

        do {
                backUp();

        // Turn left for 1800.
        try {
                Motor.C.forward();
                Thread.sleep(1800);
                } catch(InterruptedException e) {}
                
                } while( hitRightSensor() );

        }

}

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Lejos-discussion mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/lejos-discussion

Reply via email to