Saturday, June 28, 2014


This is a beginners project for the Arduino. I am using the Arduino Uno but should work with little or no modification on other arduino models. 

Setting up the hardware is very easy. This project is more of a training tool for learning how to program a timer list while listening for a button press.

Parts you will need.
- Arduino
- Solder-less Breadboard
- 10 LEDs
--- 2 Red
--- 2 Yellow
--- 2 Green
--- 2 Small Red
--- 2 Small Green
- 2 1Kohm Resistors
- 2 Momentary Switches (normally open)
- Jumper Wires

Step 1: The Circuit


Step 2: Setup LEDs







Setup the lights on the breadboard like a traffic light. Red on top, then yellow, and then green. The small red and green are the pedestrian crossing signals. Again red on top and green just below.

In my images I am using the right side positive run of the breadboard as negative because its easier to put in the LED.

Put the cathode (-) into the negative run of the breadboard and the anode (+) into a hole next to it.

Step 3: Buttons





Put the buttons in so you have at least 2 pin holes space to add the wires and resistors. Place the 1kohm resistor between one side of the button and the negative run on the breadboard.

Step 4: Wire up LEDs








There are a lot of wire in this project so try to keep it neat so you know where everything is going.

Start by linking the two power rails of the breadboard and giving the breadboard power from the arduino.

An easy way to wire the lights is to just start from the top and work your way down.

Wire guide:
LED / Pin

Side A
Red = 4
Yellow = 3
Green = 2
Small Red = 7
Small Green = 6

Side B
Red = 10
Yellow = 9
Green = 8
Small Red = 13
Small Green = 12

Step 5: Wire up Buttons




A wire on the same side of the resistor will go to pins 5 (Side A) and 11 (Side B)
On the opposite side a wire will go to power (+3.5v)

Step 6: Make the program


// 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;
  }
}



Step 7: Upload and Run



Connect your arduino to your computer and upload the program. After it has uploaded you will see it cycle through the lights.
When you press a button it will wait for the next pass for that side to be green before turning the pedestrian light on. It will then give a warning flash before the light turns yellow

Leave a comment tell me about your success.





What is Arduino?

Arduino is a tool for making computers that can sense and control more of the physical world than your desktop computer. It's an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board.
Arduino can be used to develop interactive objects, taking inputs from a variety of switches or sensors, and controlling a variety of lights, motors, and other physical outputs. Arduino projects can be stand-alone, or they can communicate with software running on your computer (e.g. Flash, Processing, MaxMSP.) The boards can be assembled by hand or purchased preassembled; the open-source IDE can be downloaded for free.
The Arduino programming language is an implementation of Wiring, a similar physical computing platform, which is based on the Processing multimedia programming environment.

Why Arduino?

There are many other microcontrollers and microcontroller platforms available for physical computing. Parallax Basic Stamp, Netmedia's BX-24, Phidgets, MIT's Handyboard, and many others offer similar functionality. All of these tools take the messy details of microcontroller programming and wrap it up in an easy-to-use package. Arduino also simplifies the process of working with microcontrollers, but it offers some advantage for teachers, students, and interested amateurs over other systems:
  • Inexpensive - Arduino boards are relatively inexpensive compared to other microcontroller platforms. The least expensive version of the Arduino module can be assembled by hand, and even the pre-assembled Arduino modules cost less than $50
  • Cross-platform - The Arduino software runs on Windows, Macintosh OSX, and Linux operating systems. Most microcontroller systems are limited to Windows.
  • Simple, clear programming environment - The Arduino programming environment is easy-to-use for beginners, yet flexible enough for advanced users to take advantage of as well. For teachers, it's conveniently based on the Processing programming environment, so students learning to program in that environment will be familiar with the look and feel of Arduino
  • Open source and extensible software- The Arduino software is published as open source tools, available for extension by experienced programmers. The language can be expanded through C++ libraries, and people wanting to understand the technical details can make the leap from Arduino to the AVR C programming language on which it's based. Similarly, you can add AVR-C code directly into your Arduino programs if you want to.
  • Open source and extensible hardware - The Arduino is based on Atmel's ATMEGA8 and ATMEGA168 microcontrollers. The plans for the modules are published under a Creative Commons license, so experienced circuit designers can make their own version of the module, extending it and improving it. Even relatively inexperienced users can build the breadboard version of the module in order to understand how it works and save money.