Very nice I will study this code, Do you have anything with a pedestrian puts button? thank you
larryd:
FYI:
You can use Flags to control code execution.
// TrafficLight.ino
//
// Version YY/MM/DD
// 1.00 20/05/15 Running code
#define enabled true
#define disabled false
#define LEDon HIGH
#define LEDoff LOW
const byte redLED = 5; //+5V----[>|]----[220R]----GND
const byte greenLED = 6;
const byte yellowLED = 7;
const byte heartbeatLED = 13;
bool redFlag = true;
bool greenFlag = false;
bool yellowFlag = false;
unsigned long redMillis;
unsigned long greenMillis;
unsigned long yellowMillis;
unsigned long currentMillis;
unsigned long heartbeatMillis;
const unsigned long redInterval = 5 * 1000ul;
const unsigned long greenInterval = 5 * 1000ul;
const unsigned long yellowInterval = 5 * 1000ul;
// s e t u p ( )
//***************************************************************************************
void setup()
{
Serial.begin(9600);
pinMode(redLED, OUTPUT);
digitalWrite(redLED, LEDon);
pinMode(greenLED, OUTPUT);
digitalWrite(greenLED, LEDoff);
pinMode(yellowLED, OUTPUT);
digitalWrite(yellowLED, LEDoff);
pinMode(heartbeatLED, OUTPUT);
} //END of setup()
// l o o p ( )
//***************************************************************************************
void loop()
{
//save current time
currentMillis = millis();
//********************************
//to help show if there is any blocking code, this LED toggles every 500ms
if (currentMillis - heartbeatMillis >= 500)
{
//restart this TIMER
heartbeatMillis = currentMillis;
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//********************************
checkRed();
//********************************
checkGreen();
//********************************
checkYellow();
//********************************
// Other non blocking code goes here
//********************************
} //END of loop()
// c h e c k R e d ( )
//***************************************************************************************
void checkRed()
{
if (redFlag == enabled && currentMillis - redMillis >= redInterval)
{
digitalWrite(redLED, LEDoff);
digitalWrite(greenLED, LEDon);
redFlag = disabled;
greenFlag = enabled;
//restart the TIMER
greenMillis = currentMillis;
}
} //END of checkRed()
// c h e c k G r e e n ( )
//***************************************************************************************
void checkGreen()
{
if (greenFlag == enabled && currentMillis - greenMillis >= greenInterval)
{
digitalWrite(greenLED, LEDoff);
digitalWrite(yellowLED, LEDon);
greenFlag = disabled;
yellowFlag = enabled;
//restart the TIMER
yellowMillis = currentMillis;
}
} //END of checkGreen()
// c h e c k Y e l l o w ( )
//***************************************************************************************
void checkYellow()
{
if (yellowFlag == enabled && currentMillis - yellowMillis >= yellowInterval)
{
digitalWrite(yellowLED, LEDoff);
digitalWrite(redLED, LEDon);
yellowFlag = disabled;
redFlag = enabled;
//restart the TIMER
redMillis = currentMillis;
}
} //END of checkYellow()
//***************************************************************************************
You can use a State Machine to control code execution:
// TrafficLightStateMachine.ino
//
// Version YY/MM/DD
// 1.00 20/05/15 Running code
#define LEDon HIGH
#define LEDoff LOW
const byte redLED = 5; //+5V----[>|]----[220R]----GND
const byte greenLED = 6;
const byte yellowLED = 7;
const byte heartbeatLED = 13;
unsigned long redMillis;
unsigned long greenMillis;
unsigned long yellowMillis;
unsigned long currentMillis;
unsigned long heartbeatMillis;
const unsigned long redInterval = 5 * 1000ul;
const unsigned long greenInterval = 5 * 1000ul;
const unsigned long yellowInterval = 5 * 1000ul;
enum STATES {STARTUP, RED, GREEN, YELLOW};
STATES mState = STARTUP;
// s e t u p ( )
//***************************************************************************************
void setup()
{
Serial.begin(9600);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
} //END of setup()
// l o o p ( )
//***************************************************************************************
void loop()
{
//save current time
currentMillis = millis();
//********************************
//to help show if there is any blocking code, this LED toggles every 500ms
if (currentMillis - heartbeatMillis >= 500)
{
//restart this TIMER
heartbeatMillis = currentMillis;
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//********************************
checkTrafficLight();
//********************************
// Other non blocking code goes here
//********************************
} //END of loop()
// c h e c k T r a f f i c L i g h t ( )
//***************************************************************************************
void checkTrafficLight()
{
switch (mState)
{
//*****************
case STARTUP:
{
digitalWrite(redLED, LEDon);
digitalWrite(greenLED, LEDoff);
digitalWrite(yellowLED, LEDoff);
//next State
mState = RED;
//restart the TIMER
redMillis = currentMillis;
}
break;
//*****************
case RED:
{
if (currentMillis - redMillis >= redInterval)
{
digitalWrite(redLED, LEDoff);
digitalWrite(greenLED, LEDon);
//next State
mState = GREEN;
//restart the TIMER
greenMillis = currentMillis;
}
}
break;
//*****************
case GREEN:
{
if (currentMillis - greenMillis >= greenInterval)
{
digitalWrite(greenLED, LEDoff);
digitalWrite(yellowLED, LEDon);
//next State
mState = YELLOW;
//restart the TIMER
yellowMillis = currentMillis;
}
}
break;
//*****************
case YELLOW:
{
if (currentMillis - yellowMillis >= yellowInterval)
{
digitalWrite(yellowLED, LEDoff);
digitalWrite(redLED, LEDon);
//next State
mState = RED;
//restart the TIMER
redMillis = currentMillis;
}
}
break;
//*****************
} //END of switch/case
} //END of checkTrafficLight()
//***************************************************************************************