Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // --- ATTRIBUTES --- //
  3.  
  4. int subStateMachineTraffic;
  5. boolean subStateMachineTrafficBusy = false; // Is the substateBusy?
  6. boolean canBeChangedSubTraffic;
  7.  
  8. const int TRAFFIC_OPEN = 0;
  9. const int TRAFFIC_CLOSING = 1;
  10. const int TRAFFIC_CLOSED = 2;
  11.  
  12. const int INTERVAL_TRAFFIC_GREEN = 8000;
  13. const int INTERVAL_TRAFFIC_ORANGE = 3000;
  14.  
  15. // --- SETUP --- //
  16.  
  17. void subStateMachineTrafficSetup(int trafficLight) {
  18.   subStateMachineTrafficBusy = true;
  19.   canBeChangedSubTraffic = false;
  20.  
  21.   subStateMachineTraffic = TRAFFIC_OPEN;
  22.   TRAFFIC_OPEN_entry(trafficLight);
  23. }
  24.  
  25. // --- FUNCTIONS --- //
  26.  
  27. void subStateMachineTrafficLoop(int trafficLight) {
  28.   switch (subStateMachineTraffic)  {
  29.     case TRAFFIC_OPEN :
  30.       TRAFFIC_OPEN_do();
  31.       if (canBeChangedSubTraffic) {
  32.         TRAFFIC_OPEN_exit();
  33.         subStateMachineTraffic = TRAFFIC_CLOSING;
  34.         TRAFFIC_CLOSING_entry(trafficLight);
  35.       }
  36.       break;
  37.     case TRAFFIC_CLOSING :
  38.       TRAFFIC_CLOSING_do();
  39.       Serial.println("two");
  40.       if (canBeChangedSubTraffic) {
  41.         TRAFFIC_CLOSING_exit();
  42.         subStateMachineTraffic = TRAFFIC_CLOSED;
  43.         TRAFFIC_CLOSED_entry(trafficLight);
  44.       }
  45.       break;
  46.     case TRAFFIC_CLOSED :
  47.       TRAFFIC_CLOSED_do();
  48.       Serial.println("three");
  49.       if (canBeChangedSubTraffic) {
  50.         TRAFFIC_CLOSED_exit();
  51.       }
  52.       break;
  53.   }
  54. }
  55.  
  56. // --- SETTERS --- //
  57.  
  58.  
  59. // --- ENTRY | DO | EXIT --- //
  60.  
  61. // || -- TRAFFIC OPEN -- || //
  62. void TRAFFIC_OPEN_entry( int trafficLight) {
  63.   turnTrafficLightOn(trafficLight, GREEN); // Changes traffic light to green
  64. }
  65. void TRAFFIC_OPEN_do() {
  66.   buzzerTicking(SLOW); // Buzzer ticks slowly
  67.   myfnUpdateDisplay(getStopSign()); // Displays a stop sign on the 7-segment display
  68.   int currentMillis = 0;
  69.   currentMillis = millis();
  70.   if (currentMillis > INTERVAL_TRAFFIC_GREEN) {
  71.     canBeChangedSubTraffic = true;
  72.   }
  73. }
  74. void TRAFFIC_OPEN_exit() {
  75.   canBeChangedSubTraffic = false;
  76. }
  77.  
  78. // || -- TRAFFIC CLOSING -- || //
  79. void TRAFFIC_CLOSING_entry(int trafficLight) {
  80.   turnTrafficLightOn(trafficLight, ORANGE); // Changes traffic light to orange
  81. }
  82. void TRAFFIC_CLOSING_do() {
  83.   buzzerTicking(SLOW); // Buzzer ticks slowly
  84.   myfnUpdateDisplay(getStopSign()); // Displays a stop sign on the 7-segment display
  85.   int currentMillis = 0;
  86.   currentMillis = millis();
  87.   if (currentMillis > INTERVAL_TRAFFIC_ORANGE) {
  88.     canBeChangedSubTraffic = true;
  89.   }
  90. }
  91. void TRAFFIC_CLOSING_exit() {
  92.   canBeChangedSubTraffic = false;
  93. }
  94.  
  95. // || -- TRAFFIC CLOSED -- || //
  96. void TRAFFIC_CLOSED_entry(int trafficLight) {
  97.   turnTrafficLightOn(trafficLight, RED); // Turns traffic light back to red
  98. }
  99. void TRAFFIC_CLOSED_do() {
  100.   buzzerTicking(SLOW); // Buzzer ticks slowly
  101. }
  102. void TRAFFIC_CLOSED_exit() {
  103.   Serial.println("DEAD TIMER");
  104.   if (getDeadTimer())  {
  105.     Serial.println("DEAD TIMER DONE");
  106.     subStateMachineTrafficBusy = false;
  107.     headStateMachineSetup();
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement