Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
eldo85
Published © Apache-2.0

Railroad Crossing Lights

Three LED and a switch equals railroad crossing lights.

BeginnerShowcase (no instructions)1 hour7,888
Railroad Crossing Lights

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
LED (generic)
LED (generic)
×3
Slide Switch
Slide Switch
×1
Resistor 1k ohm
Resistor 1k ohm
×3
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Breadboard

This is breadbord

Schematic

This is schem

Code

Railroad cross lights

C/C++
/*
 Railroad crossing lights
by DAVID
 */
// constants won't change. Used here to set a pin number :
const int ledPinP1 =  9;     // the number of the LED pin
const int ledPinP2 =  6;     // the number of the LED pin
const int ledPinF =  3;     // the number of the LED pin
const int switchPin = 2;    // the number of the switch pin
int switchState = 0;       // variable for reading the switch status   

void setup() {
  
  pinMode(ledPinP1, OUTPUT);  // initialize the LED pin as an output:
  pinMode(ledPinP2, OUTPUT);
  pinMode(ledPinF, OUTPUT);
  pinMode(switchPin, INPUT);  // initialize the pushbutton pin as an input:
}

void loop() 
{
    switchState = digitalRead(switchPin);   // read the state of the 
                                            //pushbuttonvalue:
    if (switchState == HIGH)   //if the switch on:
    {
        digitalWrite(ledPinF, LOW);     //white LED off
        digitalWrite(ledPinP1, HIGH);   //red1 LED on
        digitalWrite(ledPinP2, LOW);    //red2 LED off
        delay(400);                     //wait 400ms
        digitalWrite(ledPinP1, LOW);    //red1 LED off
        digitalWrite(ledPinP2, HIGH);   //red2 LED on
        delay(400);
    }
    if(switchState == LOW)              //if the switch off
    {
        digitalWrite(ledPinP1, LOW);    //red1 LED off
        digitalWrite(ledPinP2, LOW);    //red2 LED off
        digitalWrite(ledPinF, HIGH);    //white LED on
        delay(500);                     //wait 500ms
        digitalWrite(ledPinF, LOW);     //white LED off
        delay(500);
    }
}

Credits

eldo85

eldo85

2 projects • 13 followers

Comments