Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
tnguy146
Published

SparkFun - Traffic Light with Crossing

This is a circuit that simulates a traffic light with a crosswalk button

BeginnerShowcase (no instructions)392
SparkFun - Traffic Light with Crossing

Things used in this project

Hardware components

SparkFun RedBoard
SparkFun RedBoard
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
SparkFun Push Button
×1
Resistor 330 ohm
Resistor 330 ohm
×4
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Traffic Light Schematic

The Schematic
View by using Fritzing

Code

Code

Arduino
The code
int button = 7;
int buttonState = 0;
int red = 3;
int yellow = 4;
int green = 5;

void setup() {

    pinMode(red, OUTPUT); // red led
    pinMode(yellow, OUTPUT); // yellow led
    pinMode(green, OUTPUT); // green led
    pinMode(button, INPUT); // button
    
}



// Main/Loop Function
void loop(){
 if (digitalRead(button) == HIGH){
        if (digitalRead(button) == HIGH) {
            // if the button pressed, stop traffic
            lightCycle();
        }
 }
    else { // If the button is off, traffic goes on
      digitalWrite(green, HIGH);
      digitalWrite(yellow, LOW);
      digitalWrite(red, LOW);
    }
 }
 

void lightCycle(){
  
  // Turns green off, Yellow on 
  delay(2000);
  digitalWrite(green, LOW);
  digitalWrite(yellow, HIGH);
  digitalWrite(red, LOW);
  delay(3000);


  // Turns yellow off, Red on
  digitalWrite(green, LOW);
  digitalWrite(yellow, LOW);
  digitalWrite(red, HIGH);
  delay(8000);
}

Credits

tnguy146
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.