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