Skip to content

Instantly share code, notes, and snippets.

@randPharoah
Forked from simonprickett/TrafficLights.java
Created August 1, 2021 23:14
Show Gist options
  • Select an option

  • Save randPharoah/77fe43035c334b9d245e261a7022e15c to your computer and use it in GitHub Desktop.

Select an option

Save randPharoah/77fe43035c334b9d245e261a7022e15c to your computer and use it in GitHub Desktop.
Raspberry Pi Traffic Lights example code for Java
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