Programming traffic light, want multiple loops at random

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

"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

Okay, here's my 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....

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

What does the setup function do ??

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:

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:

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 :slight_smile:

  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.

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.

davenunez:
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

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 :stuck_out_tongue:

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

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.

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

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

rocketman247:

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 :stuck_out_tongue:

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.

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

You left out "/run like hell".

rocketman247:
...
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 :stuck_out_tongue:

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

Cheers,
John

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

Is it true that the Motorola 6800 chip started out as a traffic light controller?

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

LOL!! Did I forget to mention that this is a novelty light? I am a mechanic, dabbling in electronics and programming, and these are simply for goofiness, hanging in my shop.

PaulS:

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

You left out "/run like hell".

2nd LOL!

Grumpy_Mike:

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.

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

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

Hey, I gotta learn somewhere. I have no brilliant friends to help me, (and never even had a home computer until I was 18) so I turn here. You're probably right, so tell me; what is the best way/place/book/anything to learn code by practicing it? Could you instead show me your way, with a breakdown of WHY it works, or point me in a direction more suited to me? I want to learn this...

P.S. Please don't be mad, I know this isn't really a "How To Program" or "Here, Let Us Hold Your Hand" site, I just am kinda lost

rocketman247:
P.S. Please don't be mad, I know this isn't really a "How To Program" or "Here, Let Us Hold Your Hand" site, I just am kinda lost

I don't think that anyone is mad at you, after all we all started at some time, some have just been doing it longer than others. I have not been using the Arduino for long so have lots of sympathy for you, but if "the book" really did say this was the way to do it then it was wrong.

A good place to start is with the examples in the Arduino editor File menu. Have a look a the the Basics/Blink example to see how to set up a pin as an output and turn an LED on and off and you will see where you are going wrong. Inappropriate use of the delay command as in the example, may cause problems for you later, but one thing at a time.

rocketman247:

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

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

Hey, I gotta learn somewhere. I have no brilliant friends to help me, (and never even had a home computer until I was 18) so I turn here. You're probably right, so tell me; what is the best way/place/book/anything to learn code by practicing it? Could you instead show me your way, with a breakdown of WHY it works, or point me in a direction more suited to me? I want to learn this...

I think Mike was specifically referring to, that he did not believe that the book said to do:

ledPin = OUTPUT;

And I agree-- that line of code is very wrong :slight_smile:

Cheers,
John

Not to argue, but the "blink" example in the Arduino IDE also declares it's led as OUTPUT in pinMode. What did I do wrong again? Why is it not necessary to declare OUTPUTs in my sketch? Please, by all means, re-write mine if you must and show me.