Advertisement
ripred

TrafficLight.ino

Jun 8th, 2023 (edited)
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | Source Code | 0 0
  1. //
  2. // TrafficLight.ino
  3. //
  4. // 2023 trent m. wyatt
  5. //
  6. // example traffic light system for 3-sided lights
  7. //
  8.  
  9. enum MagicNumbers {
  10.     // alias' for the 3 sides
  11.          N = 0,
  12.          E = 1,
  13.          S = 2,
  14.  
  15.     // alias' for the two sets of traffic: NS and EW
  16.         NS = 0,
  17.         EW = 1,
  18.  
  19.     // alias' for the 3 colors
  20.      GREEN = 0,
  21.     YELLOW = 1,
  22.        RED = 2,
  23.  
  24.     // motion sensor pin at 12
  25.     motionSensorPin = 12,
  26. };
  27.  
  28.  
  29. //
  30. // LED pin definitions array:
  31. //
  32. int pins[3][3];
  33.  
  34. //
  35. // Global Variables
  36. //
  37. bool motionDetected = false;
  38.  
  39. //
  40. // Current traffic flow
  41. //
  42. int flowing = NS;
  43.  
  44. //
  45. // Preset Color Patterns:
  46. //                       G     Y     R
  47. int   greenOnly[3] = { HIGH,  LOW,  LOW };
  48. int  yellowOnly[3] = {  LOW, HIGH,  LOW };
  49. int     redOnly[3] = {  LOW,  LOW, HIGH };
  50. int yellowGreen[3] = { HIGH, HIGH,  LOW };
  51.  
  52.  
  53. //
  54. // set the 3 lights on one of the sides
  55. //
  56. void set_side(int dir, int colors[3]) {
  57.     for (int color = GREEN; color <= RED; color++) {
  58.         digitalWrite(pins[dir][color], colors[color]);
  59.     }
  60. }
  61.  
  62.  
  63. //
  64. // Sets the current flow of traffic to the specified colors
  65. //
  66. void set_green() {
  67.     if (flowing == NS) {
  68.         set_side(N, greenOnly);
  69.         set_side(E,   redOnly);
  70.         set_side(S, greenOnly);
  71.     }
  72.     else {
  73.         set_side(N,   redOnly);
  74.         set_side(E, greenOnly);
  75.         set_side(S,   redOnly);
  76.     }
  77. }
  78.  
  79.  
  80. void set_caution() {
  81.     if (flowing == NS) {
  82.         set_side(N, yellowGreen);
  83.         set_side(S, yellowGreen);
  84.     }
  85.     else {
  86.         set_side(E, yellowGreen);
  87.     }
  88. }
  89.  
  90.  
  91. void change_flow() {
  92.     if (flowing == NS) {
  93.         flowing = EW;
  94.     }
  95.     else {
  96.         flowing = NS;
  97.     }
  98. }
  99.  
  100.  
  101. void setup() {
  102.     delay(500);
  103.     Serial.begin(115200);
  104.     delay(500);
  105.  
  106.     // configure the input pin
  107.     pinMode(motionSensorPin, INPUT);
  108.    
  109.     // configure the output pins
  110.     for (int side = N; side <= S; side++) {
  111.         for (int color = GREEN; color <= RED; color++) {
  112.             // calculate the pin #
  113.             int pin = color + (side * 3) + 2;
  114.  
  115.             // set the pin mode: (INPUT, INPUT_PULLUP, or OUTPUT)
  116.             pinMode(pin, OUTPUT);
  117.  
  118.             // remember the pin numbers
  119.             pins[side][color] = pin;
  120.         }
  121.     }
  122.  
  123.     set_green();
  124. }
  125.  
  126.  
  127. void loop() {
  128.     motionDetected = digitalRead(motionSensorPin);
  129.  
  130.     if (motionDetected) {
  131.         set_caution();  // sets green and yellow in the current flow direction
  132.         delay(5000);
  133.         change_flow();  // change from NS flow to EW or from EW to NS flow
  134.         set_green();    // sets green in current flow direction and red in the other direction
  135.     }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement