Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- //*****************************************^*****************************************
- PATH /home/john/Arduino/_My_Working_Arduino_Projects/My_Working_USA_Traffic_System_1/My_Working_USA_Traffic_System_1.ino
- USA Traffic Light System Script, by John James Guarnieri, 09_15_2023
- Credits: LarryD Arduino Forum https://forum.arduino.cc/t/help-with-adding-millis-and-replace-delay-100-in-this-sketch/655674/23
- This original script had just one "set" of signal lights RED, YELLOW, GREEN, and one pedestrian button, and two Pedestrian LED's, RED and GREEN
- I am in the process of editing, adding to,and changing the script for my needs, to have both a North South Traffic Light, and an East_West Traffic Light, with Pedestrian buttons
- Also, I made an option to either have the Pedestrian walk RED led work automatically "which I am currently preferring",
- or to be constantly "ON" RED, until a Predestrion button press is initiated, this is done with the bool flag "North_South_Bypass_The_Pedestrian_Button"
- Also a USA traffic light has 12 LED's, by hooking up the LED's in "series" I will only need 6 wires + 1 ground wire "connected to all grounds of the LED's", to run all 12 LED's, cuts doen on the rat's nest
- // 1 - 39 ohm resistor for 2 red or yellow LED's in series
- // 1 - 56 ohm resistor for 2 green LED in series
- //***********************************************************************************
- // TrafficLightStateMachinePedSwitch.ino
- //
- // Version YY/MM/DD
- // 1.00 19/07/23 Running code (to be added, other direction coding)
- // 1.10 20/05/17 Added switch filtering, adjusted LED timing
- // 1.20 20/05/27 Added the red walk LED stuff
- // 1.30 20/05/28 Added red don't walk light delay
- //
- //*****************************************^*****************************************
- */
- #define LEDon HIGH //BEGIN: My add/Edit
- #define LEDoff LOW
- #define noCHANGE 0
- #define wasPUSHED 1
- #define wasRELEASED 2
- #define ENABLED true
- #define DISABLED false
- #define PUSHED LOW //+5V---Internal Pullup---PIN---[switch]---GND
- //*****************************************^*****************************************
- //Switch 'structure' definition
- struct North_South_defineSwitch
- {
- const byte pin; //physical pin being used
- const byte pushedLevel; //pin level when the switch is pushed
- byte lastState; //last state the switch was in
- byte counter; //switch filtering count
- unsigned long pushedMillis; //how long the switch was pressed for
- };
- //*****************************************^*****************************************
- struct East_West_defineSwitch
- {
- const byte pin; //physical pin being used
- const byte pushedLevel; //pin level when the switch is pushed
- byte lastState; //last state the switch was in
- byte counter; //switch filtering count
- unsigned long pushedMillis; //how long the switch was pressed for
- };
- //*****************************************^*****************************************
- North_South_defineSwitch North_South_walkSwitch = { 2, PUSHED, !PUSHED, 0, 0 }; //pedestrian walk switch
- const byte North_South_redLED = 5; //+5V----[>|]----[220R]----GND ATTENTION! two RED LED's in series use a 33 ohm resistor
- const byte North_South_greenLED = 7;
- const byte North_South_yellowLED = 6;
- const byte North_South_walk_green_LED = 4;
- const byte North_South_walk_red_LED = 3;
- const byte heartbeatLED = 13;
- //East_West
- East_West_defineSwitch East_West_walkSwitch = { 2, PUSHED, !PUSHED, 0, 0 }; //pedestrian walk switch
- const byte East_West_redLED = 8; //+5V----[>|]----[220R]----GND ATTENTION! two RED LED's in series use a 33 ohm resistor
- const byte East_West_greenLED = 10;
- const byte East_West_yellowLED = 9;
- const byte East_West_walk_green_LED = 12;
- const byte East_West_walk_red_LED = 11;
- const byte East_West_heartbeatLED = 13;
- const byte ResetPin = 1; //use this to reset the microcontroller in code
- //*****************************************^*****************************************
- bool North_South_walkFlag = DISABLED;
- bool North_South_pedFlag = DISABLED;
- bool North_South_REDdontWalkFlag = DISABLED;
- bool North_South_flashingFlag = DISABLED;
- bool East_West_walkFlag = DISABLED;
- bool East_West_pedFlag = DISABLED;
- bool East_West_REDdontWalkFlag = DISABLED;
- bool East_West_flashingFlag = DISABLED;
- //===================================================================================================================
- //BEGIN: My add/Edit you decide if you want the "North_South_walkFlag" and/or the "East_West_walkFlag" RED LED to be automatic or manual using the Pedestrian button press
- //===================================================================================================================
- //BE SURE TO UNCOMMENT THE CORRECT BOOL IF YOU DECIDE, TO MAKE THE CHANGE, FRON PEDESTRIAN BUTTON TO NO PEDESTRIAN BUTTON
- bool Bypass_The_Pedestrian_Button = ENABLED; // CURRENTLY THE PREFERRED METHOD, Yes, Bypass the Pedestrian button "AUTOMATIC" Pedestrian button
- //bool Bypass_The_Pedestrian_Button = DISABLED; // No, don't bypass. Use the Pedestrian button MANUALLY
- //===================================================================================================================
- //END: My add/Edit you decide if you want the "North_South_walkFlag" and/or the "East_West_walkFlag" RED LED to be automatic or manual using the Pedestrian button press
- //===================================================================================================================
- //===================================================================================================================
- //BEGIN: My add/Edit you decide if you want the Pedestrian Blinking LED to br green or red
- //===================================================================================================================
- bool Pedestrian_Crossing_Blinking_LED_is_green = ENABLED; //yes use the green LED
- //bool Pedestrian_Crossing_Blinking_LED_is_green = DISABLED; //No, use the red LED
- //===================================================================================================================
- //END: My add/Edit you decide if you want the Pedestrian Blinking LED to br Green or Red
- //===================================================================================================================
- unsigned long North_South_redMillis;
- unsigned long North_South_greenMillis;
- unsigned long North_South_yellowMillis;
- unsigned long North_South_REDdontWalkMillis;
- unsigned long North_South_currentMillis;
- unsigned long heartbeatMillis;
- unsigned long North_South_switchMillis;
- unsigned long North_South_pedMillis;
- unsigned long North_South_flashingMillis;
- const unsigned long North_South_switchDelay = 25; //25ms
- unsigned long East_West_redMillis;
- unsigned long East_West_greenMillis;
- unsigned long East_West_yellowMillis;
- unsigned long East_West_REDdontWalkMillis;
- unsigned long East_West_currentMillis;
- unsigned long East_West_heartbeatMillis;
- unsigned long East_West_switchMillis;
- unsigned long East_West_pedMillis;
- unsigned long East_West_flashingMillis;
- const unsigned long East_West_switchDelay = 25; //25ms
- /*
- //********************************
- //BEGIN:define our light on times ORIGINAL
- //
- //Change as needed
- //#define North_South_redInterval 60 * 1000ul //ON time in seconds
- //#define North_South_greenInterval 60 * 1000ul //ON time in seconds
- //#define North_South_yellowInterval 12 * 1000ul //ON time in seconds
- //#define North_South_flashingDelay 40 * 1000ul //Time at which the flashing starts
- //#define North_South_pedInterval 50 * 1000ul //Time at which the walk light goes off
- //#define North_South_REDdontWalkInterval 3 * 1000ul //wait 3 seconds before the "Don't Walk" comes on
- #define North_South_flashingInterval 200ul
- #define North_South_minHoldTime 500ul //switch must be held for at least this time
- //*************************
- //For Testing
- //#define North_South_redInterval 10 * 1000ul //ON time in seconds ORIGINAL
- //#define North_South_greenInterval 10 * 1000ul //ON time in seconds ORIGINAL
- //#define North_South_yellowInterval 4 * 1000ul //ON time in seconds ORIGINAL
- #define North_South_redInterval 5 * 4000ul //ON time in seconds ORIGINAL
- #define North_South_greenInterval 5 * 4000ul //ON time in seconds ORIGINAL
- #define North_South_yellowInterval 4800ul //ON time in seconds ORIGINAL
- //#define North_South_flashingDelay 3 * 1000ul //Time at which the flashing starts ORIGINAL
- #define North_South_flashingDelay 15 * 1000ul //Time at which the flashing starts "this keeps the green walk light on for n seconds before the blink starts" TESTING HERE
- //#define North_South_pedInterval 5 * 1000ul //Time at which the walk light goes off ORIGINAL
- #define North_South_pedInterval 25 * 1000ul //Time at which the walk light turns off "this is the blink duration" TESTING HERE
- //#define North_South_REDdontWalkInterval 3 * 1000ul //wait 3 seconds before the "Don't Walk" comes on ORIGINAL
- #define North_South_REDdontWalkInterval 250ul //wait 3 seconds before the "Don't Walk" comes on TESTING HERE
- //*************************
- //END:define our light on times ORIGINAL
- */
- //===================================================================================================================
- //BEGIN: My add/Edit North_South
- //===================================================================================================================
- #define North_South_flashingInterval 200ul
- #define North_South_minHoldTime 500ul //switch must be held for at least this time
- //*************************
- //#define North_South_redInterval 5 * 6800ul //ON time in seconds ORIGINAL
- #define North_South_redInterval 5 * 6020ul //ON time in seconds TESTING WORKING GREAT
- //#define North_South_greenInterval 5 * 5800ul //ON time in seconds ORIGINAL
- #define North_South_greenInterval 5 * 5000ul //ON time in seconds TESTING WORKING GREAT
- #define North_South_yellowInterval 4800ul //ON time in seconds
- //#define North_South_flashingDelay 3 * 1000ul //Time at which the flashing starts ORIGINAL
- #define North_South_flashingDelay 15 * 1000ul //Time at which the flashing starts "this keeps the green walk light on for n seconds before the blink starts" TESTING HERE
- //#define North_South_pedInterval 5 * 1000ul //Time at which the walk light goes off ORIGINAL
- #define North_South_pedInterval 25 * 1000ul //Time at which the walk light turns off "this is the blink duration" TESTING HERE
- //#define North_South_REDdontWalkInterval 3 * 1000ul //wait 3 seconds before the "Don't Walk" comes on ORIGINAL
- #define North_South_REDdontWalkInterval 250ul //wait 3 seconds before the "Don't Walk" comes on TESTING HERE
- //===================================================================================================================
- //BEGIN: My add/Edit East_West
- //===================================================================================================================
- //#define East_West_redInterval 5 * 6800ul //ON time in seconds ORIGINAL
- #define East_West_redInterval 5 * 6020ul //ON time in seconds TESTING WORKING GREAT
- //#define East_West_greenInterval 5 * 5800ul //ON time in seconds ORIGINAL
- #define East_West_greenInterval 5 * 5000ul //ON time in seconds TESTING WORKING GREAT
- #define East_West_yellowInterval 4800ul //ON time in seconds
- //#define East_West_flashingDelay 3 * 1000ul //Time at which the flashing starts ORIGINAL
- #define East_West_flashingDelay 15 * 1000ul //Time at which the flashing starts "this keeps the green walk light on for n seconds before the blink starts" TESTING HERE
- //#define East_West_pedInterval 5 * 1000ul //Time at which the walk light goes off ORIGINAL
- #define East_West_pedInterval 25 * 1000ul //Time at which the walk light turns off "this is the blink duration" TESTING HERE
- //#define East_West_REDdontWalkInterval 3 * 1000ul //wait 3 seconds before the "Don't Walk" comes on ORIGINAL
- #define East_West_REDdontWalkInterval 250ul //wait 3 seconds before the "Don't Walk" comes on TESTING HERE
- #define East_West_flashingInterval 200ul
- #define East_West_minHoldTime 500ul //Pedestrian switch must be held for at least this time
- //===================================================================================================================
- //END: My add/Edit
- //===================================================================================================================
- //*************************
- //states in our machine
- enum North_South_STATES { North_South_STARTUP,
- North_South_RED,
- North_South_GREEN,
- North_South_YELLOW,
- North_South_CROSSING,
- North_South_REDdontWalk };
- North_South_STATES North_South_mState = North_South_STARTUP;
- //states in our machine
- enum East_West_STATES { East_West_STARTUP,
- East_West_GREEN,
- East_West_YELLOW,
- East_West_RED,
- East_West_CROSSING,
- East_West_REDdontWalk };
- East_West_STATES East_West_mState = East_West_STARTUP;
- // s e t u p ( )
- //*****************************************^*****************************************
- void setup() {
- Serial.begin(115200);
- pinMode(North_South_redLED, OUTPUT);
- pinMode(North_South_greenLED, OUTPUT);
- pinMode(North_South_yellowLED, OUTPUT);
- pinMode(North_South_walk_red_LED, OUTPUT);
- pinMode(North_South_walk_green_LED, OUTPUT);
- pinMode(heartbeatLED, OUTPUT);
- pinMode(ResetPin, OUTPUT);
- pinMode(ResetPin, LOW); //off
- pinMode(North_South_walkSwitch.pin, INPUT_PULLUP);
- North_South_walkSwitch.lastState = digitalRead(North_South_walkSwitch.pin);
- pinMode(East_West_redLED, OUTPUT);
- pinMode(East_West_greenLED, OUTPUT);
- pinMode(East_West_yellowLED, OUTPUT);
- pinMode(East_West_walk_red_LED, OUTPUT);
- pinMode(East_West_walk_green_LED, OUTPUT);
- pinMode(heartbeatLED, OUTPUT);
- pinMode(East_West_walkSwitch.pin, INPUT_PULLUP);
- East_West_walkSwitch.lastState = digitalRead(East_West_walkSwitch.pin);
- //pinMode (otherSwitch.pin, INPUT_PULLUP);
- //otherSwitch.lastState = digitalRead(otherSwitch.pin);
- } //END of setup()
- // l o o p ( )
- //*****************************************^*****************************************
- void loop() {
- //save current time
- North_South_currentMillis = millis();
- //********************************
- //to help show if there is any blocking code, this LED toggles every 1000ms
- if (North_South_currentMillis - heartbeatMillis >= 1000) {
- Serial.print("Time = ");
- Serial.println(North_South_currentMillis / 1000ul);
- //start this TIMER
- heartbeatMillis = North_South_currentMillis;
- digitalWrite(heartbeatLED, !digitalRead(heartbeatLED)); //built in LED pin 13
- }
- //********************************
- North_South_checkSwitches();
- //********************************
- North_South_checkTrafficLight();
- //********************************
- // Other non blocking code goes here
- //********************************
- //East_West_ here
- East_West_currentMillis = millis();
- //********************************
- //to help show if there is any blocking code, this LED toggles every 1000ms
- if (East_West_currentMillis - heartbeatMillis >= 1000) {
- Serial.print("Time = ");
- Serial.println(East_West_currentMillis / 1000ul);
- //start this TIMER
- heartbeatMillis = East_West_currentMillis;
- digitalWrite(heartbeatLED, !digitalRead(heartbeatLED)); //built in LED pin 13
- }
- //********************************
- East_West_checkSwitches();
- //********************************
- East_West_checkTrafficLight();
- //********************************
- // Other non blocking code goes here
- //********************************
- } //END of loop()
- //*****************************************^*****************************************
- //BEGIN: TAB 1 NORTH_SOUTH TRAFFIC lIGHTS
- //*****************************************^*****************************************
- // c h e c k T r a f f i c L i g h t ( )
- //*****************************************^*****************************************
- void North_South_checkTrafficLight() {
- switch (North_South_mState) {
- //*****************
- case North_South_STARTUP:
- {
- digitalWrite(North_South_redLED, LEDon);
- digitalWrite(North_South_greenLED, LEDoff);
- digitalWrite(North_South_yellowLED, LEDoff);
- digitalWrite(North_South_walk_red_LED, LEDon);
- digitalWrite(North_South_walk_green_LED, LEDoff);
- //start the TIMER
- North_South_redMillis = North_South_currentMillis;
- //next State
- //ORIGINAL HERE
- North_South_mState = North_South_RED;
- //TESTING HERE
- //North_South_pedFlag = ENABLED;
- //North_South_mState = North_South_GREEN;
- Serial.print("North_South_RED Traffic ON @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- }
- break;
- //*****************
- case North_South_RED:
- {
- if (North_South_currentMillis - North_South_redMillis >= North_South_redInterval) {
- Serial.print("North_South_Red Traffic OFF @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- digitalWrite(North_South_redLED, LEDoff);
- digitalWrite(North_South_greenLED, LEDon);
- //start the TIMER
- North_South_greenMillis = North_South_currentMillis;
- Serial.print("North_South_Green Traffic ON @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- //===================================================================================================================
- //BEGIN: My add/Edit "North_South_walkFlag" forced to ENABLED
- //===================================================================================================================
- if (Bypass_The_Pedestrian_Button) //This flag determins "AUTOMATIC" if "true", or "MANUAL" Pedestrian button if "false"
- {
- North_South_walkFlag = ENABLED;
- }
- //===================================================================================================================
- //END: My add/Edit "North_South_walkFlag" forced to ENABLED
- //===================================================================================================================
- if (North_South_walkFlag == ENABLED) {
- digitalWrite(North_South_walk_red_LED, LEDoff);
- digitalWrite(North_South_walk_green_LED, LEDon);
- //start the TIMER
- North_South_pedMillis = North_South_currentMillis;
- North_South_walkFlag = DISABLED;
- North_South_pedFlag = ENABLED;
- Serial.print("North_South_Walk Green ON @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- }
- //next State
- North_South_mState = North_South_GREEN;
- }
- }
- break;
- //*****************
- break;
- //*****************
- case North_South_GREEN:
- {
- if (North_South_pedFlag == ENABLED) {
- North_South_mState = North_South_CROSSING;
- }
- if (North_South_REDdontWalkFlag == ENABLED) {
- North_South_mState = North_South_REDdontWalk;
- }
- if (North_South_REDdontWalkFlag == DISABLED && North_South_pedFlag == DISABLED && North_South_currentMillis - North_South_greenMillis >= North_South_greenInterval) {
- Serial.print("North_South_Green Traffic OFF @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- digitalWrite(North_South_greenLED, LEDoff);
- digitalWrite(North_South_yellowLED, LEDon);
- //start the TIMER
- North_South_yellowMillis = North_South_currentMillis;
- //next State
- North_South_mState = North_South_YELLOW;
- Serial.print("North_South_Yellow Traffic ON @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- }
- }
- break;
- //*****************
- case North_South_CROSSING:
- {
- //next State
- North_South_mState = North_South_GREEN;
- //time to toggle LED ?
- if (North_South_flashingFlag == ENABLED && North_South_currentMillis - North_South_flashingMillis >= North_South_flashingInterval) {
- //start the TIMER
- North_South_flashingMillis = North_South_currentMillis;
- //toggle LED, blink the Green LED, that crossing time is getting short, as a warning to the Pedestrian
- if (Pedestrian_Crossing_Blinking_LED_is_green)
- {
- digitalWrite(North_South_walk_green_LED, !digitalRead(North_South_walk_green_LED));
- }
- else //The bool in Declarations, "Pedestrian_Crossing_Blinking_LED_is_green", is DISABLED
- {
- //blink the Red LED, that crossing time is getting short, as a warning to the Pedestrian
- digitalWrite(North_South_walk_green_LED, LEDoff);
- digitalWrite(North_South_walk_red_LED, !digitalRead(North_South_walk_red_LED));
- }
- }
- //is it time to start flashing the walk light ?
- if (North_South_flashingFlag == DISABLED && North_South_currentMillis - North_South_pedMillis >= North_South_flashingDelay) {
- North_South_flashingFlag = ENABLED;
- //start the TIMER
- North_South_flashingMillis = North_South_currentMillis;
- Serial.print("North_South_Walk Green Flashing @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- }
- //has walking time expired ?
- if (North_South_currentMillis - North_South_pedMillis >= North_South_pedInterval) {
- North_South_pedFlag = DISABLED;
- North_South_flashingFlag = DISABLED;
- digitalWrite(North_South_walk_green_LED, LEDoff);
- Serial.print("North_South_Walk Green OFF 2 ");
- Serial.println(North_South_currentMillis / 1000ul);
- North_South_REDdontWalkFlag = ENABLED;
- //start TIMER
- North_South_REDdontWalkMillis = North_South_currentMillis;
- //next State
- North_South_mState = North_South_REDdontWalk;
- }
- }
- break;
- //*****************
- case North_South_YELLOW:
- {
- if (North_South_currentMillis - North_South_yellowMillis >= North_South_yellowInterval) {
- Serial.print("North_South_Yellow Traffic OFF @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- Serial.print("North_South_Red Traffic ON @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- digitalWrite(North_South_yellowLED, LEDoff);
- digitalWrite(North_South_redLED, LEDon);
- //start the TIMER
- North_South_redMillis = North_South_currentMillis;
- //next State
- North_South_mState = North_South_RED;
- }
- }
- break;
- //*****************
- case North_South_REDdontWalk:
- {
- //next State
- North_South_mState = North_South_GREEN;
- if (North_South_currentMillis - North_South_REDdontWalkMillis > North_South_REDdontWalkInterval) {
- North_South_REDdontWalkFlag = DISABLED;
- digitalWrite(North_South_walk_red_LED, LEDon);
- Serial.print("North_South_Walk Red ON @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- }
- }
- break;
- } //END of switch/case
- } //END of North_South_checkTrafficLight()
- // c h e c k S w i t c h e s ( )
- //*****************************************^*****************************************
- void North_South_checkSwitches() {
- //***************************
- //Time to check the switches?
- if (North_South_currentMillis - North_South_switchMillis < North_South_switchDelay) {
- //it is not time yet
- return;
- }
- //restart the TIMER
- North_South_switchMillis = North_South_switchMillis + North_South_switchDelay;
- //*************************** >>>>-----> w a l k S w i t c h
- switch (North_South_checkThisSwitch(North_South_walkSwitch)) {
- case wasPUSHED:
- {
- }
- break;
- case wasRELEASED:
- {
- //only if the switch was held for North_South_minHoldTime or more seconds
- if (North_South_walkSwitch.pushedMillis >= North_South_minHoldTime) {
- North_South_walkFlag = ENABLED;
- Serial.print("North_South_Switch pushed @ ");
- Serial.println(North_South_currentMillis / 1000ul);
- }
- }
- break;
- }
- //***************************
- //Other switches go here
- //***************************
- } //END of North_South_checkSwitches()
- // c h e c k T h i s S w i t c h ( )
- //*****************************************^*****************************************
- //handles switch change in state, timing and filtering
- byte North_South_checkThisSwitch(North_South_defineSwitch& North_South_SWITCH) {
- //***************************
- byte North_South_currentState = digitalRead(North_South_SWITCH.pin);
- //has the North_South_SWITCH changed state ?
- if (North_South_SWITCH.lastState == North_South_currentState) {
- //no change, reset the filter counter
- North_South_SWITCH.counter = 0;
- //there was no change detected
- return noCHANGE;
- }
- //there was a North_South_SWITCH change in state
- North_South_SWITCH.counter++;
- //was this the 2nd sequential time we detected the change
- if (North_South_SWITCH.counter > 1) {
- //a valid North_South_SWITCH change was detected
- //get ready for the next 2 filter samples
- North_South_SWITCH.counter = 0;
- //update the North_South_SWITCH state
- North_South_SWITCH.lastState = North_South_currentState;
- //was the North_South_SWITCH pushed ?
- if (North_South_currentState == North_South_SWITCH.pushedLevel) {
- //record the time the North_South_SWITCH was pushed
- North_South_SWITCH.pushedMillis = North_South_currentMillis;
- return wasPUSHED;
- }
- else {
- //the North_South_SWITCH was released
- //save the time the North_South_SWITCH 'remained' pressed
- North_South_SWITCH.pushedMillis = North_South_currentMillis - North_South_SWITCH.pushedMillis;
- return wasRELEASED;
- }
- } //END of if(North_South_SWITCH.counter > 1)
- //there was no valid change detected
- return noCHANGE;
- } //END of North_South_checkThisSwitch()
- //*****************************************^*****************************************
- //*****************************************^*****************************************
- //*****************************************^*****************************************
- //END: TQB 1 NORTH_SOUTH TRAFFIC lIGHTS
- //*****************************************^*****************************************
- //*****************************************^*****************************************
- //BEGIN: TAB 2 EAST_WEST TRAFFIC lIGHTS
- //*****************************************^*****************************************
- // c h e c k T r a f f i c L i g h t ( )
- //*****************************************^*****************************************
- void East_West_checkTrafficLight() {
- switch (East_West_mState) {
- //*****************
- case East_West_STARTUP:
- {
- //ORIGINAL HERE
- //digitalWrite(East_West_redLED, LEDon);
- //digitalWrite(East_West_greenLED, LEDoff);
- //digitalWrite(East_West_yellowLED, LEDoff);
- //digitalWrite(East_West_walk_red_LED, LEDon);
- //digitalWrite(East_West_walk_green_LED, LEDoff);
- //TESTING HERE
- digitalWrite(East_West_redLED, LEDoff);
- digitalWrite(East_West_greenLED, LEDon);
- digitalWrite(East_West_yellowLED, LEDoff);
- digitalWrite(East_West_walk_red_LED, LEDoff);
- digitalWrite(East_West_walk_green_LED, LEDon);
- //start the TIMER
- East_West_redMillis = East_West_currentMillis;
- //next State
- //ORIGINAL HERE
- //East_West_mState = East_West_RED;
- //TESTING HERE
- East_West_pedFlag = ENABLED;
- East_West_mState = East_West_GREEN;
- //East_West_mState = East_West_CROSSING;
- Serial.print("East_West_RED Traffic ON @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- }
- break;
- //*****************
- case East_West_RED:
- {
- if (East_West_currentMillis - East_West_redMillis >= East_West_redInterval) {
- Serial.print("East_West_Red Traffic OFF @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- digitalWrite(East_West_redLED, LEDoff);
- digitalWrite(East_West_greenLED, LEDon);
- //TESTING HERE
- //digitalWrite(East_West_redLED, LEDon);
- //digitalWrite(East_West_greenLED, LEDoff);
- //start the TIMER
- East_West_greenMillis = East_West_currentMillis;
- Serial.print("East_West_Green Traffic ON @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- //===================================================================================================================
- //BEGIN: My add/Edit "East_West_walkFlag" forced to ENABLED
- //===================================================================================================================
- if (Bypass_The_Pedestrian_Button) //This flag determins "AUTOMATIC" if "true", or "MANUAL" Pedestrian button if "false"
- {
- East_West_walkFlag = ENABLED;
- }
- //===================================================================================================================
- //END: My add/Edit "East_West_walkFlag" forced to ENABLED
- //===================================================================================================================
- if (East_West_walkFlag == ENABLED) {
- digitalWrite(East_West_walk_red_LED, LEDoff);
- digitalWrite(East_West_walk_green_LED, LEDon);
- //TESTING HERE
- //digitalWrite(East_West_walk_red_LED, LEDon);
- //digitalWrite(East_West_walk_green_LED, LEDoff);
- //start the TIMER
- East_West_pedMillis = East_West_currentMillis;
- East_West_walkFlag = DISABLED;
- East_West_pedFlag = ENABLED;
- Serial.print("East_West_Walk Green ON @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- }
- //next State
- East_West_mState = East_West_GREEN;
- }
- }
- break;
- //*****************
- break;
- //*****************
- case East_West_GREEN:
- {
- if (East_West_pedFlag == ENABLED) {
- East_West_mState = East_West_CROSSING;
- }
- if (East_West_REDdontWalkFlag == ENABLED) {
- East_West_mState = East_West_REDdontWalk;
- }
- if (East_West_REDdontWalkFlag == DISABLED && East_West_pedFlag == DISABLED && East_West_currentMillis - East_West_greenMillis >= East_West_greenInterval) {
- Serial.print("East_West_Green Traffic OFF @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- digitalWrite(East_West_greenLED, LEDoff);
- digitalWrite(East_West_yellowLED, LEDon);
- //TESTING HERE
- //digitalWrite(East_West_greenLED, LEDoff);
- //digitalWrite(East_West_yellowLED, LEDon);
- //start the TIMER
- East_West_yellowMillis = East_West_currentMillis;
- //next State
- East_West_mState = East_West_YELLOW;
- Serial.print("East_West_Yellow Traffic ON @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- }
- }
- break;
- //*****************
- case East_West_CROSSING:
- {
- //next State
- East_West_mState = East_West_GREEN;
- //time to toggle LED ?
- if (East_West_flashingFlag == ENABLED && East_West_currentMillis - East_West_flashingMillis >= East_West_flashingInterval) {
- //start the TIMER
- East_West_flashingMillis = East_West_currentMillis;
- //toggle LED, blink the Green LED, that crossing time is getting short, as a warning to the Pedestrian
- if (Pedestrian_Crossing_Blinking_LED_is_green)
- {
- digitalWrite(East_West_walk_green_LED, !digitalRead(East_West_walk_green_LED));
- }
- else //The bool in Declarations, "Pedestrian_Crossing_Blinking_LED_is_green", is DISABLED
- {
- //blink the Red LED, that crossing time is getting short, as a warning to the Pedestrian
- digitalWrite(East_West_walk_green_LED, LEDoff);
- digitalWrite(East_West_walk_red_LED, !digitalRead(East_West_walk_red_LED));
- }
- }
- //is it time to start flashing the walk light ?
- if (East_West_flashingFlag == DISABLED && East_West_currentMillis - East_West_pedMillis >= East_West_flashingDelay) {
- East_West_flashingFlag = ENABLED;
- //start the TIMER
- East_West_flashingMillis = East_West_currentMillis;
- Serial.print("East_West_Walk Green Flashing @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- }
- //has walking time expired ?
- if (East_West_currentMillis - East_West_pedMillis >= East_West_pedInterval) {
- East_West_pedFlag = DISABLED;
- East_West_flashingFlag = DISABLED;
- digitalWrite(East_West_walk_green_LED, LEDoff);
- Serial.print("East_West_Walk Green OFF 2 ");
- Serial.println(East_West_currentMillis / 1000ul);
- East_West_REDdontWalkFlag = ENABLED;
- //start TIMER
- East_West_REDdontWalkMillis = East_West_currentMillis;
- //next State
- East_West_mState = East_West_REDdontWalk;
- }
- }
- break;
- //*****************
- case East_West_YELLOW:
- {
- if (East_West_currentMillis - East_West_yellowMillis >= East_West_yellowInterval) {
- Serial.print("East_West_Yellow Traffic OFF @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- Serial.print("East_West_Red Traffic ON @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- digitalWrite(East_West_yellowLED, LEDoff);
- digitalWrite(East_West_redLED, LEDon);
- //testing
- //digitalWrite(East_West_yellowLED, LEDon);
- //digitalWrite(East_West_redLED, LEDoff);
- //start the TIMER
- East_West_redMillis = East_West_currentMillis;
- //next State
- East_West_mState = East_West_RED;
- }
- }
- break;
- //*****************
- case East_West_REDdontWalk:
- {
- //next State
- East_West_mState = East_West_GREEN;
- if (East_West_currentMillis - East_West_REDdontWalkMillis > East_West_REDdontWalkInterval) {
- East_West_REDdontWalkFlag = DISABLED;
- digitalWrite(East_West_walk_red_LED, LEDon);
- Serial.print("East_West_Walk Red ON @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- }
- }
- break;
- } //END of switch/case
- } //END of East_West_checkTrafficLight()
- // c h e c k S w i t c h e s ( )
- //*****************************************^*****************************************
- void East_West_checkSwitches() {
- //***************************
- //Time to check the switches?
- if (East_West_currentMillis - East_West_switchMillis < East_West_switchDelay) {
- //it is not time yet
- return;
- }
- //restart the TIMER
- East_West_switchMillis = East_West_switchMillis + East_West_switchDelay;
- //*************************** >>>>-----> w a l k S w i t c h
- switch (East_West_checkThisSwitch(East_West_walkSwitch)) {
- case wasPUSHED:
- {
- }
- break;
- case wasRELEASED:
- {
- //only if the switch was held for East_West_minHoldTime or more seconds
- if (East_West_walkSwitch.pushedMillis >= East_West_minHoldTime) {
- East_West_walkFlag = ENABLED;
- Serial.print("East_West_Switch pushed @ ");
- Serial.println(East_West_currentMillis / 1000ul);
- }
- }
- break;
- }
- //***************************
- //Other switches go here
- //***************************
- } //END of East_West_checkSwitches()
- // c h e c k T h i s S w i t c h ( )
- //*****************************************^*****************************************
- //handles switch change in state, timing and filtering
- byte East_West_checkThisSwitch(East_West_defineSwitch& East_West_SWITCH) {
- //***************************
- byte East_West_currentState = digitalRead(East_West_SWITCH.pin);
- //has the East_West_SWITCH changed state ?
- if (East_West_SWITCH.lastState == East_West_currentState) {
- //no change, reset the filter counter
- East_West_SWITCH.counter = 0;
- //there was no change detected
- return noCHANGE;
- }
- //there was a East_West_SWITCH change in state
- East_West_SWITCH.counter++;
- //was this the 2nd sequential time we detected the change
- if (East_West_SWITCH.counter > 1) {
- //a valid East_West_SWITCH change was detected
- //get ready for the next 2 filter samples
- East_West_SWITCH.counter = 0;
- //update the East_West_SWITCH state
- East_West_SWITCH.lastState = East_West_currentState;
- //was the East_West_SWITCH pushed ?
- if (East_West_currentState == East_West_SWITCH.pushedLevel) {
- //record the time the East_West_SWITCH was pushed
- East_West_SWITCH.pushedMillis = East_West_currentMillis;
- return wasPUSHED;
- }
- else {
- //the East_West_SWITCH was released
- //save the time the East_West_SWITCH 'remained' pressed
- East_West_SWITCH.pushedMillis = East_West_currentMillis - East_West_SWITCH.pushedMillis;
- return wasRELEASED;
- }
- } //END of if(East_West_SWITCH.counter > 1)
- //there was no valid change detected
- return noCHANGE;
- } //END of East_West_checkThisSwitch()
- //*****************************************^*****************************************
Advertisement
Add Comment
Please, Sign In to add comment