Pages: 1 [2]   Go Down
Author Topic: Traffic light code (beginner level coding)  (Read 9579 times)
0 Members and 1 Guest are viewing this topic.
Georgia, US
Offline Offline
Sr. Member
****
Karma: 5
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink.

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

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Portland, OR
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That button will work just fine. For the Logic you can use a case based system like this:

Code:
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.
« Last Edit: February 25, 2011, 05:45:29 pm by seckela » Logged

Damn the Man, Save the Empire

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok and to define those terms you added I just place

Code:
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:
pinMode (4 , INPUT);

for the button and map it?

Logged

Georgia, US
Offline Offline
Sr. Member
****
Karma: 5
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: February 25, 2011, 08:00:57 pm by Valalvax » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Netherlands
Offline Offline
Full Member
***
Karma: 0
Posts: 132
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

England - UK
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Georgia, US
Offline Offline
Sr. Member
****
Karma: 5
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You English people have weird lights
Logged

Portland, OR
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Damn the Man, Save the Empire

Georgia, US
Offline Offline
Sr. Member
****
Karma: 5
Posts: 371
Arduino makes my head hurt :(
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: 1 [2]   Go Up
Jump to: