Offline
Newbie
Karma: 0
Posts: 21
|
 |
« on: January 01, 2013, 07:53:07 pm » |
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: 24
Posts: 1477
Now, More Than Ever
|
 |
« Reply #1 on: January 01, 2013, 09:37:56 pm » |
"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
|
Don't React -- Read!
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #2 on: January 01, 2013, 10:47:12 pm » |
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....
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #3 on: January 02, 2013, 02:38:15 am » |
Does your sketch do what you want or expect as it is currently written ?
What does the setup function do ??
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #4 on: January 02, 2013, 03:14:04 am » |
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 
|
|
|
|
« Last Edit: January 02, 2013, 04:01:48 am by davenunez »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #5 on: January 02, 2013, 05:54:26 am » |
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
Faraday Member
Karma: 33
Posts: 3015
I only know some basic electricity....
|
 |
« Reply #6 on: January 02, 2013, 06:05:54 am » |
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
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #7 on: January 02, 2013, 09:16:39 am » |
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: 21
|
 |
« Reply #8 on: January 02, 2013, 10:22:34 am » |
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
Edison Member
Karma: 34
Posts: 1705
|
 |
« Reply #9 on: January 02, 2013, 10:27:57 am » |
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
|
IT Crowd: Roy... "Have you tried turning it off and on again?" Moss.. "Have you tried forcing an unexpected reboot?"
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 299
Posts: 26024
Solder is electric glue
|
 |
« Reply #10 on: January 02, 2013, 10:36:00 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #11 on: January 02, 2013, 12:13:21 pm » |
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
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #12 on: January 02, 2013, 12:14:15 pm » |
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: 354
|
 |
« Reply #13 on: January 02, 2013, 12:19:03 pm » |
... 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
Faraday Member
Karma: 33
Posts: 3015
I only know some basic electricity....
|
 |
« Reply #14 on: January 02, 2013, 01:11:27 pm » |
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
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
|