Skip to content

Instantly share code, notes, and snippets.

@simonprickett
Created August 21, 2021 17:56
Show Gist options
  • Save simonprickett/6d072619672db21dccbe3c7917915c97 to your computer and use it in GitHub Desktop.
Save simonprickett/6d072619672db21dccbe3c7917915c97 to your computer and use it in GitHub Desktop.
C# .NET code for Raspberry Pi GPIO Traffic Lights Article
using System;
using System.Device.Gpio;
using System.Threading;
namespace trafficlights
{
class Program
{
static void Main(string[] args)
{
const int redPin = 9;
const int amberPin = 10;
const int greenPin = 11;
GpioController gpio = new GpioController();
gpio.OpenPin(redPin, PinMode.Output);
gpio.OpenPin(amberPin, PinMode.Output);
gpio.OpenPin(greenPin, PinMode.Output);
void AllLightsOff()
{
gpio.Write(redPin, PinValue.Low);
gpio.Write(amberPin, PinValue.Low);
gpio.Write(greenPin, PinValue.Low);
}
Console.CancelKeyPress += delegate {
AllLightsOff();
};
AllLightsOff();
while (true) {
// Red
gpio.Write(redPin, PinValue.High);
Thread.Sleep(3000);
// Red and amber
gpio.Write(amberPin, PinValue.High);
Thread.Sleep(1000);
// Green
gpio.Write(redPin, PinValue.Low);
gpio.Write(amberPin, PinValue.Low);
gpio.Write(greenPin, PinValue.High);
Thread.Sleep(5000);
// Amber
gpio.Write(greenPin, PinValue.Low);
gpio.Write(amberPin, PinValue.High);
Thread.Sleep(2000);
// Amber off
gpio.Write(amberPin, PinValue.Low);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment