0

I am supposed to make my finch follow a light source, with the following requirements:

-make the finch move towards the light source - set led colour to blue when light source is detected

  • if no light source, finch doesn't move.
  • sets led colour to red.

But I'm getting a syntax error on the last bit.

myf.quit(); (unreachable code is the error) 

And my code also doesn't do the required tasks it seems. Where would the issues be?

My code:

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

public static void main(String[] args) {

    Finch myf = new Finch();
    int[] lightsensors =  myf.getLightSensors();
    int meetsthreshold = 300;
    int lightsensorsaverage = 0;

    while(true){

        lightsensors =  myf.getLightSensors();
        lightsensorsaverage = (lightsensors[0]+lightsensors[1])/2;

        if (lightsensorsaverage < meetsthreshold)
        {
            myf.stopWheels();
            myf.setLED(100, 0, 0);

            while (lightsensorsaverage < meetsthreshold){

                lightsensors =  myf.getLightSensors();
                lightsensorsaverage = (lightsensors[0]+lightsensors[1])/2;

                if (lightsensorsaverage > meetsthreshold){

                    myf.setLED(0, 0, 100);
                    myf.setWheelVelocities(100, 100);


                }
            }


            }
        }

    myf.quit();
    System.exit(0);

    }


}
jjj
  • 83
  • 7
  • You never end the outer `while (true)` loop, so `myf.quit();` is never reached. – Mark Rotteveel Feb 22 '16 at 15:50
  • @MarkRotteveel I see, So how would I end it? Or is that not the right way to use the while loop – jjj Feb 22 '16 at 15:54
  • 1
    To end a while loop, you either use `break;`, or use a condition in the `while` that ends the loop when it is done. I have no clue what you are trying to achieve with that code, nor why you have put it in a `while (true)` loop, as such I can't tell you when/where you should end it. – Mark Rotteveel Feb 22 '16 at 15:56
  • @MarkRotteveel so I tweeked the braces around and managed to get the error solved. But my code seems to not run. It says 'connecting to finch this may take a few seconds....' but I assume there's something wrong with my code and not the finch since I ran a sample (example) code which seems to work. – jjj Feb 22 '16 at 15:59
  • I am attempting to check the light levels using the light sensors on the finch and I though I'd use a while loop to keep checking if the right light levels are met for the finch to do the tasks. – jjj Feb 22 '16 at 16:01

0 Answers0