so I'm working on a program to turn on or off my hue lights as I enter my room. At my roomdoor are 2 HCSr04's mounted to detect the direction of the incoming person. The program works mostly fine but after a short period of time it just freezes and nothing happens. It's running on a Rapsberry Pi 3 with Raspbian.
Here is the code:
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;
}
}
I think your code can be simplified a lot. E.g. why are you using variables like distance1 and distance3, their purpose is not clear to me. I have rewritten your code to a more simple and readable version. Try to run the code and if it fails, surround it with a try catch block and check the stacktrace. Be careful with while loops such as
`while(sensorEchoPin3.isLow()){ //Wait until the ECHO pin gets HIGH
}
while(sensorEchoPin3.isHigh()){ //Wait until the ECHO pin gets LOW
}`
Are you sure the pins will return to low or high state eventually? Also try to not create variables inside the thread run method, create them outside the loop and reference them. They are memory heavy.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With