Hey guys, so i'm making a program for uni which simulates two 4-way traffic lights and a pedestrian light.
So I managed to finish the program using 'switch' statements inside functions. However I have been told by a lecturer that this is not acceptable. I can't have the Switch statements in it.
e.g.
int colour;
void trafficLight1()
{
switch (colour) {
case 'Red' : //Red
digitalWrite(red, HIGH);
digitalWrite(amber, LOW);
digitalWrite(green, LOW);
Serial.println("L1 = ON OFF OFF");
break;
case 'Amber' : //amber
digitalWrite(red, LOW);
digitalWrite(amber, HIGH);
digitalWrite(green, LOW);
Serial.println("L1 = OFF ON OFF");
break;
case 'Green' : //Green
digitalWrite(red, LOW);
digitalWrite(amber, LOW);
digitalWrite(green, HIGH);
Serial.println("L1 = OFF OFF ON");
break;
case 'RA' : //Red/Amber
digitalWrite(red, HIGH);
digitalWrite(amber, HIGH);
digitalWrite(green, LOW);
Serial.println("L1 = ON ON OFF");
break;
}
}
They want the code in functions like this...
void trafficLight1(int red, int amber, int green)
{
}
I've been messing around with this way and I am totally stoked at what I have to do, haha. He said it would be easy but I just keep getting this error...
"traffic_lights.ino: In function 'void trafficLight1(int, int, int)':
traffic_lights:25: error: void value not ignored as it ought to be
traffic_lights:26: error: void value not ignored as it ought to be
traffic_lights:27: error: void value not ignored as it ought to be
traffic_lights.ino: In function 'void trafficLight2(int, int, int)':
traffic_lights:31: error: 'red2On' was not declared in this scope
traffic_lights:32: error: 'amberOn' was not declared in this scope
traffic_lights:33: error: 'greenOn' was not declared in this scope
traffic_lights.ino: In function 'void loop()':
traffic_lights:37: error: too few arguments to function 'void trafficLight1(int, int, int)'
traffic_lights.ino:23:6: note: declared here
traffic_lights:38: error: too few arguments to function 'void trafficLight2(int, int, int)'
traffic_lights.ino:29:6: note: declared here
traffic_lights:41: error: too few arguments to function 'void trafficLight1(int, int, int)'
traffic_lights.ino:23:6: note: declared here
traffic_lights:42: error: too few arguments to function 'void trafficLight2(int, int, int)'
traffic_lights.ino:29:6: note: declared here
traffic_lights:45: error: too few arguments to function 'void trafficLight1(int, int, int)'
traffic_lights.ino:23:6: note: declared here
traffic_lights:46: error: too few arguments to function 'void trafficLight2(int, int, int)'
traffic_lights.ino:29:6: note: declared here
traffic_lights:49: error: too few arguments to function 'void trafficLight1(int, int, int)'
traffic_lights.ino:23:6: note: declared here
traffic_lights:50: error: too few arguments to function 'void trafficLight2(int, int, int)'
traffic_lights.ino:29:6: note: declared here
traffic_lights:58: error: too few arguments to function 'void trafficLight1(int, int, int)'
traffic_lights.ino:23:6: note: declared here
traffic_lights:59: error: too few arguments to function 'void trafficLight2(int, int, int)'
traffic_lights.ino:29:6: note: declared here
traffic_lights:68: error: too few arguments to function 'void trafficLight2(int, int, int)'
traffic_lights.ino:29:6: note: declared here
void value not ignored as it ought to be"
Below is the whole code that I am currently trying...
const int red = 13; // the pin that the LED is attached to
const int amber = 12;
const int green = 11;
const int red2 = 10;
const int amber2 = 9;
const int green2 = 8;
const int pedGreen = 7;
int incomingByte; // a variable to read incoming serial data into
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(red, OUTPUT);
pinMode(amber, OUTPUT);
pinMode(green, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(amber2, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(pedGreen, OUTPUT);
}
void trafficLight1(int red, int amber, int green)
{
red = digitalWrite(red, HIGH);
amber = digitalWrite(amber, HIGH);
green = digitalWrite(green, HIGH);
}
void trafficLight2(int red2, int amber2, int green2)
{
red2 = digitalWrite(red2, HIGH);
amber2 = digitalWrite(amber2, HIGH);
green2 = digitalWrite(green2, HIGH);
}
void loop()
{
trafficLight1(red);
trafficLight2(green);
Serial.print("L1 = ON OFF OFF L2 = OFF OFF ON PED = OFF");
delay(2000);
trafficLight1(red, amber);
trafficLight2(amber2);
Serial.print("L1 = ON ON OFF L2 = OFF ON OFF PED = OFF");
delay(2000);
trafficLight1(green);
trafficLight2(red2);
Serial.print("L1 = OFF OFF ON L2 = ON OFF OFF PED = OFF");
delay(2000);
trafficLight1(amber);
trafficLight2(red2, amber2);
Serial.print("L1 = OFF ON OFF L2 = ON ON OFF PED = OFF");
delay(2000);
if (Serial.available() > 0) // see if there's incoming serial data
{
incomingByte = Serial.read(); // read the oldest byte in the serial buffer
digitalWrite(pedGreen, HIGH);
trafficLight1(red);
trafficLight2(red2);
delay(5000);
for (int x = 0; x < 10; x++)
{
digitalWrite(pedGreen, HIGH);
delay(150);
digitalWrite(pedGreen, LOW);
delay(150);
}
trafficLight2(red2, amber2);
delay(2000);
}
}
Any help with this would be greatly appreciated, it's due Monday but uni doesn't open again until late August so can't ask a lecturer for help.
~ Jake
p.s. Sorry if this is a bit too long for a topic, the code takes up a lot of space, aha.
p.p.s. Attached is a picture of the requirements, if you fancy checking out what I have to do to get a better understanding of my issue. Thanks
