LED traffic lights (2)

Now, we start a new circuit.

Task:
Build a traffic lights, but trigger the action with a switch!

Before we start, we need some new basics:

It’s a simple question, how can I use a switch in the circuit. The answer is not so simple. But it’s not difficult!

What is the function of the switch? Why we need it? In a “normal” circuit, we use switches to close or interrupt a circuit. At using the Arduino, we can close or interrupt the circuit with the pins. In our situation, we use the switch to interact with our circuit. To tell the Arduino, I want to change something in this moment. The Arduino knows only two states, LOW or HIGH. And so, the task of the switch is to generate in one situation a LOW, and in the other situation a HIGH. It’s not important, what we get if the switch is pressed. We need this knowledge in our sketch only.

There are two possibilities to realise this: Using a pull-up resistor, or using a pull-down resistor. Let’s use and describe a pull-up resistor.

pull-up

The circuit is simple. We have a resistor and the Switch. The resistor is connected to Vcc with one pin.

How does it work?
The switch is open: There is a circuit from Vcc about the resistor to the Arduino pin. We have a current between Vcc and the Arduino pin. So we register a HIGH at the Arduino pin.
The switch is closed: There is an other circuit. Now, we have circuit from Vcc about the resistor to ground. You see, if we don’t use a resistor, we get a short in this situation! By closing the switch, we pull up the ground up to our Arduino, we register a LOW at the Arduino.
Normaly, you should use a resister between 6.8K ohm and 10K ohm. By using a 6.8K ohm resister we have a small current at 0.74 mA.

The circuit:

For this tutorial, wee need the following components:

  • LED red (i.e. 2V, max. 20 mA)
  • LED yellow (i.e. 2V, max. 20 mA)
  • LED green (i.e. 2V, max. 20 mA)
  • 3x resistors (min. 150 Ohm)
  • 1x resistor 6.8K ohm … 10K ohm

The LEDs are a standard components. We use  LEDs with 2V and a maximum current of 20 mA. Please, look tutorial 1.0 or 1.1  how to calculate the resistor. It’s the seem.

1.3-LED

In this tutorial, we use four pins – pin 9 to control the red LED, pin 10 to control the yellow LED, pin 11 to control the green LED and pin 4 to control the switch.

Here is the wiring diagram:

1.3-LED-circuit

We start our Arduino software to create the first sketch.

/*
 LED traffic lights with switch
 playground2014.wordpress.com
*/

int pinred = 9;           // PinPort LED red
int pinyellow = 10;       // PinPort LED yellow
int pingreen = 11;        // PinPort LED green
int pinswitch = 4;        // PinPort Switch
byte stat = 0;            // save the state of the switch action

void setup()  { 
  pinMode(pinred, OUTPUT);  
  pinMode(pinyellow, OUTPUT);  
  pinMode(pingreen, OUTPUT);
  pinMode(pinswitch, INPUT);      

  // to initialize the traffic ligths with red
  digitalWrite(pinred, HIGH);
  digitalWrite(pinyellow, LOW);
  digitalWrite(pingreen, LOW);
} 

void loop()  { 

  // let us start: the traffic ligths is red
  digitalWrite(pinred, HIGH);
  digitalWrite(pinyellow, LOW);
  digitalWrite(pingreen, LOW);

  do  {
   stat = digitalRead(pinswitch);
   delay(100); 
  } while (stat == 1);

  // attention, we will switch
  digitalWrite(pinred, HIGH);
  digitalWrite(pinyellow, HIGH);
  digitalWrite(pingreen, LOW);
  delay(1000); 

  // it's green
  digitalWrite(pinred, LOW);
  digitalWrite(pinyellow, LOW);
  digitalWrite(pingreen, HIGH);
  delay(2000);

  // attention, we will switch
  digitalWrite(pinred, LOW);
  digitalWrite(pinyellow, HIGH);
  digitalWrite(pingreen, LOW);
  delay(1000);

  //it's red
  // we start again (loop)
}

First, we define our variables. We need an additional variable to save the switch state.
void setup() { … }
We define the direction – we use the three LED pins as output pins. The Switch pin is an input pin. And, the first time, we set a default value.

void loop() { … }
It’s the same like tutorial 1.2 except the loop.

do  {
   stat = digitalRead(pinswitch);
   delay(100); 
} while (stat == 1);

It’s a do-while loop. We enter the loop and stay in it, as long as the conditions are complied.
We read the state of the switch pin (we get a HIGH if the switch is open, and a LOW by pressing the switch) and save the result in the variable stat. (in this relationship, HIGH is 1, LOW is 0) A little break is good.
At the end we define the condition: stay in the loop until you press the switch!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s