safwan092

Traffic Light Project in Traffic Condition 4 Sensors

Nov 30th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. #define red1Pin 7
  2. #define yellow1Pin 6
  3. #define green1Pin 5
  4. #define red2Pin 4
  5. #define yellow2Pin 3
  6. #define green2Pin 2
  7.  
  8. #define input1 8
  9. #define input2 9
  10. #define input3 10
  11. #define input4 11
  12.  
  13. int state_input1 = 0;
  14. int state_input2 = 0;
  15. int state_input3 = 0;
  16. int state_input4 = 0;
  17.  
  18. int trafficLight_1_Car_Count = 0;
  19. int trafficLight_2_Car_Count = 0;
  20.  
  21. unsigned long currentMillis = 0;
  22. unsigned long previousMillis1 = 0;
  23. unsigned long previousMillis2 = 0;
  24. unsigned long previousMillis3 = 0;
  25. unsigned long previousMillis4 = 0;
  26. const long interval1 = 2000;
  27. const long interval2 = 2000;
  28. const long interval3 = 2000;
  29. const long interval4 = 2000;
  30. int i = 0;
  31. int j = 0;
  32. enum TrafficLightState {
  33. RED,
  34. GREEN,
  35. YELLOW
  36. };
  37.  
  38. TrafficLightState lightState1 = RED;
  39. TrafficLightState lightState2 = RED;
  40.  
  41. void setup() {
  42. Serial.begin(9600);
  43. setupInputsOutputs();
  44. turnAllLightsRED();
  45. }
  46.  
  47. void loop() {
  48. readInputs();
  49. if (trafficLight_1_Car_Count > trafficLight_2_Car_Count) {
  50. Serial.println("1 [Activated]");
  51. resetLEDs();
  52. turnAllLightsRED();
  53. delay(1000);
  54. digitalWrite(red1Pin, 0);
  55. digitalWrite(yellow1Pin, 0);
  56. digitalWrite(green1Pin, 1);
  57. delay(5000);
  58. digitalWrite(green1Pin, 0);
  59. digitalWrite(yellow1Pin, 1);
  60. delay(1000);
  61. turnAllLightsRED();
  62. }
  63. if (trafficLight_2_Car_Count > trafficLight_1_Car_Count) {
  64. Serial.println("2 [Activated]");
  65. resetLEDs();
  66. turnAllLightsRED();
  67. delay(1000);
  68. digitalWrite(red2Pin, 0);
  69. digitalWrite(yellow2Pin, 0);
  70. digitalWrite(green2Pin, 1);
  71. delay(5000);
  72. digitalWrite(green2Pin, 0);
  73. digitalWrite(yellow2Pin, 1);
  74. delay(1000);
  75. turnAllLightsRED();
  76. }
  77. else {
  78. Serial.println("Normal Mode");
  79. controlLights_Normally();
  80. }
  81. }//end of LOOP
  82.  
  83.  
  84.  
  85.  
  86. void trafficLightControl(int redPin, int yellowPin, int greenPin, TrafficLightState &currentState, unsigned long &previousMillis, unsigned long currentMillis, long interval) {
  87. switch (currentState) {
  88. case RED:
  89. digitalWrite(redPin, HIGH);
  90. digitalWrite(yellowPin, LOW);
  91. digitalWrite(greenPin, LOW);
  92. if (currentMillis - previousMillis >= interval) {
  93. currentState = GREEN;
  94. previousMillis = currentMillis;
  95. }
  96. if (j == 1) {
  97. i = i + 1;
  98. j = 0;
  99. }
  100. if (i == 2)i = 0;
  101. break;
  102.  
  103. case GREEN:
  104. digitalWrite(redPin, LOW);
  105. digitalWrite(yellowPin, LOW);
  106. digitalWrite(greenPin, HIGH);
  107. if (currentMillis - previousMillis >= interval) {
  108. currentState = YELLOW;
  109. previousMillis = currentMillis;
  110. }
  111. break;
  112.  
  113. case YELLOW:
  114. digitalWrite(redPin, LOW);
  115. digitalWrite(yellowPin, HIGH);
  116. digitalWrite(greenPin, LOW);
  117. if (currentMillis - previousMillis >= interval) {
  118. currentState = RED;
  119. previousMillis = currentMillis;
  120. }
  121. j = 1;
  122. break;
  123. }
  124. }
  125.  
  126.  
  127. void turnAllLightsRED() {
  128. digitalWrite(red1Pin, HIGH);
  129. digitalWrite(yellow1Pin, LOW);
  130. digitalWrite(green1Pin, LOW);
  131. digitalWrite(red2Pin, HIGH);
  132. digitalWrite(yellow2Pin, LOW);
  133. digitalWrite(green2Pin, LOW);
  134. }
  135.  
  136. void setupInputsOutputs() {
  137. pinMode(input1, INPUT);
  138. pinMode(input2, INPUT);
  139. pinMode(input3, INPUT);
  140. pinMode(input4, INPUT);
  141. pinMode(red1Pin, OUTPUT);
  142. pinMode(yellow1Pin, OUTPUT);
  143. pinMode(green1Pin, OUTPUT);
  144. pinMode(red2Pin, OUTPUT);
  145. pinMode(yellow2Pin, OUTPUT);
  146. pinMode(green2Pin, OUTPUT);
  147. }
  148.  
  149.  
  150. void controlLights_Normally() {
  151. currentMillis = millis();
  152. if (i == 0) {
  153. // Traffic Light 1
  154. trafficLightControl(red1Pin, yellow1Pin, green1Pin, lightState1, previousMillis1, currentMillis, interval1);
  155. }
  156. else if (i == 1) {
  157. // Traffic Light 2
  158. trafficLightControl(red2Pin, yellow2Pin, green2Pin, lightState2, previousMillis2, currentMillis, interval2);
  159. }
  160. }
  161.  
  162. void readInputs() {
  163. state_input1 = !digitalRead(input1);
  164. state_input2 = !digitalRead(input2);
  165. state_input3 = !digitalRead(input3);
  166. state_input4 = !digitalRead(input4);
  167. trafficLight_1_Car_Count = state_input1 + state_input2;
  168. trafficLight_2_Car_Count = state_input3 + state_input4;
  169.  
  170. Serial.print(state_input1);
  171. Serial.print(" ");
  172. Serial.print(state_input2);
  173. Serial.print(" ");
  174. Serial.print(state_input3);
  175. Serial.print(" ");
  176. Serial.println(state_input4);
  177. Serial.print(trafficLight_1_Car_Count);
  178. Serial.print(" ");
  179. Serial.println(trafficLight_2_Car_Count);
  180.  
  181. }
  182.  
  183. void resetLEDs() {
  184. i = 0;
  185. j = 0;
  186. lightState1 = RED;
  187. lightState2 = RED;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment