-
-
Save randPharoah/77fe43035c334b9d245e261a7022e15c to your computer and use it in GitHub Desktop.
Raspberry Pi Traffic Lights example code for Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 TrafficLights { | |
| private static GpioController gpio = null; | |
| private static GpioPinDigitalOutput red = null; | |
| private static GpioPinDigitalOutput yellow = null; | |
| private static GpioPinDigitalOutput green = null; | |
| public static void main(final String[] args) throws InterruptedException { | |
| Runtime.getRuntime().addShutdownHook(new Thread() { | |
| @Override | |
| public void run() { | |
| if (gpio != null) { | |
| red.low(); | |
| yellow.low(); | |
| green.low(); | |
| gpio.shutdown(); | |
| } | |
| } | |
| }); | |
| gpio = GpioFactory.getInstance(); | |
| red = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_13, "Red", PinState.LOW); | |
| yellow = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_12, "Red", PinState.LOW); | |
| green = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_14, "Red", PinState.LOW); | |
| while (true) { | |
| red.high(); | |
| Thread.sleep(3000); | |
| yellow.high(); | |
| Thread.sleep(1000); | |
| red.low(); | |
| yellow.low(); | |
| green.high(); | |
| Thread.sleep(5000); | |
| green.low(); | |
| yellow.high(); | |
| Thread.sleep(2000); | |
| yellow.low(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment