3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Traffic Lights [Beginner Arduino Project]

Step 6Make the program

Make the program
You can download the attached file or copy and paste this code to your arduino software.


// Traffic Lights
// By Ben Hovinga http://ben.hovinga.me/

// Lights {GREEN, YELLOW, RED, PEDGREEN, PEDRED}
int light [2][5] = {{2,3,4,6,7},{8,9,10,12,13}};

// Buttons {light[0],light[1]}
int btn [] = {5,11};

// Timers
int tgreen = 30000; // 30s
int tyellow = 3000; // 3s
int tred = 3000; // 3s
int tpedwarn = 5000; // 5s (must be less than tgreen)
int tpedwarnint = 500; // 0.5s (must be less than tpedwarn)

// Other Vals. Don't Change
int btnval = 0;
boolean pedwait [] = {false,false};
boolean pedactive [] = {false,false};
int direct = 0;
int stp = 1;
boolean stpset = false;
unsigned long now = 0;
unsigned long changeat = -1;
unsigned long changeatped = -1;
boolean pedredon = true;

// SETUP
void setup() {
  // Assign all Pins
  for (int x = 0; x < 2; x++){
    for (int y = 0; y < 5; y++){
      pinMode(light[x][y], OUTPUT);
    }
    // Start everything stopped (red)
    digitalWrite(light[x][2], HIGH);
    digitalWrite(light[x][4], HIGH);
  }
 
  // Assign buttons
  for (int x = 0; x < 2; x++){
    pinMode(btn[x], INPUT);
  }
}

// RUN
void loop() {
  // Update Time
  now = millis();
 
  // Check if button pressed
  for (int x = 0; x < 2; x ++){
    btnval = digitalRead(btn[x]);
    if (btnval == HIGH){
      pedwait[x] = true; // We have someone waiting to cross
    }
  }
 
  // Get into it
  switch (stp){
    // Green
    case 1:
      // Setup
      if (stpset == false){
        changeat = now + tgreen - tpedwarn; // Set timer
        // Turn on Ped
        if (pedwait[direct] == true){
          // Lights change for ped
          digitalWrite(light[direct][3], HIGH);
          digitalWrite(light[direct][4], LOW);
          // Set some vars
          pedwait[direct] = false;
          pedactive[direct] = true;
          pedredon = false;
        }
        // Turn on Green
        digitalWrite(light[direct][0], HIGH);
        // Turn off Red
        digitalWrite(light[direct][2], LOW);
       
        stpset = true; // We are setup
      }
      // Run
      else{
        if (now > changeat){ // Times up
          if (pedactive[direct] == true){
            // Turn off Ped
            digitalWrite(light[direct][3], LOW);
            digitalWrite(light[direct][4], HIGH);
            pedredon = true;
          }
          // Next step
          stp++;
          stpset = false;
        }
      }
    break;
    // Warn ped (if possible)
    case 2:
      // Setup
      if (stpset == false){
        changeat = now + tpedwarn;
        changeatped = now + tpedwarnint;
        stpset = true;
      }
      // Run
      else{
        // Flash Ped Red
        if (pedactive[direct] == true){
          if (pedredon == true && changeatped < now){
            digitalWrite(light[direct][4], LOW);
            pedredon = false;
            changeatped = now + tpedwarnint;
          }
          if (pedredon == false && changeatped < now){
            digitalWrite(light[direct][4], HIGH);
            pedredon = true;
            changeatped = now + tpedwarnint;
          }
        }
        if (now > changeat){ // Times up
          // Turn off
          digitalWrite(light[direct][0], LOW);
          digitalWrite(light[direct][4], HIGH);
          pedredon = true;
          pedactive[direct] = false;
          // Next step
          stp++;
          stpset = false;
        }
      }
    break;
    // Yellow
    case 3:
      // Setup
      if (stpset == false){
        changeat = now + tyellow;
        digitalWrite(light[direct][1], HIGH);
        stpset = true;
      }
      // Run
      else{
        if (now > changeat){ // Times up
          // Turn off
          digitalWrite(light[direct][1], LOW);
          // Next step
          stp++;
          stpset = false;
         
        }
      }
    break;
    // Red
    case 4:
      // Setup
      if (stpset == false){
        changeat = now + tred;
        digitalWrite(light[direct][2], HIGH);
        stpset = true;
      }
      // Run
      else{
        if (now > changeat){ // Times up
          // Start over
          stp = 1;
          stpset = false;
         
          // Change Direction
          if (direct == 1){
            direct = 0;
          }
          else {
            direct = 1;
          }
        }
      }
    break;
  }
}

« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
3
Followers
4
Author:agnivohneb