//////////////////////VVV lightDemo() variables only VVV//////////////////
//this cycles up and down the lights dwelling for a preset duration.
int counter = 0; /// currentl this means the program starts halfway through the red light.
bool ascending = true;
//enum lightState{red,amber,green}; id like to use a more structure state system
int redDuration = 400;
int amberDuration = 100;
int greenDuration = 400;
//////////////////^^^^ lightDemo() variables only ^^^//////////////////////
int signalToggleTestCount = 0; // for signalchangetimertest()
bool lightIsGreen = false;
void setup() {
pinMode(0, OUTPUT);
pinMode(4, OUTPUT);
pinMode(16, OUTPUT);
Serial.begin(115200);
digitalWrite(0, HIGH);
}
void lightDemo()
{
clearPins();
//////this is just a test to show the outputs work.
Serial.println(counter);
if (ascending){counter++;}else{counter--;}
if (counter <= redDuration/2){
digitalWrite(0, HIGH);
}else if(counter <= (redDuration/2) + amberDuration){
digitalWrite(4, HIGH);
}else{
digitalWrite(16, HIGH);
}
if(counter> (redDuration/2) + amberDuration + (greenDuration / 2)){ascending = false;}else if(counter < 0 ){ascending = true;}
}
void clearPins(){
digitalWrite(0, LOW);
digitalWrite(4, LOW);
digitalWrite(16, LOW);
}
///the following function assumes a trigger is recieved telling the controller what state to set the lights
void signalChange(){
/* this function is called when input is detected
we receive some value from the gate way, this only has to be on or off
we turn on the amber light for "400ms" seconds
we turn off the current (green/red) light after "400ms"
we turn off the amber light
we turn on the new (green / red) light
signal has been handled.*/
digitalWrite(4, HIGH);
delay(400); /// amber is on with previous light
if (lightIsGreen){
digitalWrite(16, LOW);
}else{digitalWrite(0, LOW);}
delay(400); // amber is on alone
lightIsGreen = !lightIsGreen;
if(lightIsGreen){
digitalWrite(16, HIGH);
}
else{digitalWrite(0, HIGH);}
digitalWrite(4, LOW);
///new light colour is set and amber is turned off.
}
void signalChangeTimerTest(){
signalToggleTestCount++;
if (signalToggleTestCount > 500){
signalChange();
signalToggleTestCount = 0;}
}
void loop()
{
// lightDemo();
// we call light demo and it just cycles up and down the lights.
signalChangeTimerTest();
//calls the signalChange() function every 5s
delay(10); // this speeds up the simulation
}