Go Down

Topic: Programming traffic light, want multiple loops at random (Read 2458 times) previous topic - next topic

rocketman247

How do I do this? I have a 5 signal traffic light I am building a relay board for. I wrote one sketch to run the lights in sequence, but thought it would be cool to rig it to have different loops pulled at random. I am still super new at this though, and have no clue of the higher functions. Thanks!!

Runaway Pancake

"different loops pulled at random"
To run different sequences?

each sequence would run from a function
which function and how many iterations would be based on the results of a random number

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

rocketman247

Okay, here's my code:

Code: [Select]
// This is a program designed to run a five signal traffic light.
//Designed by JohnnyRocket

int red = 2;
int yellow = 3;
int green = 4;
int yel_arrow = 5;
int grn_arrow = 6;
int swap = 4000;
int time = 7000;
int redtime = 7000;
int crosstime = 10;
int flash = 1000;


void setup(){
  red = OUTPUT;
  yellow = OUTPUT;
  red = OUTPUT;
  yel_arrow = OUTPUT;
  grn_arrow = OUTPUT;
}

void loop(){
  digitalWrite(green, HIGH);
  digitalWrite(grn_arrow, HIGH);     //now both lights are on
  delay(crosstime);                  //make it look authentic
  digitalWrite(red, LOW);            // turn red off
  delay(time);                       //Wait
  digitalWrite(yel_arrow, HIGH);     //Turn green arrow to yellow
  delay(crosstime);
  digitalWrite(grn_arrow, LOW);
  delay(time);
  digitalWrite(yel_arrow, LOW);     //Begin yellow arrow flash
  delay(flash);
  digitalWrite(yel_arrow, HIGH);    //Once
  delay(flash);
  digitalWrite(yel_arrow, LOW);
  delay(flash);
  digitalWrite(yel_arrow, HIGH);    //Twice
  delay(flash);
  digitalWrite(yel_arrow, LOW);
  delay(flash);
  digitalWrite(yel_arrow, HIGH);    //Third
  delay(flash);
  digitalWrite(yel_arrow, LOW);
  delay(flash);
  digitalWrite(yel_arrow, HIGH);    //Fourth
  delay(flash);
  digitalWrite(yel_arrow, LOW);
  delay(flash);
  digitalWrite(yel_arrow, HIGH);    //Fifth
  delay(flash);
  digitalWrite(yel_arrow, LOW);
  delay(flash);
  digitalWrite(yel_arrow, HIGH);    //Sixth
  delay(flash);
  digitalWrite(yel_arrow, LOW);     //And off
  delay(time);
  digitalWrite(yellow, HIGH);       //Turn green main to yellow
  delay(crosstime);
  digitalWrite(green, LOW);
  delay(swap);
  digitalWrite(red, HIGH);         //Turn yellow to red
  delay(crosstime);
  digitalWrite(yellow, LOW);
  delay(redtime);
}
 


I want to run multiple sequences so that it may start out with a green arrow, then go red, then go green light, or something else alltogether....

UKHeliBob

Does your sketch do what you want or expect as it is currently written ?

What does the setup function do ??

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

davenunez

#4
Jan 02, 2013, 09:14 am Last Edit: Jan 02, 2013, 10:01 am by davenunez Reason: 1
A more flexible way to encode your sequence would be as an array of bytes. Each byte means something, e.g.:

0 = green light
1 = red light
2 = delay(100)

So you can store a sequence with something like:

byte* sequence = {0,2,1,2};

that means "green light, pause, red light, pause".

You then have a playback function that looks something like:

Code: [Select]

void playback(byte* sequence, int lengthOfSequence)
{
for (int i = 0; i < lengthOfSequence; i++)
{
switch(sequence[i])
{
case 0:
digitalWrite(green, HIGH);
break;
case 1:
digitalWrite(red, HIGH);
break;
case 2:
delay(100);
break;
}
}
}



So now you can have whatever sequence you want, and they are simple to edit. If you want a random seuqence, you can easily generate one with  something like:

Code: [Select]

void randSequence(byte* emptySequence, int lengthOfEmptySequence)
{
for (int i = 0; i < lengthOfEmptySequence; i++)
{
emptySequence[i] = rand()%3;  // we use 3 because there are 3 possible states in our system - red, green, pause
}
}


And you can easily define many different sequences and switch between them. You can even save sequences to an SD card, and then read them off there as you need.

The magic here is that we have separated our CODE from our DATA; notice that in your original code snippet, the sequence (which is data) is baked into the code with hardwired instructions (in your loop() function). This is not a great idea, because it means if you want to change  something small inyour data, you need to change your code, which in turn menas you can add bugs, you need to recompile, etc etc. Separation of code and data is always a good idea  :)

PaulS

Code: [Select]
 red = OUTPUT;
 yellow = OUTPUT;
 red = OUTPUT;
 yel_arrow = OUTPUT;
 grn_arrow = OUTPUT;

Why? This is NOT setting the mode of the pins. Pins are, by default, INPUT. Writing HIGH to an INPUT pin turns the pullup resistor on. Writing LOW turns it off. I can't see why you want to do that in loop().

And, of course, since you've reassigned all the pins to the same pin, the rest of your code is crap.

GoForSmoke

#6
Jan 02, 2013, 12:05 pm Last Edit: Jan 02, 2013, 12:21 pm by GoForSmoke Reason: 1
A state machine would be perfect for that!


On second thought, you don't even need that. You have 5 lights you want to turn on and off in random sequence. So you only need to track which light to turn on, delay, turn off, each time through loop() and code to change which light to light (ie pin to set HIGH, delay, set LOW).

The code itself should be very, very small.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

johncc



byte* sequence = {0,2,1,2};
that means "green light, pause, red light, pause".



Since he has "Arrow" lights, I would think he would want cases where he could have more than one on at the time, so maybe

byte sequence[] = { 0b00001, 0, 0b00010, 0, 0b00011, 0};
that means "green light, pause, red light, pause, green and red light, pause...".

Cheers,
John


rocketman247


Does your sketch do what you want or expect as it is currently written ?

What does the setup function do ??




I am not quite sure.... The code for my smaller walk/don't walk light worked in this manner. The code compiles fine, but this is the way the book said to do it. Maybe my book sucks? I learn by practice. So, I'm having to learn as I go. I haven't gotten to arrays yet; I've just made it past digitalWrite, integer setups and delay :-P

JimboZA

As a civil engineer who started his career in traffic engineering, the idea of a traffic light that changes its phasing randomly, scares the sh!t out of me.....
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/ (not always alive tho)
Don't read too much into my being a Faraday Member: that just means I post a lot, not that I know much.

No PMs for help please

Grumpy_Mike

Quote
The code compiles fine

All that means is that you have not made an error in syntax. It doesn't mean the code will actually do anything.

Quote
but this is the way the book said to do it.

Sorry I do not believe you. You are misunderstanding what it says.



UKHeliBob



Does your sketch do what you want or expect as it is currently written ?

What does the setup function do ??




I am not quite sure.... The code for my smaller walk/don't walk light worked in this manner. The code compiles fine, but this is the way the book said to do it. Maybe my book sucks? I learn by practice. So, I'm having to learn as I go. I haven't gotten to arrays yet; I've just made it past digitalWrite, integer setups and delay :-P


I can write code that compiles OK but that does not mean that it does what I want, or does anything for that matter.

I would be interested to see the code for your walk/don't walk program.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

Quote
I would be interested to see the code for your walk/don't walk program.

You left out "/run like hell".

johncc


...
So, I'm having to learn as I go. I haven't gotten to arrays yet; I've just made it past digitalWrite, integer setups and delay :-P


Don't count yourself as past digitalWrite yet unless you understand pinMode :)

Cheers,
John

GoForSmoke


As a civil engineer who started his career in traffic engineering, the idea of a traffic light that changes its phasing randomly, scares the sh!t out of me.....


Is it true that the Motorola 6800 chip started out as a traffic light controller?
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up