Search code examples
javatimefinch

Java - Adding timed closure


I am trying to make my code quit if there is no light coming through any light sensors.

import edu.cmu.ri.createlab.terk.robot.finch.Finch;


public class RunProgram {

public static Finch LeFinch = new Finch();

public static boolean endProgram = false;

private static long WaitingTime = System.currentTimeMillis();

public static void main(String args[])
{

    LightSensors lightsensor = new LightSensors();

//do {

        while(ObjectSensor.Obstacle()==false || WaitingTime < 5000)
        {

            if (lightsensor.leftsensor() == true && lightsensor.rightsensor() == true) 
            {
                Movement.forward();
            } 
            else if (lightsensor.leftsensor() == true && lightsensor.rightsensor() == false) 
            {
                Movement.left();
                System.out.println("LEFT");
            } 
            else if (lightsensor.leftsensor() == false && lightsensor.rightsensor() == true) 
            {
                Movement.right();
                System.out.println("RIGHT");
            }
            else if (lightsensor.leftsensor() == false && lightsensor.rightsensor() == false) 
            {
                Movement.stop();
            } 

        }System.out.println("Object Detected");

//  } while(endProgram == false);

}

I have tried using System.currentTimeMillis and creating a while loop that will stop running once its over 5000 milliseconds, but this does not seem to work.

This is using the finch api.

I have updated the code, I have decided to use a counter which terminates the application once it reaches 5000+

However, this value is not resetting once a light is has been shined onto the finch.

static long counterTime = 0;

while(counterTime < 5000)
        {

            if (lightsensor.leftsensor() == true && lightsensor.rightsensor() == true) 
            {
                Movement.forward();
                counterTime = 0;
            } 
            else if (lightsensor.leftsensor() == true && lightsensor.rightsensor() == false) 
            {
                Movement.left();
                System.out.println("LEFT");
                counterTime = 0;
            } 
            else if (lightsensor.leftsensor() == false && lightsensor.rightsensor() == true) 
            {
                Movement.right();
                System.out.println("RIGHT");
                counterTime = 0;
            }
            else 
            {
                Movement.stop();
                counterTime = System.currentTimeMillis() - startTime;
                System.out.println(counterTime);
            }

        }endProgram = true;

Solution

  • You need to update the time when you last saw light (or whatever other event you want to use to reset the timeout):

    long lastLightTime = System.nanoTime();
    while(ObjectSensor.Obstacle()==false
        || System.nanoTime()-lastLightTime < TimeUnit.SECONDS.toNanos(5)) {
      if (lightsensor.leftsensor() || lightsensor.rightsensor()) {  // Or other condition to reset timeout
        lastLightTime = System.nanoTime();
      }
    
      // The rest of your loop...
    }
    

    Why System.nanoTime()? That's the correct way to measure elapsed time. (see How do I measure time elapsed in Java?).