0

I'm having to produce code whereby a Finch robot follows around an object after it is activated via a tap, and the program quits when it is tapped twice. I can't understand how I could do this. Right now, I'm trying to make it so that after the robot has been activated (it would be after the while(x==1) code), it will add 1 to 'tappedCount' each time the robot is tapped.

However, as the program will just about always be working within the other 'while' loops (which are within the if statements which check where the object is, one of these will always be activated due to the nature of the Finch. This means that the:

if(myf.isTapped())
    {
        tappedCount++;
    }
if(tappedCount==2)
    {
        System.out.println("Exiting Finch");
        System.exit(0);
        myf.quit();
    }

code never gets a chance to run.

Does anyone have suggestions as to how I could get that to run? or an alternative way to quit the program when the robot is tapped twice would be great. Thanks.

import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class FinchCode {

static long instant1;
static long instant2;
static long instant3;
static long instant4;
static long duration;
static long duration1;
static int x = 0;

static int Buzz = 300;
static int BuzzDuration = 1200;

static int R = 250;
static int G = 250;

static int velocityLeft = 150;
static int velocityRight = 150;

static int turnLeft = -100;
static int turnRight = -100;

static int time = 0;
static int tappedCount = 0;
static int millis;
public static void main(String[] args) throws InterruptedException{


    Finch myf = new Finch();    

    while(!myf.isBeakDown())
    {
        if(myf.isTapped()&& x==0)
        {
            if(myf.isObstacleLeftSide()&&myf.isObstacleRightSide())
            {
                x = 1;
                myf.setLED(R,0,0);
                myf.stopWheels();       
            }
        }
        while(x==1)
        {
            if(myf.isTapped())
            {
                tappedCount++;
            }
            if(tappedCount==2)
            {
                System.out.println("Exiting Finch");
                System.exit(0);
                myf.quit();
            }
            if(!myf.isObstacleLeftSide()&&!myf.isObstacleRightSide())
            {
                while(!myf.isObstacleLeftSide()&&!myf.isObstacleRightSide())
                {   
                    myf.setLED(0,G,0);
                    myf.setWheelVelocities(velocityLeft, velocityRight);
                }
                System.out.println("Forward");
            }
            if(!myf.isObstacleLeftSide()&&myf.isObstacleRightSide())
            {
                while(!myf.isObstacleLeftSide()&&myf.isObstacleRightSide())
                {
                    myf.setLED(0,G,0);
                    myf.setWheelVelocities(velocityLeft, turnRight);
                }
                System.out.println("Right");
            }
            if(myf.isObstacleLeftSide()&&!myf.isObstacleRightSide())
            {
                while(myf.isObstacleLeftSide()&&!myf.isObstacleRightSide())
                {
                    myf.setLED(0,G,0);
                    myf.setWheelVelocities(turnLeft, velocityRight);
                }
                System.out.println("Left");
            }
            if(myf.isObstacleLeftSide()&&myf.isObstacleRightSide())
            {
                while(myf.isObstacleLeftSide()&&myf.isObstacleRightSide())
                {
                    myf.setLED(R,0,0);
                    myf.stopWheels();                       
                }
                System.out.println("Stop");
            }
        }
    }
} 
9
  • You could use Boolean? True for follow, false for static, use a while loop? Follow? Commented Feb 25, 2016 at 3:59
  • Could you be a little more specific? Sorry, I'm a beginner in Java. Commented Feb 25, 2016 at 4:03
  • Sure let me try, I'm not at my computer but I'm on my phone.... Commented Feb 25, 2016 at 4:07
  • So im just gunna give you an example of what im saying, its by no means an answer, or a working piece, just an approach i thought of Commented Feb 25, 2016 at 4:11
  • do you havethe code for the finch class? Commented Feb 25, 2016 at 4:15

1 Answer 1

1
    boolean move = false;
    if(myf.isTapped())
    {
           move = true;
    }
     while(move)
        {
           //Code keeping the finch moving
           //check at the end of loop if the tapp has occured yet
           //if so reset to false, and the while loop will exit
           if(myf.isTapped())
           {
           move = false;
           }
        }
     //Exit program and print your stuff          
            System.out.println("Exiting Finch");
            System.exit(0);
            myf.quit();
Sign up to request clarification or add additional context in comments.

1 Comment

Not the answer you are looking for, but i think it could streamline some things make it simpler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.