Traffic Light

The "Eleven" is our Uno-equivalent Arduino-compatible board, but with a number of improvements including prototyping area, a mini-USB connector, LEDs mounted near the edge, and the D13 LED isolated using a FET. [Product page]
Post Reply
Jones512
Posts: 4
Joined: Tue Jul 23, 2013 9:57 am

Traffic Light

Post by Jones512 » Sun Aug 11, 2013 4:19 am

Hi guys new here and new to Arduino

What I'm trying to do is make a traffic light style controller I've followed this tutorial so far
http://www.makeuseof.com/tag/arduino-tr ... /#comments
It's actually for a theatre play scene where the actor is at a seminar and they need to be able to switch this themselves.

But what I want to be able to do is have a single button that when pressed changes the LED in a certain order i.e Green --> Amber --> Red.
--> = a button push
I also want them to stay on until the next button push
I don't want them to cycle through automatically.

So far this is my code:

Code: Select all

//Traffic Light Style Controller
//By Caleb Jones

//Define LED's to Pin number
int red =13;
int amber =12;
int green = 11;
int button = 2; // Switch is on Pin 2
int buttonValue = 0; // Defaults switch to LOW

void setup()
{
  pinMode(button,INPUT);
  pinMode(red,OUTPUT);
  pinMode(amber,OUTPUT);
  pinMode(green,OUTPUT);
}

void changeLights(){
//Green On
digitalWrite(red,LOW);
digitalWrite(amber,LOW);
digitalWrite(green,HIGH);


//Amber On
digitalWrite(red,LOW);
digitalWrite(amber,HIGH);
digitalWrite(green,LOW);


//Red On
digitalWrite(red,HIGH);
digitalWrite(amber,LOW);
digitalWrite(green,LOW);

}

void loop(){
// read the value of the switch
buttonValue = digitalRead(button);
// if the switch is HIGH, ie. pushed down - change the lights!
if (buttonValue == HIGH)
{
changeLights();
delay(15000); // wait for 15 seconds

}
}

Can you tell me if this will work?
I haven't assembled the board yet but I will be doing that tomorrow, once I get this working I will migrate it to a LeoStick

Any help is greatly appreciated

Cheers
Caleb.

cef
Freetronics Staff
Freetronics Staff
Posts: 106
Joined: Wed Nov 09, 2011 12:53 am

Re: Traffic Light

Post by cef » Sun Aug 11, 2013 5:37 am

I'm no master of Arduino code, but the first thing that I notice is that in each sequences you've set up, there is no delay in the sequences when doing Green/Amber/Red and Red/Amber/Green.

In essence (for Red/Amber/Green), you'll go in red, come out green, and you will not see the amber. You probably want to put in a delay between each.

Note: The delays in these vary from country to country (and in most cases, based on the size of the intersection), so don't be too fussed if they seem slightly too short. Since this is for a theatre scene, use your own judgement. That said, usually the delay between green and amber, and amber and green is the same value. If you're going the other way, then the delay doesn't really apply unless you want to show Red, Red & Amber, then Green.

Jones512
Posts: 4
Joined: Tue Jul 23, 2013 9:57 am

Re: Traffic Light

Post by Jones512 » Sun Aug 11, 2013 7:37 am

Hi Cef

Thanks for your response, I took the delays out on purpose As i don't want it to cycle through itself, I shouldn't really be calling it a traffic light, I just need to switch between 3 LEDs with one button.

So press the button red comes on
Press again red goes off amber comes on
press again amber goes off green comes on
and lastly press again and all LEDs are off
Press again and start that sequence again

I've changed code now here is the new one:

Code: Select all

//LED Light Changer 

int buttonPin = 2;
int yellowLed = 3;
int redLed = 4;
int greenLed = 5;
int currentLed = 2; 


void setup() {
  pinMode(buttonPin, INPUT);      // set the switch pin to be an input
  pinMode(yellowLed, OUTPUT);   // set the yellow LED pin to be an output
  pinMode(redLed, OUTPUT);   // set the red LED pin to be an output
  pinMode(greenLed, OUTPUT); //set green pin as output
}

void loop()
{
  if (digitalRead(buttonPin) == LOW) 
  {
    switch (currentLed)
    {
    case 2:  //Red LED ON      
      digitalWrite(redLed, HIGH); 
      digitalWrite(yellowLed, LOW); 
      digitalWrite(greenLed, LOW);
      currentLed++;
      break;    
      
    case 3:  //Yellow LED ON 
      digitalWrite(redLed, LOW); 
      digitalWrite(yellowLed, HIGH); 
      digitalWrite(greenLed, HIGH);    
      currentLed++;
      break;    


    case 4: //Green LED ON                     
     digitalWrite(redLed, LOW); 
      digitalWrite(yellowLed, LOW); 
      digitalWrite(greenLed, HIGH);;     
      currentLed++;
      break;

    default:                           
      digitalWrite(yellowLed, LOW);    // turn off the yellow LED   
      digitalWrite(redLed, LOW);    // turn off the red LED   
      digitalWrite(greenLed, LOW);    // turn off the green LED 
      currentLed = 2;                  //reset the counter      
    } 
  }
}


Thanks Once again

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Traffic Light

Post by angusgr » Mon Aug 12, 2013 12:53 am

Hi Caleb,

The code looks pretty good. The only thing to remember is that the Arduino runs very fast, faster than you can see. So with the code example you've posted, what you'll actually see is a single colour on until the button is pressed, then a blur of red, green and amber all at once for as long as the button is held down, and then it will settle on a random colour as soon as the button is released.

The easy way around this is just to add a delay() at the bottom of the loop to give the actor time to release the button before it checks the input again. delay(2000) or delay(5000) or something to be safe.

The other catch with buttons is the need to "debounce" the inputs, basically a button press can look like dozens of "on/off" events all in the space of a very short period of time (milliseconds), as the button actually makes contact. The delay(2000) will also work around problems with debouncing, with the caveat that you can't switch lights more than that frequently. If you need them to be able to switch more often than once a second, say, then you may need to do some research into debouncing techniques. Let us know if you need any help getting that going.

Finally, the best way to get this stuff working really is to just hook it up and run it! It's a lot quicker to find the problems when they're right there happening in front of you. :)

- Angus

Jones512
Posts: 4
Joined: Tue Jul 23, 2013 9:57 am

Re: Traffic Light

Post by Jones512 » Mon Aug 12, 2013 9:01 am

Hi Angus Thanks for your reply

Thanks for your reply, regarding my code in the above post, how would I go about adding a Debounce sequence to that?

I had a quick look on the net and it looked a bit confusing haha


Cheers

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Traffic Light

Post by angusgr » Tue Aug 13, 2013 12:28 am

Hi Jones,

Well, the easiest option really is to just to leave in something like delay(2000). That limits the rate of traffic light switching to every 2 seconds (assuming the actor holds down the button that long), but also prevents bounces from glitching it and creating multiple light changes.

Otherwise, probably one of the simplest Debouncing solutions is the one in the one on Arduino Playground:
http://playground.arduino.cc/Code/Debounce

That way you can eliminate the delay, and the actor will be able to tap the button quickly to cycle the lights rapidly. After integrating the library, you'll want to insert something like this into your main loop:

Code: Select all

void loop()
{
  debouncer.update();
  if(debouncer.read() == LOW) // Button is down
  {
    // Update the currently lit traffic light
  }
  while(debouncer.read() == LOW) // Wait until the button has been released
  {
    debouncer.update();
  }
}
If you want to go that way then feel free to post the code if you try integrating it but it doesn't work.


- Angus

Jones512
Posts: 4
Joined: Tue Jul 23, 2013 9:57 am

Re: Traffic Light

Post by Jones512 » Wed Aug 14, 2013 10:38 am

Hi Angus and Cef

Thanks so much for your replies I'm sure you've taken time out of your day to help me out and its very much appreciated. This was my first ever project and certainly not my last!
I've got a working prototype which I am now going to port over to a LeoStick.

For those who wish to replicate what I've done here is the code:

Code: Select all

//LED Light Changer 
//Code By: Caleb Jones
//Version 
//14/08/2013


int buttonPin = 2;
int yellowLed = 3;
int redLed = 4;
int greenLed = 5;
int currentLed = 2; 


void setup() {
  pinMode(buttonPin, INPUT);      // set the switch pin to be an input
  pinMode(yellowLed, OUTPUT);   // set the yellow LED pin to be an output
  pinMode(redLed, OUTPUT);   // set the red LED pin to be an output
  pinMode(greenLed, OUTPUT); //set green pin as output
}

void loop()
{
  if (digitalRead(buttonPin) == HIGH) 
  {
    switch (currentLed)
    {
    case 2:  //Red LED ON      
      digitalWrite(redLed, HIGH); 
      digitalWrite(yellowLed, LOW); 
      digitalWrite(greenLed, LOW);
      currentLed++;
      delay(200);
      break;    
      
    case 3:  //Yellow LED ON 
      digitalWrite(redLed, LOW); 
      digitalWrite(yellowLed, HIGH); 
      digitalWrite(greenLed, LOW);    
      currentLed++;
      delay(200);
      break;    


    case 4: //Green LED ON                     
     digitalWrite(redLed, LOW); 
      digitalWrite(yellowLed, LOW); 
      digitalWrite(greenLed, HIGH);;     
      currentLed++;
      delay(200);
      break;

    default:                           
      digitalWrite(yellowLed, LOW);    // turn off the yellow LED   
      digitalWrite(redLed, LOW);    // turn off the red LED   
      digitalWrite(greenLed, LOW);   // turn off the green LED 
      delay(200);
      currentLed = 2;                  //reset the counter      
    } 
  }
}


Thanks once again to Angus and Cef

Caleb

Post Reply