Pages: [1] 2 3   Go Down
 Author Topic: Programming traffic light, want multiple loops at random  (Read 2075 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 22
 « on: January 01, 2013, 07:53:07 pm » Bigger Smaller Reset

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

Offline
Edison Member
Karma: 65
Posts: 2499
Now, More Than Ever
 « Reply #1 on: January 01, 2013, 09:37:56 pm » Bigger Smaller Reset

"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

 Logged

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

Offline
Newbie
Karma: 0
Posts: 22
 « Reply #2 on: January 01, 2013, 10:47:12 pm » Bigger Smaller Reset

Okay, here's my code:

Code:
// 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....
 Logged

East Anglia (UK)
Offline
Karma: 114
Posts: 4225
 « Reply #3 on: January 02, 2013, 02:38:15 am » Bigger Smaller Reset

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

What does the setup function do ??

 Logged

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

Offline
Newbie
Karma: 0
Posts: 3
 « Reply #4 on: January 02, 2013, 03:14:04 am » Bigger Smaller Reset

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:
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:
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
 « Last Edit: January 02, 2013, 04:01:48 am by davenunez » Logged

Seattle, WA USA
Offline
Brattain Member
Karma: 610
Posts: 49040
Seattle, WA USA
 « Reply #5 on: January 02, 2013, 05:54:26 am » Bigger Smaller Reset

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

Pittsburgh, PA, USA
Offline
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
 « Reply #6 on: January 02, 2013, 06:05:54 am » Bigger Smaller Reset

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.

 « Last Edit: January 02, 2013, 06:21:14 am by GoForSmoke » Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 361
 « Reply #7 on: January 02, 2013, 09:16:39 am » Bigger Smaller Reset

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

 Logged

Offline
Newbie
Karma: 0
Posts: 22
 « Reply #8 on: January 02, 2013, 10:22:34 am » Bigger Smaller Reset

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
 Logged

Johannesburg. UTC+2
Offline
Karma: 91
Posts: 4217
 « Reply #9 on: January 02, 2013, 10:27:57 am » Bigger Smaller Reset

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

Roy from ITCrowd: Have you tried turning it off an on again?

Manchester (England England)
Offline
Brattain Member
Karma: 618
Posts: 33959
Solder is electric glue
 « Reply #10 on: January 02, 2013, 10:36:00 am » Bigger Smaller Reset

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.

 Logged

East Anglia (UK)
Offline
Karma: 114
Posts: 4225
 « Reply #11 on: January 02, 2013, 12:13:21 pm » Bigger Smaller Reset

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

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

Seattle, WA USA
Offline
Brattain Member
Karma: 610
Posts: 49040
Seattle, WA USA
 « Reply #12 on: January 02, 2013, 12:14:15 pm » Bigger Smaller Reset

Quote
I would be interested to see the code for your walk/don't walk program.
You left out "/run like hell".
 Logged

Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 361
 « Reply #13 on: January 02, 2013, 12:19:03 pm » Bigger Smaller Reset

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

Pittsburgh, PA, USA
Offline
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
 « Reply #14 on: January 02, 2013, 01:11:27 pm » Bigger Smaller Reset

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

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

 Pages: [1] 2 3   Go Up