The code by "Pilotincontrol" compiles fine for me.
// CONSTANT VARIABLES
const int eastblocksensor = A0; // BLOCK ONE
const int westblocksensor = A1; // BLOCK ONE
const int eastblocktwosensor = A2; // BLOCK TWO
const int westblocktwosensor = A3; // BLOCK TWO
// VARIABLES THAT WILL CHANGE
int tcrtEBlockState = 0; // BLOCK ONE variable for reading the TCRT5000 status
int tcrtWBlockState = 0; // BLOCK ONE
int tcrtEBlockTwoState = 0; // BLOCK TWO
int tcrtWBlockTwoState = 0; // BLOCK TWO
int BLOCKONEGREEN = 11; // BLOCK ONE
int BLOCKONERED = 12; // BLOCK ONE
int BLOCKTWOGREEN = 10; // BLOCK TWO
int BLOCKTWORED = 9; // BLOCK TWO
//#define BLOCKONESTATE
//#define BLOCKLIGHTSTATES
void setup()
{
// initialize the LED pin as an output:
Serial.begin(9600);
pinMode(BLOCKONEGREEN, OUTPUT); // BLOCK ONE
pinMode(BLOCKONERED, OUTPUT); // BLOCK ONE
pinMode(BLOCKTWOGREEN, OUTPUT); // BLOCK TWO
pinMode(BLOCKTWORED, OUTPUT); // BLOCK TWO
//pinMode(led, OUTPUT);
// initialize the tcrt5000 pin as an input, and turn on the internal pullup resistors:
pinMode(westblocksensor, INPUT); // A0 BLOCK ONE
pinMode(eastblocksensor, INPUT); // A1 BLOCK ONE
pinMode(westblocktwosensor, INPUT); // A2 BLOCK TWO
pinMode(eastblocktwosensor, INPUT); // A3 BLOCK TWO
//digitalWrite(eastsensor, HIGH);
//digitalWrite(westsensor, HIGH);
digitalWrite(BLOCKONEGREEN, HIGH); // DEFAULT GREEN ON
digitalWrite(BLOCKTWOGREEN, HIGH); // DEFAULT GREEN ON
} // END OF MASTER SETUP
// BLOCK LIGHT STATES
enum BLOCKLIGHTSTATES
{
ST_GREEN, // GREEN
ST_GREENTWO, // GREEN TWO
ST_RED, // RED
ST_REDTWO, // RED TWO
};
enum BLOCKTWOLIGHTSTATES
{
ST_GREENTHREE, // GREEN THREE
ST_GREENFOUR, // GREEN FOUR
ST_REDTHREE, // RED THREE
ST_REDFOUR, // RED FOUR
};
// STATE OF BLOCK ONE GREEN
BLOCKLIGHTSTATES BLOCKONESTATE = ST_GREEN; // BLOCK ONE
BLOCKTWOLIGHTSTATES BLOCKTWOSTATE = ST_GREENTHREE; // BLOCK TWO
// CURRENT TIME
static unsigned long currentTime;
void loop()
{
// GET CURRENT TIME
currentTime = millis();
// read the state of the tcrt5000:
tcrtEBlockState = digitalRead(eastblocksensor); // BLOCK ONE
tcrtWBlockState = digitalRead(westblocksensor); // BLOCK ONE
tcrtEBlockTwoState = digitalRead(eastblocktwosensor); // BLOCK TWO
tcrtWBlockTwoState = digitalRead(westblocktwosensor); // BLOCK TWO
// check if the tcrt5000 sensor detects something.
// if it is, the tcrtState is LOW:
//digitalWrite(RIGHT, LOW);
//digitalWrite(LEFT, LOW);
Serial.println(tcrtEBlockState); // BLOCK ONE
Serial.print(tcrtWBlockState); // BLOCK ONE
Serial.print(tcrtEBlockTwoState); // BLOCK TWO
Serial.print(tcrtWBlockTwoState); // BLOCK TWO
switch (BLOCKONESTATE)
{
// GREEN LIGHT WILL SWITCH TO RED IF SENSORS ARE TRIGGERED
case ST_GREEN:
BLOCK1GREEN(tcrtEBlockState, tcrtWBlockState);
break;
case ST_GREENTWO:
BLOCK1GREEN1(tcrtEBlockState, tcrtWBlockState);
break;
case ST_RED:
BLOCK1RED(tcrtEBlockState, tcrtWBlockState);
break;
case ST_REDTWO:
BLOCK1RED1(tcrtEBlockState, tcrtWBlockState);
break;
}
switch (BLOCKTWOSTATE)
{
// GREEN LIGHT WILL SWITCH TO RED IF SENSORS ARE TRIGGERED
case ST_GREENTHREE:
BLOCK2GREEN(tcrtEBlockTwoState, tcrtWBlockTwoState);
break;
case ST_GREENFOUR:
BLOCK2GREEN2(tcrtEBlockTwoState, tcrtWBlockTwoState);
break;
case ST_REDTHREE:
BLOCK2RED(tcrtEBlockTwoState, tcrtWBlockTwoState);
break;
case ST_REDFOUR:
BLOCK2RED2(tcrtEBlockTwoState, tcrtWBlockTwoState);
break;
}
} // END MASTER LOOP
// BLOCK ONE STATE
void BLOCK1GREEN(int tcrtEBlockState, int tcrtWBlockState)
{
digitalWrite(BLOCKONEGREEN, HIGH);
digitalWrite(BLOCKONERED, LOW);
if (tcrtEBlockState == LOW && tcrtWBlockState == HIGH)
{
// CHANGE TO RED
BLOCKONESTATE = ST_RED;
}
else if (tcrtEBlockState == HIGH && tcrtWBlockState == LOW)
{
// CHANGE TO RED
BLOCKONESTATE = ST_REDTWO;
}
} //END VOID
void BLOCK1GREEN1(int tcrtEBlockState, int tcrtWBlockState)
{
// DURATION RED TIMER
const unsigned long duration = 2000;
// START TIME
static unsigned long startTime = 0;
if (startTime == 0)
{
// TURN RED TIMER
digitalWrite(BLOCKONEGREEN, LOW);
digitalWrite(BLOCKONERED, HIGH);
// START TIMER
startTime = currentTime;
// DO NOTHING
return;
}
// IF UNKNOWN SECONDS HAVE PASSED
if (currentTime - startTime >= duration)
{
// RESET VARIABLES START FROM ZERO
startTime = 0;
// CHANGE TO GREEN
BLOCKONESTATE = ST_GREEN;
}
} // END VOID
void BLOCK1RED(int tcrtEBlockState, int tcrtWBlockState)
{
digitalWrite(BLOCKONEGREEN, LOW);
digitalWrite(BLOCKONERED, HIGH);
if (tcrtEBlockState == HIGH && tcrtWBlockState == LOW)
{
// CHANGE TO GREEN
BLOCKONESTATE = ST_GREENTWO; // TIMER
}
} // END VOID
void BLOCK1RED1(int tcrtEBlockState, int tcrtWBlockState)
{
digitalWrite(BLOCKONEGREEN, LOW);
digitalWrite(BLOCKONERED, HIGH);
if (tcrtEBlockState == LOW && tcrtWBlockState == HIGH)
{
// CHANGE TO GREEN
BLOCKONESTATE = ST_GREENTWO; // TIMER
}
} // END VOID
// BLOCK TWO STATE
void BLOCK2GREEN(int tcrtEBlockTwoState, int tcrtWBlockTwoState)
{
digitalWrite(BLOCKTWOGREEN, HIGH);
digitalWrite(BLOCKTWORED, LOW);
if (tcrtEBlockTwoState == LOW && tcrtWBlockTwoState == HIGH)
{
// CHANGE TO RED
BLOCKTWOSTATE = ST_REDTHREE;
}
else if (tcrtEBlockTwoState == HIGH && tcrtWBlockTwoState == LOW)
{
// CHANGE TO RED
BLOCKTWOSTATE = ST_REDFOUR;
}
}
void BLOCK2GREEN2(int tcrtEBlockTwoState, int tcrtWBlockTwoState)
{
// DURATION RED TIMER
const unsigned long duration = 2000;
// START TIME
static unsigned long startTime = 0;
if (startTime == 0)
{
// TURN RED TIMER
digitalWrite(BLOCKTWOGREEN, LOW);
digitalWrite(BLOCKTWORED, HIGH);
// START TIMER
startTime = currentTime;
// DO NOTHING
return;
}
// IF UNKNOWN SECONDS HAVE PASSED
if (currentTime - startTime >= duration)
{
// RESET VARIABLES START FROM ZERO
startTime = 0;
// CHANGE TO GREEN
BLOCKTWOSTATE = ST_GREENTHREE;
}
}
void BLOCK2RED(int tcrtEBlockTwoState, int tcrtWBlockTwoState)
{
digitalWrite(BLOCKTWOGREEN, LOW);
digitalWrite(BLOCKTWORED, HIGH);
if (tcrtEBlockTwoState == HIGH && tcrtWBlockTwoState == LOW)
{
// CHANGE TO GREEN
BLOCKTWOSTATE = ST_GREENFOUR;
}
}
void BLOCK2RED2(int tcrtEBlockTwoState, int tcrtWBlockTwoState)
{
digitalWrite(BLOCKTWOGREEN, LOW);
digitalWrite(BLOCKTWORED, HIGH);
if (tcrtEBlockTwoState == LOW && tcrtWBlockTwoState == HIGH)
{
// CHANGE TO GREEN
BLOCKTWOSTATE = ST_GREENFOUR;
}
}