Interactive Traffic light - button doesn't work

Hello,
I have got a problem with project called "Traffic Light".
Part of excersise

This time you are going to extend the previous project to include a set of pedestrian lights and a
pedestrian push button to request to cross the road. The Arduino will react when the button is pressed
by changing the state of the lights to make the cars stop and allow the pedestrian to cross safely.

// Project 4 - Interactive Traffic Lights
int carRed = 12; // assign the car lights
int carYellow = 11;
int carGreen = 10;
int pedRed = 9; // assign the pedestrian lights
int pedGreen = 8;
int button = 2; // button pin
int crossTime = 5000; // time alloyoud to cross
unsigned long changeTime; // time since button pressed
void setup() {
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT); // button on pin 2
// turn on the green light
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
}
void loop() {
int state = digitalRead(button);
/* check if button is pressed and it is over 5 seconds since last button press */
if (state == HIGH && (millis() - changeTime) > 5000) {
// Call the function to change the lights
changeLights();
}
}
void changeLights() {
digitalWrite(carGreen, LOW); // green off
digitalWrite(carYellow, HIGH); // yellow on
delay(2000); // wait 2 seconds
digitalWrite(carYellow, LOW); // yellow off
digitalWrite(carRed, HIGH); // red on
delay(1000); // wait 1 second till its safe
digitalWrite(pedRed, LOW); // ped red off
digitalWrite(pedGreen, HIGH); // ped green on
delay(crossTime); // wait for preset time period
// flash the ped green
for (int x=0; x<10; x++) {
digitalWrite(pedGreen, HIGH);
delay(250);
digitalWrite(pedGreen, LOW);
delay(250);
}
// turn ped red on
digitalWrite(pedRed, HIGH);
delay(500);
digitalWrite(carYellow, HIGH); // yellow on
digitalWrite(carRed, LOW); // red off
delay(1000);
digitalWrite(carGreen, HIGH);
digitalWrite(carYellow, LOW); // yellow off
// record the time since last change of lights
changeTime = millis();
// then return to the main program loop
}

Photos:



What's wrong?

  • Pushbutton does not work
  • There is only green "for car's" which does not change.
  • I have only red and green led's so i'm useing secound red in place of yellow;-)

What should i do to make it working? where is my error?

PS: My english is not well.
PS: Text and images of exercise are part of Michael McRoberts book "Beginning Arduino"

Hmm, another school assignment?

Part of your problem might be using delay(). When the program is in a delay(), it isn't looking for a button press. The button might be working just fine, but the program isn't getting it.

  1. Check your resistors values.

At each resistors : 220 to 470
At each switches : 1 K to 10 K

  1. Check polarity of each led's.

  2. Check the switch connection.

I look at the resistors of the leds...look fine...220 ohms

I look at the push switch...bad way ! move it to 90 degree CW or CCW. The way you did it, it is shorted.
In my opinion...

code : Change...

if (state == HIGH && (millis() - changeTime) > 5000)
 to 

if ( ( state == HIGH ) && ( (millis() - changeTime) > 5000 ) )  <-- I forgot an extra )

Make the change...and see what happen... It may work or not...

edit : I check with the Arduino simulator...it work fine... The simulator is available here : http://arduino.com.au/Simulator-for-Arduino.html

You haven't used a pull-up (or pull-down) resistor with the button. That'll leave it floating. Which may or may not be what you are seeing. I'm not sure what what "doesn't work" means.

It is strange to see you using millis() and delay() in the same sketch. As already stated, every millisecond you are "delayed", that is a millisecond the arduino can't check the button. So for over 3 seconds you are blind to the button's operation.

Finally, do yourself a favor and use the IDE's Tools -> Auto Format to make your code readable.

@James C4S

I look at the first picture, where the switch is, the resistor ( ? value ) connect in a pull-down way. The Arduino wire do go to the switch and resistor. If you look closely at the push button, I notice the button is wire impropely.

I hope you agree with me.

@Techone, Yup are you right. I missed the resistor there.

I am now confused about the resistors. If they are in fact 55ohm, that is way too low. That'll draw almost 50mA on the LEDs, killing the I/O pins. However, the resistor shown in the close up looks like 22ohm to me, which is even worse.

Not only that, but when the button is pushed that'll drop 5V over 55ohm = 90mA. That's 450mW! So a minimum of a 1/2W resistor must be used.

I think those are 220?, 1% (4-band): red-red-black-black, 2 - 2 - 0 - and 0 0's, 220

red red brown would be 220
red red black is 22

So:

@runaway_pancake: yes it is 220 Ohm, my mistake on drawing.

@James C4S and Techone: I change everthink i understood. What i have now:

  • 220+55= 275 Ohm per LED (i supose i don't know how to calculate required resistor. google translator doesn't know what is pull-down resistor - am i useing good resostor or not?)
  • there is improve the operations: whe i push the button the cars light change. But light for pedestrian still doesn't work
  • when i put off the resistor from button it doesn't work, but now i use 2.2k resistor and it seems to be good.
  • At the moment it looks that:
    Traffic Light Arduino problem.MP4 - YouTube

@kesztyn_PL

Let see... The push button is working... <--- Match the code

The traffic led's... work fine.

The pedestrians led's... something are wrong. Can you connect them just like the first picture ? The way is was before, look fine. But I don't about the Led's polarity... If the code for the pedestrians leds are pin 9 and pin 8., than it should work fine. The code inside the Arduino should be the same code you posted.

  1. Double check the resistors connections.
  2. Check Led polarity.
  3. The value for your led should be 220. <-- That is OK.

I will post a test code for you to test the leds only.

@Techone:

There were a mistake in... polarity...
Thank You, for Your time Techone :slight_smile:

Have a nice day!

No problem. You should do a small code to test the leds.

Edit:

Here a test code to check the leds of your project.

const byte ledpins[5]={12,11,10,9,8};

void setup()
{
  for (int i=0; i<5; i++)
  {
    pinMode(ledpins[i], OUTPUT);
  }
  for (int i=0; i<5; i++)
  {
    digitalWrite(ledpins[i], LOW);
  } 
}

void loop()
{
  for (int i=0; i<5; i++)
  {
    digitalWrite(ledpins[i], HIGH);
    delay(1000);
    digitalWrite(ledpins,[i], LOW);
  }
}

Hey all,

I have exact the same problem with my pedestrian lights not working.

I have run the led test code of Techone, and then only the pedestrian led on pin 9 doesn't work.

-I've changed the leds, so the problem is not the led
-I've reconnected everything
-I've tested the resistors(150 ohm) and they work fine
-The wires are the right way connected
-The polarity of the leds are right connected

It's very strange that it isn't working :astonished:

Can someone help me?

Well, can you show us : Schematic, circuit setup and code.

My test code is simple. It is just a Blink, but using multiple leds. 150 ohms is a bit high. Use 330 ohms or 470 ohms. Check the led polarity : cathode and anode. Check where the ground wire is, and the digital output pins are wired. My code is design to check the leds and connections... Checking the hardware basicaly.

I will send my setup soon in a few minutes.

// Project 4 - Interactive Traffic Lights
int carRed = 12; // assign the car lights
int carYellow = 11;
int carGreen = 10;
int pedRed = 9; // assign the pedestrian lights
int pedGreen = 8;
int button = 2; // button pin
int crossTime = 5000; // time allowed to cross
unsigned long changeTime; // time since button pressed
void setup() {
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT); // button on pin 2
// turn on the green light
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
}
void loop() {
int state = digitalRead(button);
/* check if button is pressed and it is
over 5 seconds since last button press */
if (state == HIGH && (millis() - changeTime) > 5000) {
// Call the function to change the lights
changeLights();
}
}
void changeLights() {
digitalWrite(carGreen, LOW); // green off
digitalWrite(carYellow, HIGH); // yellow on
delay(2000); // wait 2 seconds
digitalWrite(carYellow, LOW); // yellow off
digitalWrite(carRed, HIGH); // red on
delay(1000); // wait 1 second till its safe
digitalWrite(pedRed, LOW); // ped red off
digitalWrite(pedGreen, HIGH); // ped green on
delay(crossTime); // wait for preset time period
// flash the ped green
for (int x=0; x<10; x++) {
digitalWrite(pedGreen, HIGH);
delay(250);
digitalWrite(pedGreen, LOW);
delay(250);
}
// turn ped red on
digitalWrite(pedRed, HIGH);
delay(500);
digitalWrite(carYellow, HIGH); // yellow on
digitalWrite(carRed, LOW); // red off
delay(1000);
digitalWrite(carGreen, HIGH);
digitalWrite(carYellow, LOW); // yellow off
// record the time since last change of lights
changeTime = millis();
// then return to the main program loop
}

So, you did run my test code to check the leds ?

If one led - pin 9 not working, did you try to exchange with a led - pin that work . example : pin 12 led with pin 9 led. re-run the code. If pin 9 is still not working... well in that case, the ATMega328P chip maybe partially damage...

I will try this now.

Here is the setup:


Just tested it, and it still not works...

  1. Thank you for a picture of your setup. Much better.

  2. If you did my code and pin 9 is still not working... Hum...

  3. I did look at the breadboard you have... I notice a big space. The gnd connection of the led connected to pin 9, is in my opinion is "open" The negative / gnd rail is not connected completely.

I mean the "power" rail is cut in half.

Look at my picture here... see the red wire connected to the "blue" power rail ? In some breadboard, the rails are not connected completely. Just place a jumper wire like my picture and re-run my test code and see what happen... I hope my instint is right.

That solved the problem!!
Thank you very much!!! XD