所以当我进入我的房间时,我正在制作一个打开或关闭我的色调灯的程序。在我的房间里安装了2个HCSr04,用于检测来电人员的方向。该程序大多数工作正常,但在很短的时间后,它只是冻结,没有任何反应。它在Raspbian的Rapsberry Pi 3上运行。
以下是代码:
public class Distance {
//GPIO Pins
private static GpioPinDigitalOutput sensorTriggerPin;
private static GpioPinDigitalInput sensorEchoPin;
private static GpioPinDigitalOutput sensorTriggerPin1;
private static GpioPinDigitalInput sensorEchoPin1;
int f = 0;
ZoneId id;
public static double distance2;
public static double distance3;
public static boolean left = false;
public static boolean right = false;
public static long timeleft;
public static long timeright;
public int counter;
final static GpioController gpio = GpioFactory.getInstance();
public static void main(String[] args) throws InterruptedException {
System.out.println("Starting...");
ZoneId id = ZoneId.of("Europe/Berlin");
int hour = 22; //LocalDateTime.now(id).getHour();
if (hour > 18 || hour < 7) {
} else {
System.out.println("Hour not in estimated working time");
System.out.println("Hour:" + hour);
}
sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00); // Trigger pin as OUTPUT // rechts von Schrank aus
sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN); // Echo pin as INPUT
sensorTriggerPin1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_27); // links von Schrank aus
sensorEchoPin1 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_25, PinPullResistance.PULL_DOWN);
new UltraHue();
UltraHue.hue();
}
public void run() throws InterruptedException {
do {
Thread.sleep(20);
Double Distance = getdistance(sensorEchoPin, sensorTriggerPin);
//int hour =22; //LocalDateTime.now(id).getHour();
// && hour>18 || hour<7
if (Distance < 70 && distance2 < 70) { //rechts vom Schrank
if (right == false) {
right = true;
}
if (right == true && left == true) {
UltraHue.lightson(PHHueSDK.getInstance().getSelectedBridge());
right = false;
left = false;
counter = 0;
}
}
distance2 = Distance;
Thread.sleep(30);
double Distance1 = getdistance(sensorEchoPin1, sensorTriggerPin1);
int hour1 = 22; //LocalDateTime.now(id).getHour();
// && hour>18 || hour<7
if (Distance1 < 70 && distance3 < 70) { //links vom Schrank
if (left == false) {
left = true;
}
if (right == true && left == true) {
UltraHue.lightsoff(PHHueSDK.getInstance().getSelectedBridge());
right = false;
left = false;
counter = 0;
}
}
distance3 = Distance1;
if (left == true || right == true) {
counter = counter + 1;
System.out.println(counter);
if (counter == 70) {
System.out.println("resetting counter");
left = false;
right = false;
counter = 0;
}
} else {
counter = 0;
}
} while (true);
}
public static double getdistance(GpioPinDigitalInput sensorEchoPin3, GpioPinDigitalOutput sensorTriggerPin3) throws InterruptedException {
sensorTriggerPin3.high(); // Make trigger pin HIGH
Thread.sleep((long) 0.01); // Delay for 10 microseconds
sensorTriggerPin3.low(); //Make trigger pin LOW
while (sensorEchoPin3.isLow()) { //Wait until the ECHO pin gets HIGH
}
long startTime = System.nanoTime(); // Store the current time to calculate ECHO pin HIGH time.
while (sensorEchoPin3.isHigh()) { //Wait until the ECHO pin gets LOW
}
long endTime = System.nanoTime(); // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
double distance = (((endTime - startTime) / 1e3) / 2) / 29.1;
return distance;
}
}
答案 0 :(得分:2)
我认为您的代码可以简化很多。例如。为什么你使用distance1和distance3之类的变量,他们的目的对我来说并不清楚。我已将您的代码重写为更简单易读的版本。尝试运行代码,如果失败,请使用try catch块将其包围并检查堆栈跟踪。小心while循环,例如
`while(sensorEchoPin3.isLow()){ //Wait until the ECHO pin gets HIGH
}
while(sensorEchoPin3.isHigh()){ //Wait until the ECHO pin gets LOW
}`
你确定引脚最终会回到低或高状态吗?还尝试不在线程运行方法中创建变量,在循环外创建它们并引用它们。他们记忆力很重。
public void run() throws InterruptedException {
while(true){
sleep(20);
//int hour =22; //LocalDateTime.now(id).getHour();
// && hour>18 || hour<7
if (getdistance(sensorEchoPin, sensorTriggerPin) < 70) { //rechts vom Schrank
right = true;
if (left) {
UltraHue.lightson(PHHueSDK.getInstance().getSelectedBridge());
reset();
}
}
sleep(30);
if (getdistance(sensorEchoPin1, sensorTriggerPin1) < 70) { //links vom Schrank
left = true;
if (right) {
UltraHue.lightsoff(PHHueSDK.getInstance().getSelectedBridge());
reset();
}
}
if (left || right) {
//System.out.println(counter);
if (counter++ >= 70) {
//System.out.println("resetting counter");
reset();
}
}
}
}
private void reset(){
counter = 0;
left = false;
right = false;
}
private static long endTime;
private static long startTime;
public static double getdistance(GpioPinDigitalInput sensorEchoPin3, GpioPinDigitalOutput sensorTriggerPin3) throws InterruptedException{
sensorTriggerPin3.high(); // Make trigger pin HIGH
Thread.sleep((long) 0.01);// Delay for 10 microseconds
sensorTriggerPin3.low(); //Make trigger pin LOW
while(sensorEchoPin3.isLow()){ //Wait until the ECHO pin gets HIGH
}
startTime= System.nanoTime(); // Store the current time to calculate ECHO pin HIGH time.
while(sensorEchoPin3.isHigh()){ //Wait until the ECHO pin gets LOW
}
endTime= System.nanoTime(); // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
return(((endTime-startTime)/1e3)/2) / 29.1;
}