0
Offline
Newbie
Karma: 0
Posts: 30
Arduino rocks
|
 |
« Reply #4 on: February 23, 2010, 08:28:11 am » |
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); }
|