树莓派闪烁的LED问题 - Python与Java
2022-09-04 01:49:02
我正在尝试使用我在网上找到的一些代码在树莓派上制作LED灯闪光灯(我知道 - 不是最好的,但它是一个教程网站)
当我运行以下python代码时,led灯会闪烁;
import RPi.GPIO as GPIO
import time
pinNum = 4
GPIO.setmode(GPIO.BCM) #numbering scheme that corresponds to breakout board and pin layout
GPIO.setup(pinNum,GPIO.OUT) #replace pinNum with whatever pin you used, this sets up that pin as an output
#set LED to flash forever
while True:
GPIO.output(pinNum,GPIO.HIGH)
time.sleep(0.5)
GPIO.output(pinNum,GPIO.LOW)
time.sleep(0.5)
当我运行以下Java代码时,它应该做同样的事情 - 我到达控制台的只是我添加的print语句 - 没有闪光灯
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
public class ControlGpioExample {
public static void main(String[] args) throws InterruptedException {
final GpioController gpio = GpioFactory.getInstance();
final GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin
.GPIO_04, "MyLED", PinState.LOW);
System.out.println("Started");
try
{
while(true)
{
System.out.println(ledPin==null);
System.out.println("Looping pin now");
ledPin.high();
System.out.println("Set high called");
Thread.sleep(2000);
ledPin.low();
System.out.println("Set low called");
Thread.sleep(2000);
}
}
catch(Exception ex)
{
gpio.shutdown();
ex.printStackTrace();
}
}
}
有谁知道为什么会这样?我认为从逻辑上讲,应该做同样的事情 - 两者都使用相同的GPIO引脚数从pi
感谢您的帮助