j panton
j panton

Reputation: 232

How to test for if statement while executing while loops

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");
            }
        }
    }
} 

Upvotes: 0

Views: 77

Answers (1)

R.Hull
R.Hull

Reputation: 136

    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();

Upvotes: 1

Related Questions