如果没有光通过任何光传感器,我正试图让我的代码退出。
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);
}
我曾尝试使用 System.currentTimeMillis 并创建一个 while 循环,该循环将在超过 5000 毫秒后停止运行,但这似乎不起作用。
这是使用 finch api。
我已经更新了代码,我决定使用一个计数器,一旦达到 5000+ 就终止应用程序
但是,一旦灯光照射到雀上,这个值就不会重置。
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;