Devices & Components
Arduino Uno Rev3
LED (generic)
Resistor 1k ohm
Resistor 10k ohm
Slide Switch
Software & Tools
Arduino IDE
Project description
Code
Railroad cross lights
c_cpp
1/* 2 Railroad crossing lights 3by DAVID 4 */ 5// constants won't change. Used here to set a pin number : 6const int ledPinP1 = 9; // the number of the LED pin 7const int ledPinP2 = 6; // the number of the LED pin 8const int ledPinF = 3; // the number of the LED pin 9const int switchPin = 2; // the number of the switch pin 10int switchState = 0; // variable for reading the switch status 11 12void setup() { 13 14 pinMode(ledPinP1, OUTPUT); // initialize the LED pin as an output: 15 pinMode(ledPinP2, OUTPUT); 16 pinMode(ledPinF, OUTPUT); 17 pinMode(switchPin, INPUT); // initialize the pushbutton pin as an input: 18} 19 20void loop() 21{ 22 switchState = digitalRead(switchPin); // read the state of the 23 //pushbuttonvalue: 24 if (switchState == HIGH) //if the switch on: 25 { 26 digitalWrite(ledPinF, LOW); //white LED off 27 digitalWrite(ledPinP1, HIGH); //red1 LED on 28 digitalWrite(ledPinP2, LOW); //red2 LED off 29 delay(400); //wait 400ms 30 digitalWrite(ledPinP1, LOW); //red1 LED off 31 digitalWrite(ledPinP2, HIGH); //red2 LED on 32 delay(400); 33 } 34 if(switchState == LOW) //if the switch off 35 { 36 digitalWrite(ledPinP1, LOW); //red1 LED off 37 digitalWrite(ledPinP2, LOW); //red2 LED off 38 digitalWrite(ledPinF, HIGH); //white LED on 39 delay(500); //wait 500ms 40 digitalWrite(ledPinF, LOW); //white LED off 41 delay(500); 42 } 43} 44 45
Downloadable files
Breadboard
This is breadbord
Breadboard

Schematic
This is schem
Schematic

Breadboard
This is breadbord
Breadboard

Schematic
This is schem
Schematic

Comments
Only logged in users can leave comments