pleasedontcode

Traffic Controller rev_01

Nov 23rd, 2025
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Traffic Controller
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-11-23 07:01:06
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a scheduling system for traffic lights */
  21.     /* (yellow, red, green, Don't Walk, Walk) */
  22.     /* synchronized with a push button to activate Walk */
  23.     /* mode during yellow or red lights. Use the */
  24.     /* EasyButton library and RTC for precise timing. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. // SYSTEM REQUIREMENTS:
  31. // Implement a scheduling system for traffic lights
  32. // (yellow, red, green, Don't Walk, Walk)
  33. // synchronized with a push button to activate Walk mode during yellow or red lights.
  34. // Use EasyButton library and RTC for precise timing.
  35.  
  36. #include <EasyButton.h>
  37. #include <PCF85063A-SOLDERED.h>
  38.  
  39. // Define pins for traffic lights
  40. const uint8_t pinYellow = 12;
  41. const uint8_t pinRed = 11;
  42. const uint8_t pinGreen = 10;
  43. // Define pins for pedestrian signals
  44. const uint8_t pinDontWalk = 9;
  45. const uint8_t pinWalk = 8;
  46. // Define push button pin to activate Walk mode
  47. const uint8_t buttonPin = 2;
  48.  
  49. // Timing variables in milliseconds
  50. unsigned long yellowDuration = 4000;
  51. unsigned long redDuration = 4000;
  52. unsigned long greenDuration = 4000;
  53. unsigned long walkDuration = 2000;
  54. unsigned long flashInterval = 250;
  55.  
  56. // Define button instance
  57. EasyButton walkButton(buttonPin);
  58.  
  59. // Define RTC instance
  60. PCF85063A rtc;
  61.  
  62. // State enumeration for traffic light states
  63. enum State {
  64.   S_YELLOW,
  65.   S_RED,
  66.   S_GREEN,
  67.   S_DONT_WALK,
  68.   S_WALK_STEADY,
  69.   S_WALK_FLASH
  70. };
  71.  
  72. State currentState = S_YELLOW;
  73. unsigned long stateStartTime = 0;
  74. bool walkModeActivated = false;
  75. bool walkButtonPressed = false;
  76.  
  77. void setup() {
  78.   // Initialize serial for debugging
  79.   Serial.begin(115200);
  80.  
  81.   // Initialize pins
  82.   pinMode(pinYellow, OUTPUT);
  83.   pinMode(pinRed, OUTPUT);
  84.   pinMode(pinGreen, OUTPUT);
  85.   pinMode(pinDontWalk, OUTPUT);
  86.   pinMode(pinWalk, OUTPUT);
  87.  
  88.   // Initialize RTC
  89.   rtc.begin();
  90.   // Set initial time and date
  91.   rtc.setTime(6, 0, 0); // 6:00:00
  92.   rtc.setDate(6, 15, 5, 2020); // Saturday, 15.05.2020
  93.   // Set RTC alarm for scheduled transition if needed
  94.   // (Optional, not used in current logic)
  95.  
  96.   // Initialize button
  97.   walkButton.begin();
  98.   walkButton.onPressed([](){
  99.     walkButtonPressed = true;
  100.   });
  101.  
  102.   // Start with yellow light
  103.   currentState = S_YELLOW;
  104.   stateStartTime = millis();
  105. }
  106.  
  107. void loop() {
  108.   // Read button state
  109.   walkButton.read();
  110.  
  111.   // Check if walk button was pressed
  112.   if (walkButtonPressed) {
  113.     walkModeActivated = true;
  114.     walkButtonPressed = false;
  115.   }
  116.  
  117.   unsigned long currentTime = millis();
  118.  
  119.   switch (currentState) {
  120.     case S_YELLOW:
  121.       digitalWrite(pinYellow, HIGH);
  122.       digitalWrite(pinRed, LOW);
  123.       digitalWrite(pinGreen, LOW);
  124.       digitalWrite(pinDontWalk, HIGH);
  125.       digitalWrite(pinWalk, LOW);
  126.       if (currentTime - stateStartTime >= yellowDuration) {
  127.         currentState = S_RED;
  128.         stateStartTime = currentTime;
  129.       }
  130.       break;
  131.  
  132.     case S_RED:
  133.       digitalWrite(pinRed, HIGH);
  134.       digitalWrite(pinYellow, LOW);
  135.       digitalWrite(pinGreen, LOW);
  136.       digitalWrite(pinDontWalk, HIGH);
  137.       digitalWrite(pinWalk, LOW);
  138.       if (currentTime - stateStartTime >= redDuration || walkModeActivated) {
  139.         currentState = S_GREEN;
  140.         stateStartTime = currentTime;
  141.         walkModeActivated = false;
  142.       }
  143.       break;
  144.  
  145.     case S_GREEN:
  146.       digitalWrite(pinGreen, HIGH);
  147.       digitalWrite(pinYellow, LOW);
  148.       digitalWrite(pinRed, LOW);
  149.       digitalWrite(pinDontWalk, HIGH);
  150.       digitalWrite(pinWalk, LOW);
  151.       if (currentTime - stateStartTime >= greenDuration) {
  152.         currentState = S_DONT_WALK;
  153.         stateStartTime = currentTime;
  154.       }
  155.       break;
  156.  
  157.     case S_DONT_WALK:
  158.       digitalWrite(pinDontWalk, HIGH);
  159.       digitalWrite(pinWalk, LOW);
  160.       if (walkModeActivated) {
  161.         currentState = S_WALK_STEADY;
  162.         stateStartTime = currentTime;
  163.       } else if (currentTime - stateStartTime >= redDuration) {
  164.         currentState = S_YELLOW;
  165.         stateStartTime = currentTime;
  166.       }
  167.       break;
  168.  
  169.     case S_WALK_STEADY:
  170.       digitalWrite(pinDontWalk, LOW);
  171.       digitalWrite(pinWalk, HIGH);
  172.       if (currentTime - stateStartTime >= walkDuration) {
  173.         currentState = S_WALK_FLASH;
  174.         stateStartTime = currentTime;
  175.       }
  176.       break;
  177.  
  178.     case S_WALK_FLASH:
  179.       if ((currentTime / flashInterval) % 2 == 0) {
  180.         digitalWrite(pinWalk, HIGH);
  181.       } else {
  182.         digitalWrite(pinWalk, LOW);
  183.       }
  184.       if (currentTime - stateStartTime >= walkDuration) {
  185.         digitalWrite(pinWalk, LOW);
  186.         digitalWrite(pinDontWalk, HIGH);
  187.         walkModeActivated = false;
  188.         currentState = S_YELLOW;
  189.         stateStartTime = currentTime;
  190.       }
  191.       break;
  192.   }
  193.   // Optional: Use RTC to schedule precise transitions or for logging
  194.   // Not used directly in this implementation
  195. }
  196.  
  197. /* END CODE */
  198.  
Advertisement
Add Comment
Please, Sign In to add comment