kartonman

Traffic Lights With Shift Registers

Sep 9th, 2021 (edited)
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. /*Road intersection with traffic lights and pedestrian crossing lights. Uses 2 shift registers to control
  2. traffic lights and pedestrian crossing lights
  3.  
  4. This sketch was created by Gary Granai , July,2021. www.steamtrainfo.com
  5. Yoy are free to use and modify this sketch as long as everything in this comment
  6. section remains intact and unchanged.
  7.  
  8. Traffic lights are on shift register 1 and the pedestrian crossing lights are on shift register 2
  9.  
  10. Main road, crossing road and pedestrian crossing LEDs are each on their own resistor boards.
  11.  
  12. You may choose the number and type lights you put on the intersection. For details and documentation see https://steamtraininfo.com/arduino-projects/traffic-signals
  13. */
  14.  
  15. /*
  16. Shift register pin connections
  17. Shift Register 1 Controls Traffic Lights
  18. Q0 Main Road Red Q1 Main Road Yellow Q2 Main Road Green
  19. Q3 Crossing Road Red Q4 Crossing Road Yellow
  20. Q5 Crossing Road Green
  21.  
  22. Shift Register 2 Controls Pedestrian Crossing Lights
  23. Q0 Main Road Pedestrian Red Q1 Main Road Pedestrian Green
  24. Q2 Crossing Road Pedestrian Red Q3 Crossing Road Pedestrian Green */
  25.  
  26.  
  27.  
  28.  
  29. //Change these delays to change the timing of your traffic flow.
  30.  
  31. #define redDelay 5000
  32. #define yellowDelay 450
  33. #define maingreenDelay 7000
  34. #define crossinggreenDelay 5000
  35. #define redyellowDelay 350
  36.  
  37. //Pin connected to DS of 74HC595
  38. #define dataPin 10
  39. //Pin connected to ST_CP of 74HC595
  40. #define latchPin 11
  41. //Pin connected to SH_CP of 74HC595
  42. #define clockPin 12
  43.  
  44. //***************************************************
  45.  
  46. void setup() {
  47.  
  48. //set pins to output so you can control the shift register
  49. pinMode(latchPin, OUTPUT);
  50. pinMode(clockPin, OUTPUT);
  51. pinMode(dataPin, OUTPUT);
  52.  
  53. Serial.begin(9600);
  54.  
  55. }
  56. //************************************************
  57.  
  58. void loop() {
  59.  
  60. //Main road traffic flow, cross road traffic stopped
  61.  
  62. digitalWrite(latchPin, LOW);
  63. //Pedestrian lights
  64. shiftOut(dataPin, clockPin, MSBFIRST, B00001001);
  65. //Traffic lights
  66. shiftOut(dataPin, clockPin, MSBFIRST, B00001100);
  67.  
  68. digitalWrite(latchPin, HIGH);
  69.  
  70. delay(maingreenDelay);
  71.  
  72. //*********************************************
  73.  
  74. //pedesrian lights flash warning
  75.  
  76. /*begin for function. This flashes 8 times. Change
  77. that and the delay to fit your intersection.*/
  78.  
  79.  
  80. for(int i = 0; i < 7; i++){
  81. digitalWrite(latchPin, LOW);
  82. //Pedestrian lights
  83. shiftOut(dataPin, clockPin, MSBFIRST, B00001001);
  84. //Traffic lights
  85. shiftOut(dataPin, clockPin, MSBFIRST, B00001110);
  86. digitalWrite(latchPin, HIGH);
  87. delay(yellowDelay);
  88.  
  89. digitalWrite(latchPin, LOW);
  90.  
  91. //Pedestrian lights
  92. shiftOut(dataPin, clockPin, MSBFIRST, B00000001);
  93. //Traffic lights
  94. shiftOut(dataPin, clockPin, MSBFIRST, B00001110);
  95. digitalWrite(latchPin, HIGH);
  96.  
  97. delay(yellowDelay);
  98. }
  99. //end for function
  100.  
  101. //************************************************
  102.  
  103. //all traffic stopped
  104.  
  105. digitalWrite(latchPin, LOW);
  106. //Pedestrian lights
  107. shiftOut(dataPin, clockPin, MSBFIRST, B00000101);
  108.  
  109. //Traffic lights
  110. shiftOut(dataPin, clockPin, MSBFIRST, B00001001);
  111. digitalWrite(latchPin, HIGH);
  112. delay(redDelay);
  113.  
  114. //************************************************
  115.  
  116. //Change active lanes. Traffic starts on crossing road
  117. //red yellow flash
  118. digitalWrite(latchPin, LOW);
  119. //Pedestrian lights
  120. shiftOut(dataPin, clockPin, MSBFIRST, B00000101);
  121. //Traffic lights
  122. shiftOut(dataPin, clockPin, MSBFIRST, B00011001);
  123. digitalWrite(latchPin, HIGH);
  124. delay(redyellowDelay);
  125.  
  126.  
  127.  
  128. //Pedestrian lights
  129. digitalWrite(latchPin, LOW);
  130. shiftOut(dataPin, clockPin, MSBFIRST, B00000110);
  131. //Traffic lights
  132. shiftOut(dataPin, clockPin, MSBFIRST, B00100001);
  133. digitalWrite(latchPin, HIGH);
  134.  
  135. delay(crossinggreenDelay);
  136.  
  137. //******************************************************
  138.  
  139. //pedesrian lights flash warning
  140. /*begin for function. This flashes 8 times. Change
  141. that and the delay to fit your intersection.*/
  142.  
  143. for(int i = 0; i < 7; i++){
  144. digitalWrite(latchPin, LOW);
  145. //Pedestrian lights
  146. shiftOut(dataPin, clockPin, MSBFIRST, B00000110);
  147. //Traffic lights
  148. shiftOut(dataPin, clockPin, MSBFIRST, B00110001);
  149. digitalWrite(latchPin, HIGH);
  150. delay(yellowDelay);
  151.  
  152.  
  153. digitalWrite(latchPin, LOW);
  154. //Pedestrian lights
  155. shiftOut(dataPin, clockPin, MSBFIRST, B00000100);
  156. //Traffic lights
  157. shiftOut(dataPin, clockPin, MSBFIRST, B00110001);
  158. digitalWrite(latchPin, HIGH);
  159. delay(yellowDelay);
  160.  
  161. } //end for function
  162.  
  163. //************************************************
  164.  
  165. //all traffic stopped
  166.  
  167.  
  168. digitalWrite(latchPin, LOW);
  169. //Pedestrian lights
  170. shiftOut(dataPin, clockPin, MSBFIRST, B00000101);
  171. //Traffic lights
  172. shiftOut(dataPin, clockPin, MSBFIRST, B00001001);
  173. digitalWrite(latchPin, HIGH);
  174. delay(crossinggreenDelay);
  175.  
  176.  
  177. digitalWrite(latchPin, LOW);
  178. //Pedestrian lights
  179. shiftOut(dataPin, clockPin, MSBFIRST, B00000101);
  180. //Traffic lights
  181. shiftOut(dataPin, clockPin, MSBFIRST, B00001011);
  182. digitalWrite(latchPin, HIGH);
  183. delay(redyellowDelay);
  184. }
  185.  
  186. //*****************************************************
  187.  
  188. /*The active traffic lane will now change as program loops
  189. to beginning.*/
  190.  
  191.  
Add Comment
Please, Sign In to add comment