Pages: [1]   Go Down
Author Topic: Any1 has a sketch for a traffic light controller?  (Read 1852 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I would like to use Arduino as a controller of a traffic light, preferably 4 or 5 directions. Any one has a sketch for that?

You help would be greatly appreciated!

Thank you.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 149
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

here is an example, you cn easily change it to your needs.
I have not tested it, but there are some commnets which
explain what's going on. It is a simple state-machine implemented
with a switch statement.

Code:
// traffic light controller
// outputs for traffic lights of lane 1
#define red_1        3
#define yellow_1     4
#define green_1      5
#define walk_green_1 6
#define walk_red_1   7

// outputs for traffic lights of lane 2
#define red_2        8
#define yellow_2     9
#define green_2      10
#define walk_green_2 11
#define walk_red_2   12

// timing definitions in seconds
// green time for lane 1
#define GT_1         30
// green_time for lane 2
#define GT_2         30
// yellow to green time, same for  both lanes
#define YGT          2
// yellow to red time same for both lanes
#define YRT          5
// walk red light  before lane red light
#define WRT          10

// definition of ON and OFF

#define OFF  LOW
#define ON   HIGH

// state of the controller
int state = 0;
// next switching time in seconds
int nexttime;

void setup()
{
  pinMode(red_1,OUTPUT);
  pinMode(yellow_1,OUTPUT);
  pinMode(green_1,OUTPUT);
  pinMode(walk_green_1,OUTPUT);
  pinMode(walk_red_1,OUTPUT);
  pinMode(red_2,OUTPUT);
  pinMode(yellow_2,OUTPUT);
  pinMode(green_2,OUTPUT);
  pinMode(walk_green_2,OUTPUT);
  pinMode(walk_red_2,OUTPUT);
  nexttime = 1;
}

void loop()
{
  delay(nexttime*1000);
  switch (state) {
    case 0 : // inactive state, all off
             SetLights(1,ON,OFF,OFF,ON,OFF);
             SetLights(2,ON,OFF,OFF,ON,OFF);
             // start after WRT+5 (walkers must be gone)
             nexttime = WRT+5;
             state = 1;
             break;
    case 1 : // switch lane 1 to red/yellow
             SetLights(1,ON,ON,OFF,ON,OFF);
             SetLights(2,ON,OFF,OFF,ON,OFF);
             // goon after yellow to green time
             nexttime = YGT;
             state = 2;
             break;
    case 2 : // switch lane 1 to green
             // switch walker lights to green
             SetLights(1,OFF,OFF,ON,OFF,ON);
             SetLights(2,ON,OFF,OFF,ON,OFF);
             // wait for green time 1 minus time to switch
             // walkers light to red
             nexttime = GT_1-WRT;
             state = 3;
             break;
    case 3 : // switch lane 1 walk light to red
             SetLights(1,OFF,OFF,ON,ON,OFF);
             SetLights(2,ON,OFF,OFF,ON,OFF);
             // wait for walkers to leave the street
             nexttime = WRT;
             state = 4;
             break;
    case 4 : // switch lane 1 to yellow
             SetLights(1,OFF,ON,OFF,ON,OFF);
             SetLights(2,ON,OFF,OFF,ON,OFF);
             // wait for yellow to green time
             nexttime = YGT;
             state = 6;
             break;
    case 5 : // switch lane 1 to red
             // switch lane 2 to red/yellow
             SetLights(1,ON,OFF,OFF,ON,OFF);
             SetLights(2,ON,ON,OFF,ON,OFF);
             // wait for
             nexttime = YRT;
             state = 6;
             break;
    case 6 : // switch lane 2 to green
             // switch walker lights to green
             SetLights(1,ON,OFF,OFF,ON,OFF);
             SetLights(2,OFF,OFF,ON,OFF,ON);
             // wait for green time 1 minus time to switch
             // walkers light to red
             nexttime = GT_2-WRT;
             state = 7;
             break;
    case 7 : // switch lane 2 walk light to red
             SetLights(1,ON,OFF,OFF,ON,OFF);
             SetLights(2,OFF,OFF,ON,ON,OFF);
             // wait for walkers to leave the street
             nexttime = WRT;
             state = 8;
             break;
    case 8 : // switch lane 2 to yellow
             SetLights(1,ON,OFF,OFF,ON,OFF);
             SetLights(2,OFF,ON,OFF,ON,OFF);
             // wait for yellow to green time
             nexttime = YGT;
             state = 1;
             break;
  }
}

////////////////////////////////////////////////////////////////
// set the lights
// parameter:
// lane 1/2 set lights for this lane
// sR state of the red light
// sY state of the yellow light
// sG state of the green light
// sWR state of the walkers ed light
// sWG state of the walkers green light
void SetLights(int lane, int sR, int sY, int sG, int sWR, int sWG)
{
  if (lane == 1) {
      digitalWrite(red_1,sR);  
      digitalWrite(yellow_1,sY);  
      digitalWrite(green_1,sG);  
      digitalWrite(walk_red_1,sWR);  
      digitalWrite(walk_green_1,sWG);
  }
  else {
      digitalWrite(red_2,sR);  
      digitalWrite(yellow_2,sY);  
      digitalWrite(green_2,sG);  
      digitalWrite(walk_red_2,sWR);  
      digitalWrite(walk_green_2,sWG);
  }  
}
  

good luck
Mike
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, since im pretty new I did not notice the
Code:
switch (state) {}
control, in the reference (its in the extended)

That just my day Thanks
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Mike. I appreciate it.
I'll test it and let you know.  smiley
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Mike and all,
I tested the traffic light sketch. after very few changes it worked perfectly. I tried to develop it into a 5-lane controller instead of 2-lane but always there is an error which I could not solve. I am a beginner and hope that you take a look at the sketch to help me finding what the problem is.

Thank you,

// traffic light controller of five lanes including walkers lane

//this is the message appearing when trying to upload the sketch :
// In function 'void loop()':
//error: too few arguments to function 'void SetLights(int, int, int, int)' In function 'void SetLights
//(int, int, int, int)':


// traffic light controller
// outputs for traffic lights of lane 1
#define red_1        3
#define yellow_1     4
#define green_1             5

// outputs for traffic lights of lane 2
#define red_2        6
#define yellow_2     7
#define green_2          8

// outputs for traffic lights of lane 3
#define red_3        9
#define yellow_3     10
#define green_3          11

// outputs for traffic lights of lane 4
#define red_4        12
#define yellow_4     13
#define green_4          2

// outputs for traffic lights of lane 5 for walkers
#define green_5        1

// timing definitions in seconds
// green time for lane 1
#define GT_1         5
// green_time for lane 2
#define GT_2         5
// green_time for lane 3
#define GT_3         5
// green_time for lane 4
#define GT_4         5
// green_time for lane 5
#define GT_5         5

// yellow to green time, same for all lanes
#define YGT          2
// yellow to red time same for all lanes
#define YRT          0

// definition of ON and OFF

#define OFF  LOW
#define ON   HIGH

// state of the controller
int state = 0;
// next switching time in seconds
int nexttime;

void setup()
{
  pinMode(red_1,OUTPUT);
  pinMode(yellow_1,OUTPUT);
  pinMode(green_1,OUTPUT);
  pinMode(red_2,OUTPUT);
  pinMode(yellow_2,OUTPUT);
  pinMode(green_2,OUTPUT);
  pinMode(red_3,OUTPUT);
  pinMode(yellow_3,OUTPUT);
  pinMode(green_3,OUTPUT);
  pinMode(red_4,OUTPUT);
  pinMode(yellow_4,OUTPUT);
  pinMode(green_4,OUTPUT);
  pinMode(green_5,OUTPUT);
  nexttime = 1;
}

void loop()
{
  delay(nexttime*1000);
  switch (state) {
    case 0 : // inactive state, all off
             SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
             nexttime = 5;
             state = 2;
             break;
    case 2 : // switch lane 5 to green
             SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5,ON);
             nexttime = GT_5;
             state = 3;
             break;
    case 3 : // green flashing - Lane 5
                 digitalWrite(green_5, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(green_5, HIGH);   // set the LED on
                 delay(400);                  // wait for a second
                 digitalWrite(green_5, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(green_5, HIGH);   // set the LED on
                 delay(400);                  // wait for a second
             state = 4;
    case 4 : // switch lane 5 to red
                           SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
            nexttime = YGT;
             state = 5;
             break;
    case 5 : // switch lane 1 to green
            SetLights(1,OFF,OFF,ON);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
             nexttime = GT_1;
             state = 6;
             break;
    case 6 : // switch lane 1 to yellow
            SetLights(1,OFF,ON,ON);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
                           state = 7;
    case 7 : // yellow flashing Lane 1
                 digitalWrite(yellow_1, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_1, HIGH);   // set the LED on
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_1, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_1, HIGH);   // set the LED on
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_1, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_1, HIGH);   // set the LED on
             state = 8;            
    case 8 : // switch lane 1 to red
             // switch lane 2 to red/yellow
                           SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
             // wait for
            nexttime = YGT;
             state = 9;
             break;
    case 9 : // switch lane 2 to green
             SetLights(1,ON,OFF,OFF);
             SetLights(2,OFF,OFF,ON);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
             nexttime = GT_2;
             state = 10;
             break;
    case 10 : // switch lane 2 to yellow
            SetLights(1,ON,OFF,OFF);
             SetLights(2,OFF,ON,ON);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
                 state = 11;
    case 11 : // yellow flashing Lane 2
                 digitalWrite(yellow_2, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_2, HIGH);   // set the LED on
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_2, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_2, HIGH);   // set the LED on
                 delay(400);                  // wait for a second
             state = 12;            
    case 12 : // switch lane 2 to red
             // switch lane 2 to red/yellow
                           SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
             // wait for
             nexttime = YGT;
             state = 13;
             break;
    case 13 : // switch lane 3 to green
             SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,OFF,OFF,ON);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
             nexttime = GT_3;
             state = 14;
             break;
    case 14 : // switch lane 3 to yellow
            SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,OFF,ON,ON);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
                 state = 15;
    case 15 : // yellow flashing Lane 3
                 digitalWrite(yellow_3, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_3, HIGH);   // set the LED on
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_3, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_3, HIGH);   // set the LED on
                 delay(400);                  // wait for a second
             state = 16;            
    case 16 : // switch lane 3 to red
             // switch lane 2 to red/yellow
                           SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
             // wait for
             nexttime = YGT;
             state = 17;
             break;

    case 17 : // switch lane 4 to green
             SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,OFF,OFF,ON);
             SetLights(5, OFF);
             nexttime = GT_4;
             state = 18;
             break;
    case 18 : // switch lane 4 to yellow
            SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,OFF,ON,ON);
             SetLights(5, OFF);
                 state = 19;
    case 19 : // yellow flashing Lane 4
                 digitalWrite(yellow_4, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_4, HIGH);   // set the LED on
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_4, LOW);    // set the LED off
                 delay(400);                  // wait for a second
                 digitalWrite(yellow_4, HIGH);   // set the LED on
                 delay(400);                  // wait for a second
             state = 20;            
    case 20 : // switch lane 4 to red
             // switch lane 4 to red/yellow
                           SetLights(1,ON,OFF,OFF);
             SetLights(2,ON,OFF,OFF);
             SetLights(3,ON,OFF,OFF);
             SetLights(4,ON,OFF,OFF);
             SetLights(5, OFF);
             // wait for
             nexttime = YGT;
             state = 2;
             break;
  }
}

////////////////////////////////////////////////////////////////
// set the lights
// parameter:
// lane 1/2 set lights for this lane
// sR state of the red light
// sY state of the yellow light
// sG state of the green light
void SetLights(int lane, int sR, int sY, int sG)
{
  if (lane == 1) {
      digitalWrite(red_1,sR);
      digitalWrite(yellow_1,sY);
      digitalWrite(green_1,sG);
  }
  else {
      digitalWrite(red_2,sR);
      digitalWrite(yellow_2,sY);
      digitalWrite(green_2,sG);
  }
  else {
      digitalWrite(red_3,sR);
      digitalWrite(yellow_3,sY);
      digitalWrite(green_3,sG);
  }
  else {
      digitalWrite(red_4,sR);
      digitalWrite(yellow_4,sY);
      digitalWrite(green_4,sG);
  }
  else {
      digitalWrite(green_5,sG);
  }
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50210
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SetLights(5, OFF);

Count how many arguments there are. One, two, ... Hmm, that's not 4.
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Meanwhile, your SetLights procedure will never work correctly... there is no point having multiple 'elses'... try this:

Code:
void SetLights(int lane, int sR, int sY, int sG) {
  switch (lane) {
   case 1:
     digitalWrite(red_1,sR);
     digitalWrite(yellow_1,sY);
     digitalWrite(green_1,sG);
     break;
   case 2:
     digitalWrite(red_2,sR);
     digitalWrite(yellow_2,sY);
     digitalWrite(green_2,sG);
     break;
   case 3:
     digitalWrite(red_3,sR);
     digitalWrite(yellow_3,sY);
     digitalWrite(green_3,sG);
     break;
   case 4:
     digitalWrite(red_4,sR);
     digitalWrite(yellow_4,sY);
     digitalWrite(green_4,sG);
     break;
   case 5:
     //digitalWrite(red_5,sR); //no red 5?
     //digitalWrite(yellow_5,sY); //no yellow 5?
     digitalWrite(green_5,sG);
 }
}

And then, you don't have to have digitalWrite in your initial procedure:
Code:
case 3 : // green flashing - Lane 5
    SetLights(5, OFF, OFF, OFF);    // set the LED off
    delay(400);                  // wait for a second?? 400ms!
    SetLights(5, OFF, OFF, ON);   // set the LED on
    delay(400);                  // wait for a second?? 400ms!
    SetLights(5, OFF, OFF, OFF);    // set the LED off
    delay(400);                  // wait for a second?? 400ms!
    SetLights(5, OFF, OFF, ON);   // set the LED on
    delay(400);                  // wait for a second?? 400ms!
    state = 4;
    break;
 case 4:
     .....

Do not forget your breaks!, otherwise the code will fall through to the next case!
« Last Edit: February 24, 2010, 04:59:57 pm by stevenh » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot for your help!

I have developed a better and easier one and it works perfectly. But I still need to add a switch that turns on the 4 yellow lights into flashing mode when needed like when it's late at night.
Could you please take a look and help me adding that switch?

thanks,
Code:
/* real_us_traffic_lights2.pde
   By: Daniel Wright
   2010.JAN.30         (Modified by Ayad Derbal 2010.Feb.23)
   I kept seeing all these Traffic Light programs on the internet
   and I was real unhappy that they were all European style.
   No one had made a US Traffic light. Well, Here it is!
   Realistic US Traffic Lights without cross walk buttons.
   I am going to work on a model with the Pedestrian cross walk
   buttons next.
   */
int carRed1 = 13; // assign the car1 lights
int carYellow1 = 12;
int carGreen1 = 11;
int carRed2 = 10; // assign the car2 lights
int carYellow2 = 9;
int carGreen2 = 8;
int carRed3 = 7; // assign the car3 lights
int carYellow3 = 6;
int carGreen3 = 5;
int carRed4 = 4; // assign the car4 lights
int carYellow4 = 3;
int carGreen4 = 2;

void setup() {
pinMode(carRed1, OUTPUT);
pinMode(carYellow1, OUTPUT);
pinMode(carGreen1, OUTPUT);
pinMode(carRed2, OUTPUT);
pinMode(carYellow2, OUTPUT);
pinMode(carGreen2, OUTPUT);
pinMode(carRed3, OUTPUT);
pinMode(carYellow3, OUTPUT);
pinMode(carGreen3, OUTPUT);
pinMode(carRed4, OUTPUT);
pinMode(carYellow4, OUTPUT);
pinMode(carGreen4, OUTPUT);

// initialize the lights
digitalWrite(carRed1, HIGH);
digitalWrite(carRed2, HIGH);
digitalWrite(carRed3, HIGH);
digitalWrite(carRed4, HIGH);
delay(4000);
}

void loop() {
digitalWrite(carRed1, LOW);
digitalWrite(carYellow1, HIGH);
delay(1500);
digitalWrite(carGreen1, HIGH);
digitalWrite(carYellow1, LOW);
delay(5000);
// flash the ped red 1
for (int x=0; x<6; x++) {
digitalWrite(carYellow1, HIGH);
delay(500);
digitalWrite(carYellow1, LOW);
delay(500);
}
digitalWrite(carGreen1, LOW);
digitalWrite(carYellow1, LOW);
digitalWrite(carRed1, HIGH);

delay(2000); // wait 1 second till its safe

digitalWrite(carRed2, LOW);
digitalWrite(carYellow2, HIGH);
delay(1500);
digitalWrite(carGreen2, HIGH);
digitalWrite(carYellow2, LOW);
delay(5000);
// flash the ped red 1
for (int x=0; x<6; x++) {
digitalWrite(carYellow2, HIGH);
delay(500);
digitalWrite(carYellow2, LOW);
delay(500);
}
digitalWrite(carGreen2, LOW);
digitalWrite(carYellow2, LOW);
digitalWrite(carRed2, HIGH);
delay(2000); // wait 1 second till its safe

digitalWrite(carRed3, LOW);
digitalWrite(carYellow3, HIGH);
delay(1500);
digitalWrite(carGreen3, HIGH);
digitalWrite(carYellow3, LOW);
delay(5000);
// flash the ped red 1
for (int x=0; x<6; x++) {
digitalWrite(carYellow3, HIGH);
delay(500);
digitalWrite(carYellow3, LOW);
delay(500);
}
digitalWrite(carGreen3, LOW);
digitalWrite(carYellow3, LOW);
digitalWrite(carRed3, HIGH);
delay(2000); // wait 1 second till its safe


digitalWrite(carRed4, LOW);
digitalWrite(carYellow4, HIGH);
delay(1500);
digitalWrite(carGreen4, HIGH);
digitalWrite(carYellow4, LOW);
delay(5000);
// flash the ped red 1
for (int x=0; x<6; x++) {
digitalWrite(carYellow4, HIGH);
delay(500);
digitalWrite(carYellow4, LOW);
delay(500);
}
digitalWrite(carGreen4, LOW);
digitalWrite(carYellow4, LOW);
digitalWrite(carRed4, HIGH);
delay(2000); // wait 1 second till its safe
}


Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Could you please take a look and help me adding that switch?

aderbal,

Can you write down the exact process (I'm from Australia and we don't get a transition to Orange between Red and Green) of the traffic lights instead of making us decode it? smiley

Once we've got it sorted in English we can show you the best way to code it. Having it all in one hideous loop is ok, but not the neatest implementation.

The best method would be to have a correct timing sequence that we can then multiply to slow down. A table would work too, i.e.:
StateR1Y1G1R2Y2G2...
OFF100............
0100............
NIGHT0FLASH0............
« Last Edit: February 24, 2010, 07:48:02 pm by stevenh » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Steven for that!

The process is the following:

1-      When the controller is turned on, the red lights of North, South, East, West lights blink for 10 sec. and then go on;
2-      Lane 1:
     a.      red light turns OFF
     b.      yellow turns ON for 3 sec
     c.      yellow OFF
     d.      green ON for  … sec
     e.      yellow blinks 5 times
     f.      both green and yellow OFF
     g.      red turns ON
3-      delay for 3 sec.
4-      Lane 2:
     a.      Yellow turns ON for 3 sec.
     b.      Yellow OFF
     c.      Green ON for …sec
     d.      Yellow blink 5 times
     e.      Both green and yellow OFF
     f.      Red turns ON
5-      Delay for 3 sec       and so on

Late at night, 1am or so, yellow lights in all lanes start blinking till 6am.
An external timer is used to switch blinking mode ON at determined  times.

Pedestrians are not considered in this controller

Please let me know if you need more details  smiley

Thank you,
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This code is untested!

The basic idea is to not duplicate code.... see how it goes, ask questions... don't get stuck.

Code:
#define NORTH 0
#define EAST 1
#define SOUTH 2
#define WEST 3
#define ALL 4
#define NORTHSOUTH 5
#define EASTWEST 6

#define RED 0
#define YELLOW 1
#define GREEN 2

#define ON HIGH
#define OFF LOW

#define INITIAL_BLINK 10
#define YELLOW_FLASH_COUNT 3
#define LOOP_DELAY 3000
#define YELLOW_DELAY 3000
#define GREEN_DELAY 6000

int lightArray[4][3];
int sec = 0; //looper int for timing.

void initialiseLightArray() {
      lightArray[NORTH][RED] = 13;
      lightArray[NORTH][YELLOW] = 12;
      lightArray[NORTH][GREEN] = 11;
      lightArray[EAST][RED] = 10;
      lightArray[EAST][YELLOW] = 9;
      lightArray[EAST][GREEN] = 8;
      lightArray[SOUTH][RED] = 7;
      lightArray[SOUTH][YELLOW] = 6;
      lightArray[SOUTH][GREEN] = 5;
      lightArray[WEST][RED] = 4;
      lightArray[WEST][YELLOW] = 3;
      lightArray[WEST][GREEN] = 2;
}

void SetLights(int direction, int colour, int state) {
      switch (direction) {
            case ALL:
                  digitalWrite(lightArray[NORTH][colour],state);
                  digitalWrite(lightArray[EAST][colour],state);
                  digitalWrite(lightArray[SOUTH][colour],state);
                  digitalWrite(lightArray[WEST][colour],state);
                  break;
            case NORTHSOUTH:
                  digitalWrite(lightArray[NORTH][colour],state);
                  digitalWrite(lightArray[SOUTH][colour],state);
                  break;
            case EASTWEST:
                  digitalWrite(lightArray[EAST][colour],state);
                  digitalWrite(lightArray[WEST][colour],state);
                  break;
            default:
                  digitalWrite(lightArray[direction][colour],state);            
      }
}

void setup() {
      //SETUP the pins for the lights
      initialiseLightArray();

      //do initial Red light blink. 1sec loop.
      for (sec = 0; sec < INITIAL_BLINK; sec++) {
            SetLights(ALL,RED,ON);
            delay(250);
            SetLights(ALL,RED,OFF);
            delay(750);
      }
}

void doTrafficLights(int lights) {
      SetLights(lights,RED,OFF);
      SetLights(lights,YELLOW,ON);
      delay(YELLOW_DELAY);
      SetLights(lights,YELLOW,OFF);
      SetLights(lights,GREEN,ON);
      delay(GREEN_DELAY);
      for (sec = 0; sec < 5; sec++) {
            SetLights(lights,YELLOW,ON);
            delay(500);
            SetLights(lights,YELLOW,OFF);
            delay(500);
      }
      SetLights(lights,GREEN,OFF);
      SetLights(lights,RED,ON);
}

void loop() {      
      doTrafficLights(NORTHSOUTH);
      delay(LOOP_DELAY);
      doTrafficLights(EASTWEST);
      delay(LOOP_DELAY);
}

This does not take into account your 'external timer' for the blinking mode. Please give more information on how this is connected?
« Last Edit: February 25, 2010, 06:46:18 pm by stevenh » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Steven,
You did not test the code? I'm impressed! It works perfectly!!!
But I need to control each direction of the four separately. Also, a manual switch to turn the yellow lights into blinking mode  is important. I don't see that in this code.
So, could you help me on that.  smiley That would be appreciated!

Thanks,
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Change the loop() to:
Code:
void loop() {
      doTrafficLights(NORTH);
      delay(LOOP_DELAY);
      doTrafficLights(SOUTH);
      delay(LOOP_DELAY);
      doTrafficLights(EAST);
      delay(LOOP_DELAY);
      doTrafficLights(WEST);
      delay(LOOP_DELAY);
}

To have the direction clock-wise.

You still need to tell me how you are wiring up this switch?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It works!!!
About the switch, two wires connect a switch into the Arduino board. When this switch is turned on, the lights of all directions go off except the yellow lights which should start blinking until that switch is turned off. When this switch is turned off, the controller returns to the normal mode, which starts with red lights blinking 10 times,  etc..

I hope this helps.
thanks a lot,
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

aderbal,

Have you got a switch wired up? to which pins? and if required, do you know about pullup resistors?
Please search the arduino playground for a tutorial on digital input and get that working first. Make it read the high and low state of your switch.
Then come back here and post just that digital reading/input code... we'll then work on integrating it.

Steven.
Logged

Pages: [1]   Go Up
Jump to: