Go Down

Topic: Traffic light code (beginner level coding) (Read 11 times) previous topic - next topic

Valalvax


You could also add a photoresistor for detecting whether a car is in front of the traffic lights (a car presumably blocks the light). A force sensor would be better but those are more expensive ;).


IRL they use coils these days, force sensors need replacing too often

ThunderBob

So for the "crosswalk button with double lane traffic light" system what type of logic programming would I be using

more if statements or something else?

The only button I have is something similar to this and I only have one =/

http://www.gravitech.us/mipubusw4qt4.html

Eventually when I'm done I'm going to build a miniature city using LEDs and hotwheels with this code lol

seckela

#17
Feb 25, 2011, 11:43 pm Last Edit: Feb 25, 2011, 11:45 pm by seckela Reason: 1
That button will work just fine. For the Logic you can use a case based system like this:

Code: [Select]
if (cWalkButton == HIGH)
{
delay(1000);
cWalk = 1;
}

if (cWalk == 1)

{
lane1 = 0;
lane2 = 0;
digitalWrite(cWalkLight, HIGH);
delay(500)
digitalWrite(cWalkLight, LOW);
delay(500)
digitalWrite(cWalkLight, HIGH);
delay(500)
digitalWrite(cWalkLight, LOW);
delay(500)
cWalk = 0;
}

else
{
//stoplight code here
}



This code could be really sloppy I just wrote it. I hope its understandable. Essentially, you want to set a case for the button to be on HIGH and that to trigger a set of events, other wise you want your lanes to be working as normal. Also remember you don't want the cross walk code to depend on the button constantly being HIGH because that means you'd have to hold it for the durration and it would just loop. You want 1 press to set the condition up, then once forfilled, it to reset to off.
Damn the Man, Save the Empire

ThunderBob

ok and to define those terms you added I just place

Code: [Select]
int lane1 = 0;
int lane2 = 0;


at the top of the code right?

also wouldn't I have to use another pin as input such as

Code: [Select]
pinMode (4 , INPUT);

for the button and map it?


Valalvax

#19
Feb 26, 2011, 01:59 am Last Edit: Feb 26, 2011, 02:00 am by Valalvax Reason: 1
You don't HAVE to define it as an input (all pins are inputs by default) but I always do

With only one button you'll only have one lane with a crosswalk, all them other saps are screwed...

If you wanted to actually do it with hotwheels etc you'd need some way to stop the cars, cause they aren't going to obey traffic laws

http://www.youtube.com/watch?v=JXRe9M-z8Cs

ThunderBob

HAHA indeed that is true!
Well guys I'm going to come back to this traffic system thing and post a video sometime soon of a miniature city.

Now I'm going to start familiarizing myself with motor control with the arduino.

Meinaart

You could take a look at this topic to see an example on how to use a button to interrupt the logic in the loop function: http://arduino.cc/forum/index.php/topic,53437.0.html

Funnell7

I took a stab at the 'Two Way Traffic Lights with Pedestrian Crossing', I think I've got it working pretty well. I won't post the code just yet, to let you have a go first. Let me know, if you require any guidance or support, I would be more than happy to help... As a clue, I used an 'Interrupt'...

Two sets of traffic lights (left and right) working together, with a pedestrian crossing (in the middle). When the pedestrian button is pushed, a 'wait' led is lit, when the current traffic light finishes its cycle, the pedestrian crossing flashes. Once complete, the other traffic light resumes. Oh... the middle leds of the traffic lights should be amber but I haven't get any amber leds...

http://www.youtube.com/watch?v=IruJJLqOrOQ

Valalvax


seckela

I'm unaware of why there is a double light before the green. Though it might be based off another nations system, Im used to, RED ->Green ->Yellow -> Red
Damn the Man, Save the Empire

Valalvax


I'm unaware of why there is a double light before the green. Though it might be based off another nations system, Im used to, RED ->Green ->Yellow -> Red


Yea, I had to google it, it's the English's way of doing it

Go Up