Created
August 21, 2021 17:56
-
-
Save simonprickett/6d072619672db21dccbe3c7917915c97 to your computer and use it in GitHub Desktop.
C# .NET code for Raspberry Pi GPIO Traffic Lights Article
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
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