1
我試圖用數字風速計與我的覆盆子PI與pi4j一起工作。Pi4j風速計 - 每間隔計數器
我的想法是添加GpioPinListenerDigital
來監視引腳何時變高(意味着1次完整的風速計旋轉),但我無法實現它...我想設置一個間隔來監視中斷,但沒有成功。 ..
這是我的主類
public class Main {
public static void main(String[] args) throws InterruptedException {
Anemometer anemometer;
int rotations = 0;
System.out.println("Start");
while(true){
rotations = Anemometer.countPulse();
System.out.println("Rotations "+ rotations);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
它只需撥打風速計,這是
public class Anemometer {
final static int short_interval = 3;
static int final_counter = 0;
public static int countPulse() {
GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalInput input = gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_DOWN);
input.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
long start = System.currentTimeMillis();
long end = 0L;
int counter = 0;
while (end < short_interval * 1000) {
System.out.println("Inizio while");
if (event.getState().isHigh()) {
System.out.println("Pin state: " + event.getState());
counter++;
}
end = (new Date()).getTime() - start;
}
final_counter = counter;
System.out.println("final counter: "+final_counter);
}
});
gpio.unprovisionPin(input);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return final_counter;
}
}
它看起來就像從來沒有同時進入裏面循環...任何想法?
我不是100%確定究竟是什麼問題,但我認爲intervall設置錯誤。在進入循環之前立即設置'開始'變量。那麼你是否面臨同樣的問題? – kaetzacoatl
是的,我改變了它,但沒有任何反應......我會編輯這些問題,謝謝 – besmart