Advertisement
safwan092

Untitled

Nov 19th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. #include <RCSwitch.h>
  2.  
  3. RCSwitch mySwitch = RCSwitch();
  4.  
  5. // set pin numbers:
  6.  
  7. const int redPin = 13;// the number of the red LED pin
  8. const int yellowPin = 12;// the number of the yellow LED pin
  9. const int greenPin = 11;// the number of the green LED pin
  10. const int red2Pin = 10;// the number of the red LED pin
  11. const int yellow2Pin = 9;// the number of the yellow LED pin
  12. const int green2Pin = 8;// the number of the green LED pin
  13.  
  14. // Variables will change:
  15.  
  16. String code;
  17. int flag = 0;
  18. int redState = LOW;// redState used to set the red LED
  19. int yellowState = LOW;// yellowState used to set the yellow LED
  20. int greenState = LOW;// greenState used to set the green LED
  21. int red2State = LOW;// redState used to set the red LED
  22. int yellow2State = LOW;// yellowState used to set the yellow LED
  23. int green2State = LOW;// greenState used to set the green LED
  24.  
  25.  
  26. unsigned long StartTime = 0;// will store last time the traffic sequence was started
  27. unsigned long CheckTime;
  28. unsigned long elapsedTime;
  29.  
  30. // the follow variables is a long because the time, measured in miliseconds,
  31. // will quickly become a bigger number than can be stored in an int.
  32. long interval = 6000;// sequence interval is the time in miliseconds from the start of the green
  33. // light to the end of the red (milliseconds)
  34.  
  35. void setup() {
  36.  
  37. Serial.begin(9600);
  38. mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
  39.  
  40. // set the digital pin as output:
  41. pinMode(redPin, OUTPUT);
  42. pinMode(yellowPin, OUTPUT);
  43. pinMode(greenPin, OUTPUT);
  44. pinMode(red2Pin, OUTPUT);
  45. pinMode(yellow2Pin, OUTPUT);
  46. pinMode(green2Pin, OUTPUT);
  47. digitalWrite(redPin, 1);
  48. digitalWrite(yellowPin, 1);
  49. digitalWrite(greenPin, 1);
  50. digitalWrite(red2Pin, 1);
  51. digitalWrite(yellow2Pin, 1);
  52. digitalWrite(green2Pin, 1);
  53. delay(2000);
  54.  
  55. }
  56.  
  57. void loop() {
  58.  
  59. if (mySwitch.available()) {
  60. code = mySwitch.getReceivedValue();
  61. Serial.print("Received ");
  62. Serial.println( code );
  63. if (code == "11836946") {
  64. Serial.println("Open Green for 10 Seconds");
  65. flag = 1;
  66. }
  67. mySwitch.resetAvailable();
  68. }
  69.  
  70. if (flag == 0) {
  71. normalOperation();
  72. }
  73.  
  74. else if (flag == 1) {
  75. if (greenState) {
  76. delay(1000);
  77. digitalWrite(redPin, LOW);
  78. digitalWrite(greenPin, LOW);
  79. digitalWrite(yellowPin, HIGH);
  80. delay(1000);
  81. digitalWrite(greenPin, LOW);
  82. digitalWrite(yellowPin, LOW);
  83. digitalWrite(redPin, HIGH);
  84. digitalWrite(green2Pin, HIGH);
  85. digitalWrite(red2Pin, LOW);
  86. digitalWrite(yellow2Pin, LOW);
  87. delay(10000);
  88. digitalWrite(yellow2Pin, HIGH);
  89. digitalWrite(green2Pin, LOW);
  90. digitalWrite(red2Pin, LOW);
  91. delay(1000);
  92. flag = 0;
  93. }
  94. if (yellowState) {
  95. delay(1000);
  96. digitalWrite(redPin, HIGH);
  97. digitalWrite(greenPin, LOW);
  98. digitalWrite(yellowPin, LOW);
  99. digitalWrite(green2Pin, HIGH);
  100. digitalWrite(red2Pin, LOW);
  101. digitalWrite(yellow2Pin, LOW);
  102. delay(10000);
  103. digitalWrite(yellow2Pin, HIGH);
  104. digitalWrite(green2Pin, LOW);
  105. digitalWrite(red2Pin, LOW);
  106. delay(1000);
  107. flag = 0;
  108. }
  109. if (redState) {
  110. delay(10000);
  111. digitalWrite(yellow2Pin, HIGH);
  112. digitalWrite(green2Pin, LOW);
  113. digitalWrite(red2Pin, LOW);
  114. delay(1000);
  115. flag = 0;
  116. }
  117. }
  118.  
  119. }// end of LOOP
  120.  
  121.  
  122. void normalOperation() {
  123.  
  124. CheckTime = millis();
  125. elapsedTime = CheckTime - StartTime;
  126.  
  127. if (elapsedTime > interval) {
  128. StartTime = CheckTime;
  129. }
  130.  
  131. if (elapsedTime < 2000) {
  132. redState = LOW;
  133. yellowState = LOW;
  134. greenState = HIGH;
  135. red2State = HIGH;
  136. yellow2State = LOW;
  137. green2State = LOW;
  138. }
  139.  
  140. if (elapsedTime > 2000 && elapsedTime < 3000 ) {
  141. redState = LOW;
  142. yellowState = HIGH;
  143. greenState = LOW ;
  144. red2State = HIGH;
  145. yellow2State = LOW;
  146. green2State = LOW;
  147. }
  148.  
  149. if (elapsedTime > 3000 && elapsedTime < 5000) {
  150. redState = HIGH;
  151. yellowState = LOW;
  152. greenState = LOW ;
  153. red2State = LOW;
  154. yellow2State = LOW;
  155. green2State = HIGH;
  156. }
  157.  
  158. if (elapsedTime > 5000 && elapsedTime < 6000) {
  159. redState = HIGH;
  160. yellowState = LOW;
  161. greenState = LOW ;
  162. red2State = LOW;
  163. yellow2State = HIGH;
  164. green2State = LOW;
  165. }
  166.  
  167. digitalWrite(redPin, redState);
  168. digitalWrite(greenPin, greenState);
  169. digitalWrite(yellowPin, yellowState);
  170. digitalWrite(red2Pin, red2State);
  171. digitalWrite(green2Pin, green2State);
  172. digitalWrite(yellow2Pin, yellow2State);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement