stspringer

Traffic Light System USA With Pedestrian Button

Sep 20th, 2023 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.06 KB | None | 0 0
  1. /*
  2. //*****************************************^*****************************************
  3. PATH /home/john/Arduino/_My_Working_Arduino_Projects/My_Working_USA_Traffic_System_1/My_Working_USA_Traffic_System_1.ino
  4.  
  5. USA Traffic Light System Script, by John James Guarnieri, 09_15_2023
  6.  
  7. Credits: LarryD Arduino Forum https://forum.arduino.cc/t/help-with-adding-millis-and-replace-delay-100-in-this-sketch/655674/23
  8.  
  9. 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
  10.  
  11.  
  12.  
  13. 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
  14.  
  15. Also, I made an option to either have the Pedestrian walk RED led work automatically "which I am currently preferring",
  16. 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"
  17.  
  18. 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
  19.  
  20. // 1 - 39 ohm resistor for 2 red or yellow LED's in series
  21.  
  22. // 1 - 56 ohm resistor for 2 green LED in series
  23. //***********************************************************************************
  24.  
  25. // TrafficLightStateMachinePedSwitch.ino
  26. //
  27. // Version YY/MM/DD
  28. // 1.00 19/07/23 Running code (to be added, other direction coding)
  29. // 1.10 20/05/17 Added switch filtering, adjusted LED timing
  30. // 1.20 20/05/27 Added the red walk LED stuff
  31. // 1.30 20/05/28 Added red don't walk light delay
  32. //
  33. //*****************************************^*****************************************
  34. */
  35.  
  36. #define LEDon HIGH //BEGIN: My add/Edit
  37. #define LEDoff LOW
  38.  
  39. #define noCHANGE 0
  40. #define wasPUSHED 1
  41. #define wasRELEASED 2
  42.  
  43. #define ENABLED true
  44. #define DISABLED false
  45.  
  46. #define PUSHED LOW //+5V---Internal Pullup---PIN---[switch]---GND
  47.  
  48. //*****************************************^*****************************************
  49. //Switch 'structure' definition
  50. struct North_South_defineSwitch
  51.  
  52. {
  53. const byte pin; //physical pin being used
  54. const byte pushedLevel; //pin level when the switch is pushed
  55. byte lastState; //last state the switch was in
  56. byte counter; //switch filtering count
  57. unsigned long pushedMillis; //how long the switch was pressed for
  58. };
  59. //*****************************************^*****************************************
  60.  
  61. struct East_West_defineSwitch
  62.  
  63.  
  64. {
  65. const byte pin; //physical pin being used
  66. const byte pushedLevel; //pin level when the switch is pushed
  67. byte lastState; //last state the switch was in
  68. byte counter; //switch filtering count
  69. unsigned long pushedMillis; //how long the switch was pressed for
  70. };
  71. //*****************************************^*****************************************
  72.  
  73. North_South_defineSwitch North_South_walkSwitch = { 2, PUSHED, !PUSHED, 0, 0 }; //pedestrian walk switch
  74.  
  75. const byte North_South_redLED = 5; //+5V----[>|]----[220R]----GND ATTENTION! two RED LED's in series use a 33 ohm resistor
  76. const byte North_South_greenLED = 7;
  77. const byte North_South_yellowLED = 6;
  78. const byte North_South_walk_green_LED = 4;
  79. const byte North_South_walk_red_LED = 3;
  80.  
  81.  
  82. const byte heartbeatLED = 13;
  83. //East_West
  84. East_West_defineSwitch East_West_walkSwitch = { 2, PUSHED, !PUSHED, 0, 0 }; //pedestrian walk switch
  85.  
  86. const byte East_West_redLED = 8; //+5V----[>|]----[220R]----GND ATTENTION! two RED LED's in series use a 33 ohm resistor
  87. const byte East_West_greenLED = 10;
  88. const byte East_West_yellowLED = 9;
  89. const byte East_West_walk_green_LED = 12;
  90. const byte East_West_walk_red_LED = 11;
  91.  
  92.  
  93. const byte East_West_heartbeatLED = 13;
  94.  
  95. const byte ResetPin = 1; //use this to reset the microcontroller in code
  96.  
  97. //*****************************************^*****************************************
  98.  
  99. bool North_South_walkFlag = DISABLED;
  100. bool North_South_pedFlag = DISABLED;
  101. bool North_South_REDdontWalkFlag = DISABLED;
  102. bool North_South_flashingFlag = DISABLED;
  103.  
  104. bool East_West_walkFlag = DISABLED;
  105. bool East_West_pedFlag = DISABLED;
  106. bool East_West_REDdontWalkFlag = DISABLED;
  107. bool East_West_flashingFlag = DISABLED;
  108.  
  109.  
  110. //===================================================================================================================
  111. //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
  112. //===================================================================================================================
  113. //BE SURE TO UNCOMMENT THE CORRECT BOOL IF YOU DECIDE, TO MAKE THE CHANGE, FRON PEDESTRIAN BUTTON TO NO PEDESTRIAN BUTTON
  114.  
  115. bool Bypass_The_Pedestrian_Button = ENABLED; // CURRENTLY THE PREFERRED METHOD, Yes, Bypass the Pedestrian button "AUTOMATIC" Pedestrian button
  116. //bool Bypass_The_Pedestrian_Button = DISABLED; // No, don't bypass. Use the Pedestrian button MANUALLY
  117.  
  118. //===================================================================================================================
  119. //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
  120. //===================================================================================================================
  121.  
  122.  
  123. //===================================================================================================================
  124. //BEGIN: My add/Edit you decide if you want the Pedestrian Blinking LED to br green or red
  125. //===================================================================================================================
  126.  
  127. bool Pedestrian_Crossing_Blinking_LED_is_green = ENABLED; //yes use the green LED
  128. //bool Pedestrian_Crossing_Blinking_LED_is_green = DISABLED; //No, use the red LED
  129.  
  130. //===================================================================================================================
  131. //END: My add/Edit you decide if you want the Pedestrian Blinking LED to br Green or Red
  132. //===================================================================================================================
  133.  
  134.  
  135. unsigned long North_South_redMillis;
  136. unsigned long North_South_greenMillis;
  137. unsigned long North_South_yellowMillis;
  138.  
  139. unsigned long North_South_REDdontWalkMillis;
  140.  
  141. unsigned long North_South_currentMillis;
  142. unsigned long heartbeatMillis;
  143. unsigned long North_South_switchMillis;
  144. unsigned long North_South_pedMillis;
  145. unsigned long North_South_flashingMillis;
  146.  
  147. const unsigned long North_South_switchDelay = 25; //25ms
  148.  
  149.  
  150. unsigned long East_West_redMillis;
  151. unsigned long East_West_greenMillis;
  152. unsigned long East_West_yellowMillis;
  153.  
  154. unsigned long East_West_REDdontWalkMillis;
  155.  
  156. unsigned long East_West_currentMillis;
  157. unsigned long East_West_heartbeatMillis;
  158. unsigned long East_West_switchMillis;
  159. unsigned long East_West_pedMillis;
  160. unsigned long East_West_flashingMillis;
  161.  
  162. const unsigned long East_West_switchDelay = 25; //25ms
  163.  
  164. /*
  165. //********************************
  166. //BEGIN:define our light on times ORIGINAL
  167. //
  168.  
  169. //Change as needed
  170. //#define North_South_redInterval 60 * 1000ul //ON time in seconds
  171. //#define North_South_greenInterval 60 * 1000ul //ON time in seconds
  172. //#define North_South_yellowInterval 12 * 1000ul //ON time in seconds
  173.  
  174. //#define North_South_flashingDelay 40 * 1000ul //Time at which the flashing starts
  175. //#define North_South_pedInterval 50 * 1000ul //Time at which the walk light goes off
  176.  
  177. //#define North_South_REDdontWalkInterval 3 * 1000ul //wait 3 seconds before the "Don't Walk" comes on
  178.  
  179. #define North_South_flashingInterval 200ul
  180. #define North_South_minHoldTime 500ul //switch must be held for at least this time
  181.  
  182. //*************************
  183. //For Testing
  184. //#define North_South_redInterval 10 * 1000ul //ON time in seconds ORIGINAL
  185. //#define North_South_greenInterval 10 * 1000ul //ON time in seconds ORIGINAL
  186. //#define North_South_yellowInterval 4 * 1000ul //ON time in seconds ORIGINAL
  187.  
  188. #define North_South_redInterval 5 * 4000ul //ON time in seconds ORIGINAL
  189. #define North_South_greenInterval 5 * 4000ul //ON time in seconds ORIGINAL
  190. #define North_South_yellowInterval 4800ul //ON time in seconds ORIGINAL
  191.  
  192. //#define North_South_flashingDelay 3 * 1000ul //Time at which the flashing starts ORIGINAL
  193. #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
  194.  
  195. //#define North_South_pedInterval 5 * 1000ul //Time at which the walk light goes off ORIGINAL
  196. #define North_South_pedInterval 25 * 1000ul //Time at which the walk light turns off "this is the blink duration" TESTING HERE
  197.  
  198. //#define North_South_REDdontWalkInterval 3 * 1000ul //wait 3 seconds before the "Don't Walk" comes on ORIGINAL
  199. #define North_South_REDdontWalkInterval 250ul //wait 3 seconds before the "Don't Walk" comes on TESTING HERE
  200.  
  201. //*************************
  202. //END:define our light on times ORIGINAL
  203. */
  204.  
  205.  
  206. //===================================================================================================================
  207. //BEGIN: My add/Edit North_South
  208. //===================================================================================================================
  209. #define North_South_flashingInterval 200ul
  210. #define North_South_minHoldTime 500ul //switch must be held for at least this time
  211.  
  212. //*************************
  213. //#define North_South_redInterval 5 * 6800ul //ON time in seconds ORIGINAL
  214.  
  215. #define North_South_redInterval 5 * 6020ul //ON time in seconds TESTING WORKING GREAT
  216.  
  217. //#define North_South_greenInterval 5 * 5800ul //ON time in seconds ORIGINAL
  218.  
  219. #define North_South_greenInterval 5 * 5000ul //ON time in seconds TESTING WORKING GREAT
  220.  
  221. #define North_South_yellowInterval 4800ul //ON time in seconds
  222.  
  223. //#define North_South_flashingDelay 3 * 1000ul //Time at which the flashing starts ORIGINAL
  224. #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
  225.  
  226. //#define North_South_pedInterval 5 * 1000ul //Time at which the walk light goes off ORIGINAL
  227. #define North_South_pedInterval 25 * 1000ul //Time at which the walk light turns off "this is the blink duration" TESTING HERE
  228.  
  229. //#define North_South_REDdontWalkInterval 3 * 1000ul //wait 3 seconds before the "Don't Walk" comes on ORIGINAL
  230. #define North_South_REDdontWalkInterval 250ul //wait 3 seconds before the "Don't Walk" comes on TESTING HERE
  231.  
  232.  
  233. //===================================================================================================================
  234. //BEGIN: My add/Edit East_West
  235. //===================================================================================================================
  236. //#define East_West_redInterval 5 * 6800ul //ON time in seconds ORIGINAL
  237.  
  238. #define East_West_redInterval 5 * 6020ul //ON time in seconds TESTING WORKING GREAT
  239.  
  240. //#define East_West_greenInterval 5 * 5800ul //ON time in seconds ORIGINAL
  241. #define East_West_greenInterval 5 * 5000ul //ON time in seconds TESTING WORKING GREAT
  242.  
  243. #define East_West_yellowInterval 4800ul //ON time in seconds
  244.  
  245. //#define East_West_flashingDelay 3 * 1000ul //Time at which the flashing starts ORIGINAL
  246. #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
  247.  
  248. //#define East_West_pedInterval 5 * 1000ul //Time at which the walk light goes off ORIGINAL
  249. #define East_West_pedInterval 25 * 1000ul //Time at which the walk light turns off "this is the blink duration" TESTING HERE
  250.  
  251. //#define East_West_REDdontWalkInterval 3 * 1000ul //wait 3 seconds before the "Don't Walk" comes on ORIGINAL
  252. #define East_West_REDdontWalkInterval 250ul //wait 3 seconds before the "Don't Walk" comes on TESTING HERE
  253.  
  254. #define East_West_flashingInterval 200ul
  255. #define East_West_minHoldTime 500ul //Pedestrian switch must be held for at least this time
  256. //===================================================================================================================
  257. //END: My add/Edit
  258. //===================================================================================================================
  259.  
  260. //*************************
  261. //states in our machine
  262. enum North_South_STATES { North_South_STARTUP,
  263. North_South_RED,
  264. North_South_GREEN,
  265. North_South_YELLOW,
  266. North_South_CROSSING,
  267. North_South_REDdontWalk };
  268. North_South_STATES North_South_mState = North_South_STARTUP;
  269.  
  270.  
  271. //states in our machine
  272.  
  273. enum East_West_STATES { East_West_STARTUP,
  274. East_West_GREEN,
  275. East_West_YELLOW,
  276. East_West_RED,
  277. East_West_CROSSING,
  278. East_West_REDdontWalk };
  279. East_West_STATES East_West_mState = East_West_STARTUP;
  280.  
  281.  
  282.  
  283. // s e t u p ( )
  284. //*****************************************^*****************************************
  285. void setup() {
  286. Serial.begin(115200);
  287.  
  288. pinMode(North_South_redLED, OUTPUT);
  289. pinMode(North_South_greenLED, OUTPUT);
  290. pinMode(North_South_yellowLED, OUTPUT);
  291. pinMode(North_South_walk_red_LED, OUTPUT);
  292. pinMode(North_South_walk_green_LED, OUTPUT);
  293.  
  294. pinMode(heartbeatLED, OUTPUT);
  295.  
  296. pinMode(ResetPin, OUTPUT);
  297.  
  298. pinMode(ResetPin, LOW); //off
  299.  
  300. pinMode(North_South_walkSwitch.pin, INPUT_PULLUP);
  301. North_South_walkSwitch.lastState = digitalRead(North_South_walkSwitch.pin);
  302.  
  303.  
  304. pinMode(East_West_redLED, OUTPUT);
  305. pinMode(East_West_greenLED, OUTPUT);
  306. pinMode(East_West_yellowLED, OUTPUT);
  307. pinMode(East_West_walk_red_LED, OUTPUT);
  308. pinMode(East_West_walk_green_LED, OUTPUT);
  309.  
  310. pinMode(heartbeatLED, OUTPUT);
  311.  
  312. pinMode(East_West_walkSwitch.pin, INPUT_PULLUP);
  313. East_West_walkSwitch.lastState = digitalRead(East_West_walkSwitch.pin);
  314.  
  315.  
  316. //pinMode (otherSwitch.pin, INPUT_PULLUP);
  317. //otherSwitch.lastState = digitalRead(otherSwitch.pin);
  318.  
  319. } //END of setup()
  320.  
  321. // l o o p ( )
  322. //*****************************************^*****************************************
  323. void loop() {
  324. //save current time
  325. North_South_currentMillis = millis();
  326.  
  327. //********************************
  328. //to help show if there is any blocking code, this LED toggles every 1000ms
  329. if (North_South_currentMillis - heartbeatMillis >= 1000) {
  330. Serial.print("Time = ");
  331. Serial.println(North_South_currentMillis / 1000ul);
  332.  
  333. //start this TIMER
  334. heartbeatMillis = North_South_currentMillis;
  335.  
  336. digitalWrite(heartbeatLED, !digitalRead(heartbeatLED)); //built in LED pin 13
  337. }
  338.  
  339. //********************************
  340. North_South_checkSwitches();
  341.  
  342. //********************************
  343. North_South_checkTrafficLight();
  344.  
  345. //********************************
  346. // Other non blocking code goes here
  347. //********************************
  348. //East_West_ here
  349.  
  350. East_West_currentMillis = millis();
  351.  
  352. //********************************
  353. //to help show if there is any blocking code, this LED toggles every 1000ms
  354. if (East_West_currentMillis - heartbeatMillis >= 1000) {
  355. Serial.print("Time = ");
  356. Serial.println(East_West_currentMillis / 1000ul);
  357.  
  358. //start this TIMER
  359. heartbeatMillis = East_West_currentMillis;
  360.  
  361. digitalWrite(heartbeatLED, !digitalRead(heartbeatLED)); //built in LED pin 13
  362. }
  363.  
  364. //********************************
  365. East_West_checkSwitches();
  366.  
  367. //********************************
  368. East_West_checkTrafficLight();
  369.  
  370. //********************************
  371. // Other non blocking code goes here
  372. //********************************
  373. } //END of loop()
  374.  
  375.  
  376. //*****************************************^*****************************************
  377. //BEGIN: TAB 1 NORTH_SOUTH TRAFFIC lIGHTS
  378. //*****************************************^*****************************************
  379.  
  380.  
  381. // c h e c k T r a f f i c L i g h t ( )
  382. //*****************************************^*****************************************
  383. void North_South_checkTrafficLight() {
  384. switch (North_South_mState) {
  385. //*****************
  386. case North_South_STARTUP:
  387. {
  388. digitalWrite(North_South_redLED, LEDon);
  389. digitalWrite(North_South_greenLED, LEDoff);
  390. digitalWrite(North_South_yellowLED, LEDoff);
  391. digitalWrite(North_South_walk_red_LED, LEDon);
  392. digitalWrite(North_South_walk_green_LED, LEDoff);
  393.  
  394. //start the TIMER
  395. North_South_redMillis = North_South_currentMillis;
  396.  
  397. //next State
  398. //ORIGINAL HERE
  399. North_South_mState = North_South_RED;
  400.  
  401. //TESTING HERE
  402. //North_South_pedFlag = ENABLED;
  403. //North_South_mState = North_South_GREEN;
  404.  
  405. Serial.print("North_South_RED Traffic ON @ ");
  406. Serial.println(North_South_currentMillis / 1000ul);
  407. }
  408. break;
  409.  
  410. //*****************
  411. case North_South_RED:
  412. {
  413. if (North_South_currentMillis - North_South_redMillis >= North_South_redInterval) {
  414. Serial.print("North_South_Red Traffic OFF @ ");
  415. Serial.println(North_South_currentMillis / 1000ul);
  416.  
  417. digitalWrite(North_South_redLED, LEDoff);
  418. digitalWrite(North_South_greenLED, LEDon);
  419. //start the TIMER
  420. North_South_greenMillis = North_South_currentMillis;
  421.  
  422. Serial.print("North_South_Green Traffic ON @ ");
  423. Serial.println(North_South_currentMillis / 1000ul);
  424.  
  425. //===================================================================================================================
  426. //BEGIN: My add/Edit "North_South_walkFlag" forced to ENABLED
  427. //===================================================================================================================
  428. if (Bypass_The_Pedestrian_Button) //This flag determins "AUTOMATIC" if "true", or "MANUAL" Pedestrian button if "false"
  429. {
  430. North_South_walkFlag = ENABLED;
  431. }
  432. //===================================================================================================================
  433. //END: My add/Edit "North_South_walkFlag" forced to ENABLED
  434. //===================================================================================================================
  435.  
  436.  
  437. if (North_South_walkFlag == ENABLED) {
  438. digitalWrite(North_South_walk_red_LED, LEDoff);
  439. digitalWrite(North_South_walk_green_LED, LEDon);
  440.  
  441. //start the TIMER
  442. North_South_pedMillis = North_South_currentMillis;
  443.  
  444. North_South_walkFlag = DISABLED;
  445. North_South_pedFlag = ENABLED;
  446.  
  447. Serial.print("North_South_Walk Green ON @ ");
  448. Serial.println(North_South_currentMillis / 1000ul);
  449. }
  450.  
  451. //next State
  452. North_South_mState = North_South_GREEN;
  453. }
  454. }
  455. break;
  456.  
  457. //*****************
  458.  
  459.  
  460. break;
  461. //*****************
  462. case North_South_GREEN:
  463. {
  464. if (North_South_pedFlag == ENABLED) {
  465. North_South_mState = North_South_CROSSING;
  466. }
  467.  
  468. if (North_South_REDdontWalkFlag == ENABLED) {
  469. North_South_mState = North_South_REDdontWalk;
  470. }
  471.  
  472. if (North_South_REDdontWalkFlag == DISABLED && North_South_pedFlag == DISABLED && North_South_currentMillis - North_South_greenMillis >= North_South_greenInterval) {
  473. Serial.print("North_South_Green Traffic OFF @ ");
  474. Serial.println(North_South_currentMillis / 1000ul);
  475.  
  476. digitalWrite(North_South_greenLED, LEDoff);
  477. digitalWrite(North_South_yellowLED, LEDon);
  478.  
  479. //start the TIMER
  480. North_South_yellowMillis = North_South_currentMillis;
  481. //next State
  482. North_South_mState = North_South_YELLOW;
  483.  
  484. Serial.print("North_South_Yellow Traffic ON @ ");
  485. Serial.println(North_South_currentMillis / 1000ul);
  486. }
  487. }
  488. break;
  489.  
  490. //*****************
  491. case North_South_CROSSING:
  492. {
  493. //next State
  494. North_South_mState = North_South_GREEN;
  495.  
  496. //time to toggle LED ?
  497. if (North_South_flashingFlag == ENABLED && North_South_currentMillis - North_South_flashingMillis >= North_South_flashingInterval) {
  498. //start the TIMER
  499. North_South_flashingMillis = North_South_currentMillis;
  500.  
  501. //toggle LED, blink the Green LED, that crossing time is getting short, as a warning to the Pedestrian
  502.  
  503. if (Pedestrian_Crossing_Blinking_LED_is_green)
  504. {
  505. digitalWrite(North_South_walk_green_LED, !digitalRead(North_South_walk_green_LED));
  506. }
  507. else //The bool in Declarations, "Pedestrian_Crossing_Blinking_LED_is_green", is DISABLED
  508. {
  509. //blink the Red LED, that crossing time is getting short, as a warning to the Pedestrian
  510. digitalWrite(North_South_walk_green_LED, LEDoff);
  511. digitalWrite(North_South_walk_red_LED, !digitalRead(North_South_walk_red_LED));
  512. }
  513. }
  514.  
  515. //is it time to start flashing the walk light ?
  516. if (North_South_flashingFlag == DISABLED && North_South_currentMillis - North_South_pedMillis >= North_South_flashingDelay) {
  517. North_South_flashingFlag = ENABLED;
  518.  
  519. //start the TIMER
  520. North_South_flashingMillis = North_South_currentMillis;
  521.  
  522. Serial.print("North_South_Walk Green Flashing @ ");
  523. Serial.println(North_South_currentMillis / 1000ul);
  524. }
  525.  
  526. //has walking time expired ?
  527. if (North_South_currentMillis - North_South_pedMillis >= North_South_pedInterval) {
  528. North_South_pedFlag = DISABLED;
  529. North_South_flashingFlag = DISABLED;
  530.  
  531. digitalWrite(North_South_walk_green_LED, LEDoff);
  532.  
  533. Serial.print("North_South_Walk Green OFF 2 ");
  534. Serial.println(North_South_currentMillis / 1000ul);
  535.  
  536. North_South_REDdontWalkFlag = ENABLED;
  537. //start TIMER
  538. North_South_REDdontWalkMillis = North_South_currentMillis;
  539. //next State
  540. North_South_mState = North_South_REDdontWalk;
  541. }
  542. }
  543. break;
  544.  
  545. //*****************
  546. case North_South_YELLOW:
  547. {
  548. if (North_South_currentMillis - North_South_yellowMillis >= North_South_yellowInterval) {
  549. Serial.print("North_South_Yellow Traffic OFF @ ");
  550. Serial.println(North_South_currentMillis / 1000ul);
  551.  
  552. Serial.print("North_South_Red Traffic ON @ ");
  553. Serial.println(North_South_currentMillis / 1000ul);
  554.  
  555. digitalWrite(North_South_yellowLED, LEDoff);
  556. digitalWrite(North_South_redLED, LEDon);
  557.  
  558. //start the TIMER
  559. North_South_redMillis = North_South_currentMillis;
  560. //next State
  561. North_South_mState = North_South_RED;
  562. }
  563. }
  564. break;
  565.  
  566. //*****************
  567. case North_South_REDdontWalk:
  568. {
  569. //next State
  570. North_South_mState = North_South_GREEN;
  571.  
  572. if (North_South_currentMillis - North_South_REDdontWalkMillis > North_South_REDdontWalkInterval) {
  573. North_South_REDdontWalkFlag = DISABLED;
  574.  
  575. digitalWrite(North_South_walk_red_LED, LEDon);
  576.  
  577. Serial.print("North_South_Walk Red ON @ ");
  578. Serial.println(North_South_currentMillis / 1000ul);
  579. }
  580. }
  581. break;
  582.  
  583.  
  584. } //END of switch/case
  585.  
  586. } //END of North_South_checkTrafficLight()
  587.  
  588.  
  589. // c h e c k S w i t c h e s ( )
  590. //*****************************************^*****************************************
  591. void North_South_checkSwitches() {
  592. //***************************
  593. //Time to check the switches?
  594. if (North_South_currentMillis - North_South_switchMillis < North_South_switchDelay) {
  595. //it is not time yet
  596. return;
  597. }
  598.  
  599. //restart the TIMER
  600. North_South_switchMillis = North_South_switchMillis + North_South_switchDelay;
  601.  
  602. //*************************** >>>>-----> w a l k S w i t c h
  603. switch (North_South_checkThisSwitch(North_South_walkSwitch)) {
  604. case wasPUSHED:
  605. {
  606. }
  607. break;
  608.  
  609. case wasRELEASED:
  610. {
  611. //only if the switch was held for North_South_minHoldTime or more seconds
  612. if (North_South_walkSwitch.pushedMillis >= North_South_minHoldTime) {
  613. North_South_walkFlag = ENABLED;
  614.  
  615. Serial.print("North_South_Switch pushed @ ");
  616. Serial.println(North_South_currentMillis / 1000ul);
  617. }
  618. }
  619. break;
  620. }
  621.  
  622. //***************************
  623. //Other switches go here
  624. //***************************
  625.  
  626. } //END of North_South_checkSwitches()
  627.  
  628. // c h e c k T h i s S w i t c h ( )
  629. //*****************************************^*****************************************
  630. //handles switch change in state, timing and filtering
  631. byte North_South_checkThisSwitch(North_South_defineSwitch& North_South_SWITCH) {
  632. //***************************
  633. byte North_South_currentState = digitalRead(North_South_SWITCH.pin);
  634.  
  635. //has the North_South_SWITCH changed state ?
  636. if (North_South_SWITCH.lastState == North_South_currentState) {
  637. //no change, reset the filter counter
  638. North_South_SWITCH.counter = 0;
  639.  
  640. //there was no change detected
  641. return noCHANGE;
  642. }
  643.  
  644. //there was a North_South_SWITCH change in state
  645. North_South_SWITCH.counter++;
  646.  
  647. //was this the 2nd sequential time we detected the change
  648. if (North_South_SWITCH.counter > 1) {
  649. //a valid North_South_SWITCH change was detected
  650. //get ready for the next 2 filter samples
  651. North_South_SWITCH.counter = 0;
  652.  
  653. //update the North_South_SWITCH state
  654. North_South_SWITCH.lastState = North_South_currentState;
  655.  
  656. //was the North_South_SWITCH pushed ?
  657. if (North_South_currentState == North_South_SWITCH.pushedLevel) {
  658. //record the time the North_South_SWITCH was pushed
  659. North_South_SWITCH.pushedMillis = North_South_currentMillis;
  660.  
  661. return wasPUSHED;
  662. }
  663.  
  664. else {
  665. //the North_South_SWITCH was released
  666. //save the time the North_South_SWITCH 'remained' pressed
  667. North_South_SWITCH.pushedMillis = North_South_currentMillis - North_South_SWITCH.pushedMillis;
  668.  
  669. return wasRELEASED;
  670. }
  671.  
  672. } //END of if(North_South_SWITCH.counter > 1)
  673.  
  674. //there was no valid change detected
  675. return noCHANGE;
  676.  
  677. } //END of North_South_checkThisSwitch()
  678.  
  679. //*****************************************^*****************************************
  680.  
  681. //*****************************************^*****************************************
  682. //*****************************************^*****************************************
  683. //END: TQB 1 NORTH_SOUTH TRAFFIC lIGHTS
  684. //*****************************************^*****************************************
  685.  
  686.  
  687.  
  688. //*****************************************^*****************************************
  689. //BEGIN: TAB 2 EAST_WEST TRAFFIC lIGHTS
  690. //*****************************************^*****************************************
  691.  
  692. // c h e c k T r a f f i c L i g h t ( )
  693. //*****************************************^*****************************************
  694. void East_West_checkTrafficLight() {
  695. switch (East_West_mState) {
  696. //*****************
  697. case East_West_STARTUP:
  698. {
  699. //ORIGINAL HERE
  700. //digitalWrite(East_West_redLED, LEDon);
  701. //digitalWrite(East_West_greenLED, LEDoff);
  702. //digitalWrite(East_West_yellowLED, LEDoff);
  703. //digitalWrite(East_West_walk_red_LED, LEDon);
  704. //digitalWrite(East_West_walk_green_LED, LEDoff);
  705.  
  706.  
  707. //TESTING HERE
  708.  
  709. digitalWrite(East_West_redLED, LEDoff);
  710. digitalWrite(East_West_greenLED, LEDon);
  711. digitalWrite(East_West_yellowLED, LEDoff);
  712. digitalWrite(East_West_walk_red_LED, LEDoff);
  713. digitalWrite(East_West_walk_green_LED, LEDon);
  714.  
  715. //start the TIMER
  716. East_West_redMillis = East_West_currentMillis;
  717.  
  718. //next State
  719. //ORIGINAL HERE
  720. //East_West_mState = East_West_RED;
  721.  
  722. //TESTING HERE
  723. East_West_pedFlag = ENABLED;
  724. East_West_mState = East_West_GREEN;
  725. //East_West_mState = East_West_CROSSING;
  726.  
  727.  
  728. Serial.print("East_West_RED Traffic ON @ ");
  729. Serial.println(East_West_currentMillis / 1000ul);
  730. }
  731. break;
  732.  
  733. //*****************
  734. case East_West_RED:
  735. {
  736. if (East_West_currentMillis - East_West_redMillis >= East_West_redInterval) {
  737. Serial.print("East_West_Red Traffic OFF @ ");
  738. Serial.println(East_West_currentMillis / 1000ul);
  739.  
  740. digitalWrite(East_West_redLED, LEDoff);
  741. digitalWrite(East_West_greenLED, LEDon);
  742.  
  743. //TESTING HERE
  744. //digitalWrite(East_West_redLED, LEDon);
  745. //digitalWrite(East_West_greenLED, LEDoff);
  746.  
  747. //start the TIMER
  748. East_West_greenMillis = East_West_currentMillis;
  749.  
  750. Serial.print("East_West_Green Traffic ON @ ");
  751. Serial.println(East_West_currentMillis / 1000ul);
  752.  
  753. //===================================================================================================================
  754. //BEGIN: My add/Edit "East_West_walkFlag" forced to ENABLED
  755. //===================================================================================================================
  756. if (Bypass_The_Pedestrian_Button) //This flag determins "AUTOMATIC" if "true", or "MANUAL" Pedestrian button if "false"
  757. {
  758. East_West_walkFlag = ENABLED;
  759. }
  760. //===================================================================================================================
  761. //END: My add/Edit "East_West_walkFlag" forced to ENABLED
  762. //===================================================================================================================
  763.  
  764. if (East_West_walkFlag == ENABLED) {
  765. digitalWrite(East_West_walk_red_LED, LEDoff);
  766. digitalWrite(East_West_walk_green_LED, LEDon);
  767.  
  768. //TESTING HERE
  769. //digitalWrite(East_West_walk_red_LED, LEDon);
  770. //digitalWrite(East_West_walk_green_LED, LEDoff);
  771.  
  772. //start the TIMER
  773. East_West_pedMillis = East_West_currentMillis;
  774.  
  775. East_West_walkFlag = DISABLED;
  776. East_West_pedFlag = ENABLED;
  777.  
  778. Serial.print("East_West_Walk Green ON @ ");
  779. Serial.println(East_West_currentMillis / 1000ul);
  780. }
  781.  
  782. //next State
  783. East_West_mState = East_West_GREEN;
  784. }
  785. }
  786. break;
  787.  
  788. //*****************
  789.  
  790.  
  791. break;
  792. //*****************
  793. case East_West_GREEN:
  794. {
  795. if (East_West_pedFlag == ENABLED) {
  796. East_West_mState = East_West_CROSSING;
  797. }
  798.  
  799. if (East_West_REDdontWalkFlag == ENABLED) {
  800. East_West_mState = East_West_REDdontWalk;
  801. }
  802.  
  803. if (East_West_REDdontWalkFlag == DISABLED && East_West_pedFlag == DISABLED && East_West_currentMillis - East_West_greenMillis >= East_West_greenInterval) {
  804. Serial.print("East_West_Green Traffic OFF @ ");
  805. Serial.println(East_West_currentMillis / 1000ul);
  806.  
  807. digitalWrite(East_West_greenLED, LEDoff);
  808. digitalWrite(East_West_yellowLED, LEDon);
  809.  
  810. //TESTING HERE
  811. //digitalWrite(East_West_greenLED, LEDoff);
  812. //digitalWrite(East_West_yellowLED, LEDon);
  813.  
  814. //start the TIMER
  815. East_West_yellowMillis = East_West_currentMillis;
  816. //next State
  817. East_West_mState = East_West_YELLOW;
  818.  
  819. Serial.print("East_West_Yellow Traffic ON @ ");
  820. Serial.println(East_West_currentMillis / 1000ul);
  821. }
  822. }
  823. break;
  824.  
  825. //*****************
  826. case East_West_CROSSING:
  827. {
  828. //next State
  829. East_West_mState = East_West_GREEN;
  830.  
  831. //time to toggle LED ?
  832. if (East_West_flashingFlag == ENABLED && East_West_currentMillis - East_West_flashingMillis >= East_West_flashingInterval) {
  833. //start the TIMER
  834. East_West_flashingMillis = East_West_currentMillis;
  835.  
  836. //toggle LED, blink the Green LED, that crossing time is getting short, as a warning to the Pedestrian
  837. if (Pedestrian_Crossing_Blinking_LED_is_green)
  838. {
  839. digitalWrite(East_West_walk_green_LED, !digitalRead(East_West_walk_green_LED));
  840. }
  841. else //The bool in Declarations, "Pedestrian_Crossing_Blinking_LED_is_green", is DISABLED
  842. {
  843. //blink the Red LED, that crossing time is getting short, as a warning to the Pedestrian
  844. digitalWrite(East_West_walk_green_LED, LEDoff);
  845. digitalWrite(East_West_walk_red_LED, !digitalRead(East_West_walk_red_LED));
  846. }
  847. }
  848.  
  849. //is it time to start flashing the walk light ?
  850. if (East_West_flashingFlag == DISABLED && East_West_currentMillis - East_West_pedMillis >= East_West_flashingDelay) {
  851. East_West_flashingFlag = ENABLED;
  852.  
  853. //start the TIMER
  854. East_West_flashingMillis = East_West_currentMillis;
  855.  
  856. Serial.print("East_West_Walk Green Flashing @ ");
  857. Serial.println(East_West_currentMillis / 1000ul);
  858. }
  859.  
  860. //has walking time expired ?
  861. if (East_West_currentMillis - East_West_pedMillis >= East_West_pedInterval) {
  862. East_West_pedFlag = DISABLED;
  863. East_West_flashingFlag = DISABLED;
  864.  
  865. digitalWrite(East_West_walk_green_LED, LEDoff);
  866.  
  867. Serial.print("East_West_Walk Green OFF 2 ");
  868. Serial.println(East_West_currentMillis / 1000ul);
  869.  
  870. East_West_REDdontWalkFlag = ENABLED;
  871. //start TIMER
  872. East_West_REDdontWalkMillis = East_West_currentMillis;
  873. //next State
  874. East_West_mState = East_West_REDdontWalk;
  875. }
  876. }
  877. break;
  878.  
  879. //*****************
  880. case East_West_YELLOW:
  881. {
  882. if (East_West_currentMillis - East_West_yellowMillis >= East_West_yellowInterval) {
  883. Serial.print("East_West_Yellow Traffic OFF @ ");
  884. Serial.println(East_West_currentMillis / 1000ul);
  885.  
  886. Serial.print("East_West_Red Traffic ON @ ");
  887. Serial.println(East_West_currentMillis / 1000ul);
  888.  
  889.  
  890. digitalWrite(East_West_yellowLED, LEDoff);
  891. digitalWrite(East_West_redLED, LEDon);
  892.  
  893. //testing
  894. //digitalWrite(East_West_yellowLED, LEDon);
  895. //digitalWrite(East_West_redLED, LEDoff);
  896.  
  897. //start the TIMER
  898. East_West_redMillis = East_West_currentMillis;
  899. //next State
  900. East_West_mState = East_West_RED;
  901. }
  902. }
  903. break;
  904.  
  905. //*****************
  906. case East_West_REDdontWalk:
  907. {
  908. //next State
  909. East_West_mState = East_West_GREEN;
  910.  
  911. if (East_West_currentMillis - East_West_REDdontWalkMillis > East_West_REDdontWalkInterval) {
  912. East_West_REDdontWalkFlag = DISABLED;
  913.  
  914. digitalWrite(East_West_walk_red_LED, LEDon);
  915.  
  916. Serial.print("East_West_Walk Red ON @ ");
  917. Serial.println(East_West_currentMillis / 1000ul);
  918. }
  919. }
  920. break;
  921.  
  922.  
  923. } //END of switch/case
  924.  
  925. } //END of East_West_checkTrafficLight()
  926.  
  927.  
  928. // c h e c k S w i t c h e s ( )
  929. //*****************************************^*****************************************
  930. void East_West_checkSwitches() {
  931. //***************************
  932. //Time to check the switches?
  933. if (East_West_currentMillis - East_West_switchMillis < East_West_switchDelay) {
  934. //it is not time yet
  935. return;
  936. }
  937.  
  938. //restart the TIMER
  939. East_West_switchMillis = East_West_switchMillis + East_West_switchDelay;
  940.  
  941. //*************************** >>>>-----> w a l k S w i t c h
  942. switch (East_West_checkThisSwitch(East_West_walkSwitch)) {
  943. case wasPUSHED:
  944. {
  945. }
  946. break;
  947.  
  948. case wasRELEASED:
  949. {
  950. //only if the switch was held for East_West_minHoldTime or more seconds
  951. if (East_West_walkSwitch.pushedMillis >= East_West_minHoldTime) {
  952. East_West_walkFlag = ENABLED;
  953.  
  954. Serial.print("East_West_Switch pushed @ ");
  955. Serial.println(East_West_currentMillis / 1000ul);
  956. }
  957. }
  958. break;
  959. }
  960.  
  961. //***************************
  962. //Other switches go here
  963. //***************************
  964.  
  965. } //END of East_West_checkSwitches()
  966.  
  967. // c h e c k T h i s S w i t c h ( )
  968. //*****************************************^*****************************************
  969. //handles switch change in state, timing and filtering
  970. byte East_West_checkThisSwitch(East_West_defineSwitch& East_West_SWITCH) {
  971. //***************************
  972. byte East_West_currentState = digitalRead(East_West_SWITCH.pin);
  973.  
  974. //has the East_West_SWITCH changed state ?
  975. if (East_West_SWITCH.lastState == East_West_currentState) {
  976. //no change, reset the filter counter
  977. East_West_SWITCH.counter = 0;
  978.  
  979. //there was no change detected
  980. return noCHANGE;
  981. }
  982.  
  983. //there was a East_West_SWITCH change in state
  984. East_West_SWITCH.counter++;
  985.  
  986. //was this the 2nd sequential time we detected the change
  987. if (East_West_SWITCH.counter > 1) {
  988. //a valid East_West_SWITCH change was detected
  989. //get ready for the next 2 filter samples
  990. East_West_SWITCH.counter = 0;
  991.  
  992. //update the East_West_SWITCH state
  993. East_West_SWITCH.lastState = East_West_currentState;
  994.  
  995. //was the East_West_SWITCH pushed ?
  996. if (East_West_currentState == East_West_SWITCH.pushedLevel) {
  997. //record the time the East_West_SWITCH was pushed
  998. East_West_SWITCH.pushedMillis = East_West_currentMillis;
  999.  
  1000. return wasPUSHED;
  1001. }
  1002.  
  1003. else {
  1004. //the East_West_SWITCH was released
  1005. //save the time the East_West_SWITCH 'remained' pressed
  1006. East_West_SWITCH.pushedMillis = East_West_currentMillis - East_West_SWITCH.pushedMillis;
  1007.  
  1008. return wasRELEASED;
  1009. }
  1010.  
  1011. } //END of if(East_West_SWITCH.counter > 1)
  1012.  
  1013. //there was no valid change detected
  1014. return noCHANGE;
  1015.  
  1016. } //END of East_West_checkThisSwitch()
  1017.  
  1018. //*****************************************^*****************************************
  1019.  
  1020.  
  1021.  
Advertisement
Add Comment
Please, Sign In to add comment