Pages: [1] 2 3   Go Down
Author Topic: Programming traffic light, want multiple loops at random  (Read 2010 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Edison Member
*
Karma: 64
Posts: 2463
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 109
Posts: 4066
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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  smiley
« Last Edit: January 02, 2013, 04:01:48 am by davenunez » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 94
Posts: 4758
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 82
Posts: 3840
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 109
Posts: 4066
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 smiley

Cheers,
John
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 94
Posts: 4758
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Jump to: