Go Down

Topic: Interactive Traffic light - button doesn't work (Read 12351 times) previous topic - next topic

kesztyn_PL

Feb 07, 2012, 07:11 pm Last Edit: Feb 07, 2012, 07:32 pm by kesztyn_PL Reason: 1
Hello,
I have got a problem with project called "Traffic Light".
Part of excersise
Quote
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.


Code: [Select]
// 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"

John_S

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.


Techone

#2
Feb 07, 2012, 11:06 pm Last Edit: Feb 07, 2012, 11:40 pm by Techone Reason: 1
1. Check your resistors values.
 
   At each resistors : 220 to 470
   At each switches : 1 K to 10 K

2. Check polarity of each led's.

3. 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...

Code: [Select]

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

cmiyc

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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Techone

@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.

cmiyc

@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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

runaway_pancake

I think those are 220?, 1% (4-band): red-red-black-black, 2 - 2 - 0 - and 0 0's, 220
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

mmcp42

red red brown would be 220
red red black is 22
there are only 10 types of people
them that understands binary
and them that doesn't

kesztyn_PL

#8
Feb 08, 2012, 10:34 am Last Edit: Feb 08, 2012, 10:41 am by kesztyn_PL Reason: 1
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:
http://www.youtube.com/watch?v=eIkOq9w-HjU



Techone

@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. 

kesztyn_PL

@Techone:

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

Have a nice day!

Techone

#11
Feb 08, 2012, 11:40 pm Last Edit: Feb 09, 2012, 01:05 am by Techone Reason: 1
No problem. You should do a small code to test the leds.

Edit:

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

Code: [Select]

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


TiboJ

#12
Feb 26, 2012, 08:08 pm Last Edit: Feb 26, 2012, 08:22 pm by TiboJ Reason: 1
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 :smiley-eek:

Can someone help me?

Techone

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.

TiboJ

I will send my setup soon in a few minutes.


Code: [Select]
// 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
}




Go Up