Go Down

Topic: Traffic light code (beginner level coding) (Read 51128 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.

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

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

OrdnaeL

I am trying to do a Traffic Light code with a button, that, when pressed, the Lights turn Yellow, then Red and the Ped lights invert that.

How should i do this?

My code is this:
Quote
//Traffic Lights with Button

void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, INPUT);
}

// Loop Function
void loop() {
  digitalWrite(13, HIGH);   // green ON.
  delay(5000);              // wait 5sec.
  digitalWrite(13, LOW);    // green OFF.
  digitalWrite(12, HIGH);   // yellow ON.
  delay(2000);              // wait 2sec.
  digitalWrite(12, LOW);    // yellow OFF.
  digitalWrite(11, HIGH);   // red ON.
  delay(5000);              // wait 5sec.
  digitalWrite(11, LOW);    // red OFF.
}
if(pinMode(10==HIGH)){      // If button pressed.
  digitalWrite(12, HIGH);   // yellow ON.
  delay(2000);              // wait 2sec.
  digitalWrite(12, LOW);    // yellow OFF.
  digitalWrite(11, HIGH);   // red ON.
  delay(5000);              // wait 5sec.
  digitalWrite(11, LOW);    // red OFF.
}
But i have a error in the IF and i don't know what to do.

Roksana

 I want to design a density based traffic light system  of two lane. Can any one could help me??

bufo333

So you can replace all of the delay(); statements with a counter system using the millis() statement. Additionally you can save 1 byte of memory on every  int variable  with a value <=255 by using the byte datatype. This may not be important now, but it will be later. You can create as many counter variables as you want for different things.

I.E.
byte var = 0;
unsigned long counter;

void setup()
{
//first thing
counter = millis();
}

void loop()
{
if (millis()-counter >=5000) {
// then do stuff because 5 seconds has passed
stuff
//don't forget to reset the counter because it was activated
counter = millis();
}


//do other stuff because counter is less than 5 seconds


}



BlizzardHawk

hey, this is kind of similar to the code i have written, only i chose for an 8 second delay and a light for the walkers which will start blinking if there's 5 seconds left to cross

here's my code, sorry i don't know how to include a file like that

int red = 7;
int orange = 8;
int green = 9;
int walkerGO = 10;
int walkerSTOP = 11;

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(red, OUTPUT);
  pinMode(orange, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(walkerGO, OUTPUT);
  pinMode(walkerSTOP, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(red, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(walkerGO, HIGH);   // turn the walk light on
  delay(4000);      // wait for 4 seconds
  digitalWrite(walkerGO, LOW);  // turn the walk light off
  delay(1000);      // wait 1 second
  digitalWrite(walkerGO, HIGH);   // turn the walk light on
  delay(1000);      // wait 1 second
  digitalWrite(walkerGO, LOW);  // turn the walk light off
  delay(1000);      // wait 1 second
  digitalWrite(walkerGO, HIGH);   // turn the walk light on
  delay(1000);      // wait 1 second
  digitalWrite(walkerGO, LOW);  // turn the walk light off
  digitalWrite(walkerSTOP, HIGH);
  digitalWrite(red, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(orange, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(8000);               // wait for a second
  digitalWrite(orange, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(green, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(8000);               // wait for a second
  digitalWrite(green, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(walkerSTOP, LOW);
}

Go Up