0

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;
LyanR
  • 31
  • 2
  • 3
  • 10
  • If you don't update your WaitingTime from the time when your class loads, the second half of the condition is always false (unless you are living in January 1970); its value is set to something like 1423177824000 and then not changed. – Andy Turner Feb 05 '15 at 23:08
  • Can you advise me as to where to put the WaitingTime update? @AndyTurner – LyanR Feb 05 '15 at 23:11
  • @LyanR - see the code below. I renamed the variable "initialTime", then compute elapsed time directly in your "while()" statement. – FoggyDay Feb 05 '15 at 23:26
  • On a point of readability, you don't need to use `lightsensor.leftsensor() == true` - that is exactly the same as `lightsensor.leftsensor()` in a conditional (similarly for false - simply `!lightsensor.leftsensor()`). – Andy Turner Feb 05 '15 at 23:27
  • @LyanR - Andy Turner is correct. "== true" and "== false" is redundant (although not "wrong"). Additionally, you could use either "System.currentTimeMillis()" or "System.nanoTime()". currentTimeMillis() has been around since Java 1.0; it's probably OK for your purposes. System.nanoTime()" was introduced in JDK 1.5 (a long time ago!), and it's required if you demand precision. – FoggyDay Feb 05 '15 at 23:31

3 Answers3

0

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?).

Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

Several things:

1) Make sure you update elapsed time:

public class RunProgram {

  private static long initialTime = System.currentTimeMillis();

  public static void main(String args[]) {
    LightSensors lightsensor = new LightSensors();

    while(ObjectSensor.Obstacle()== false && (SystemcurrentTimeMillis() - initialTime < 5000) {
       ...

2) You want to continue looping while "no obstacle" AND "elapsed time < 5000). In other words, if EITHER is true, then you want to exit the "while()" loop.

3) You probably also want to set "endProgram" "true" when "no light" OR "elapsed time exceeded"

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • I am not sure that your code really does the right thing - the condition means that it will stop as soon as no is obstacle sensed, provided 5s has elapsed - the question states that the code should 'quit if there is no light coming through any light sensors', which is quite different. – Andy Turner Feb 05 '15 at 23:32
0

You're missing the actual calculation. You need to check the time passed since the moment your WaitingTime value was calculated (which happens on class load) and the time when you actually verify your time, which happens on your loop condition (just like Andy said in his answer). As per System.currentTimeMillis() documentation:

Returns: the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

So WaitingTime is not a waiting time, just the time since January 1st, also known as Epoch Time (this is due to historical reasons). You need to verify the difference between both values in order to have an estimate over the elapsed time:

while (... System.currentTimeMillis() - startTime < 5000) {
 // 

where startTime is your initial time.

Also, you might prefer to use System.nanoTime() if you find yourself needing extra accuracy in your time windows or TimeUnit.SECONDS methods to convert seconds to milliseconds, which can improve readability when dealing with larger windows.

fsb
  • 111
  • 1
  • 5
  • This does not seem to close the application, I've tried specifying the where it needs to check the time, but this does not seem to be working. – LyanR Feb 06 '15 at 01:13