safwan092

Traffic Light 2 Controlled Via WiFi RemoteXY App Android and IOS

Oct 17th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1.  
  2. //motojic288
  3.  
  4. #define REMOTEXY_MODE__WIFI_CLOUD
  5.  
  6. #include <WiFi.h>
  7.  
  8. // RemoteXY connection settings
  9. #define REMOTEXY_WIFI_SSID "BS Home1"
  10. #define REMOTEXY_WIFI_PASSWORD "0554418546"
  11. #define REMOTEXY_CLOUD_SERVER "cloud.remotexy.com"
  12. #define REMOTEXY_CLOUD_PORT 6376
  13. #define REMOTEXY_CLOUD_TOKEN "6241965ea2c1223521878674596b2e72"
  14.  
  15.  
  16. #include <RemoteXY.h>
  17.  
  18. // RemoteXY GUI configuration
  19. #pragma pack(push, 1)
  20. uint8_t RemoteXY_CONF[] = // 145 bytes
  21. { 255, 3, 0, 0, 0, 138, 0, 19, 0, 0, 0, 87, 105, 70, 105, 32, 67, 111, 110, 116,
  22. 114, 111, 108, 108, 101, 100, 32, 84, 114, 97, 102, 102, 105, 99, 32, 76, 105, 103, 104, 116,
  23. 0, 31, 1, 106, 200, 1, 1, 3, 0, 1, 28, 6, 51, 51, 0, 1, 8, 78, 111, 114,
  24. 109, 97, 108, 32, 84, 114, 97, 102, 102, 105, 99, 32, 76, 105, 103, 104, 116, 115, 0, 1,
  25. 27, 58, 52, 52, 0, 94, 8, 84, 114, 97, 102, 102, 105, 99, 32, 76, 105, 103, 104, 116,
  26. 32, 49, 32, 105, 115, 32, 71, 114, 101, 101, 110, 0, 1, 27, 111, 51, 51, 0, 120, 8,
  27. 84, 114, 97, 102, 102, 105, 99, 32, 76, 105, 103, 104, 116, 32, 50, 32, 105, 115, 32, 71,
  28. 114, 101, 101, 110, 0
  29. };
  30.  
  31. // this structure defines all the variables and events of your control interface
  32. struct {
  33.  
  34. // input variables
  35. uint8_t button_01; // =1 if button pressed, else =0
  36. uint8_t button_02; // =1 if button pressed, else =0
  37. uint8_t button_03; // =1 if button pressed, else =0
  38.  
  39. // other variable
  40. uint8_t connect_flag; // =1 if wire connected, else =0
  41.  
  42. } RemoteXY;
  43. #pragma pack(pop)
  44.  
  45.  
  46. int flagg =1;
  47.  
  48. #define red1Pin 13
  49. #define yellow1Pin 12
  50. #define green1Pin 14
  51. #define red2Pin 27
  52. #define yellow2Pin 26
  53. #define green2Pin 25
  54.  
  55. int state_input1 = 0;
  56. int state_input2 = 0;
  57. int state_input3 = 0;
  58.  
  59.  
  60. unsigned long currentMillis = 0;
  61. unsigned long previousMillis1 = 0;
  62. unsigned long previousMillis2 = 0;
  63. unsigned long previousMillis3 = 0;
  64.  
  65. const long interval1 = 2000;
  66. const long interval2 = 2000;
  67. const long interval3 = 2000;
  68.  
  69. int i = 0;
  70. int j = 0;
  71. enum TrafficLightState {
  72. RED,
  73. GREEN,
  74. YELLOW
  75. };
  76.  
  77. TrafficLightState lightState1 = RED;
  78. TrafficLightState lightState2 = RED;
  79.  
  80. void setupInputsOutputs() {
  81.  
  82. pinMode(red1Pin, OUTPUT);
  83. pinMode(yellow1Pin, OUTPUT);
  84. pinMode(green1Pin, OUTPUT);
  85.  
  86. pinMode(red2Pin, OUTPUT);
  87. pinMode(yellow2Pin, OUTPUT);
  88. pinMode(green2Pin, OUTPUT);
  89.  
  90. }
  91.  
  92. void turnAllLightsRED() {
  93.  
  94. digitalWrite(red1Pin, HIGH);
  95. digitalWrite(yellow1Pin, LOW);
  96. digitalWrite(green1Pin, LOW);
  97. digitalWrite(red2Pin, HIGH);
  98. digitalWrite(yellow2Pin, LOW);
  99. digitalWrite(green2Pin, LOW);
  100. }
  101. void trafficLightControl(int redPin, int yellowPin, int greenPin, TrafficLightState &currentState, unsigned long &previousMillis, unsigned long currentMillis, long interval) {
  102. switch (currentState) {
  103. case RED:
  104. digitalWrite(redPin, HIGH);
  105. digitalWrite(yellowPin, LOW);
  106. digitalWrite(greenPin, LOW);
  107. if (currentMillis - previousMillis >= interval) {
  108. currentState = GREEN;
  109. previousMillis = currentMillis;
  110. }
  111. if (j == 1) {
  112. i = i + 1;
  113. j = 0;
  114. }
  115. if (i == 2)i = 0;
  116. break;
  117.  
  118. case GREEN:
  119. digitalWrite(redPin, LOW);
  120. digitalWrite(yellowPin, LOW);
  121. digitalWrite(greenPin, HIGH);
  122. if (currentMillis - previousMillis >= interval) {
  123. currentState = YELLOW;
  124. previousMillis = currentMillis;
  125. }
  126. break;
  127.  
  128. case YELLOW:
  129. digitalWrite(redPin, LOW);
  130. digitalWrite(yellowPin, HIGH);
  131. digitalWrite(greenPin, LOW);
  132. if (currentMillis - previousMillis >= interval) {
  133. currentState = RED;
  134. previousMillis = currentMillis;
  135. }
  136. j = 1;
  137. break;
  138. }
  139. }
  140. void controlLights_Normally() {
  141. currentMillis = millis();
  142. if (i == 0) {
  143. // Traffic Light 1
  144. trafficLightControl(red1Pin, yellow1Pin, green1Pin, lightState1, previousMillis1, currentMillis, interval1);
  145. }
  146. else if (i == 1) {
  147. // Traffic Light 2
  148. trafficLightControl(red2Pin, yellow2Pin, green2Pin, lightState2, previousMillis2, currentMillis, interval2);
  149. }
  150.  
  151. }
  152.  
  153.  
  154. void setup() {
  155. RemoteXY_Init ();
  156. Serial.begin(9600);
  157. setupInputsOutputs();
  158. turnAllLightsRED();
  159. }
  160.  
  161. void loop() {
  162. RemoteXY_Handler ();
  163.  
  164. if (RemoteXY.button_01 != 0) {
  165. /* button pressed */
  166. flagg = 1;// 1 = normal
  167. Serial.println("flagg = 1");
  168. }
  169. if (RemoteXY.button_02 != 0) {
  170. /* button pressed */
  171. flagg = 2;// 2 = Traffic Light 1 is Green
  172. Serial.println("flagg = 2");
  173. }
  174. if (RemoteXY.button_03 != 0) {
  175. /* button pressed */
  176. flagg = 3;// 3 = Traffic Light 2 is Green
  177. Serial.println("flagg = 3");
  178. }
  179. if (flagg == 1) {
  180. controlLights_Normally();
  181. }
  182. // 2 = Traffic Light 1 is Green
  183. else if (flagg == 2) {
  184. digitalWrite(red1Pin, LOW);
  185. digitalWrite(yellow1Pin, LOW);
  186. digitalWrite(green1Pin, HIGH);
  187. digitalWrite(red2Pin, HIGH);
  188. digitalWrite(yellow2Pin, LOW);
  189. digitalWrite(green2Pin, LOW);
  190. }
  191. // 3 = Traffic Light 2 is Green
  192. else if (flagg == 3) {
  193. digitalWrite(red1Pin, HIGH);
  194. digitalWrite(yellow1Pin, LOW);
  195. digitalWrite(green1Pin, LOW);
  196. digitalWrite(red2Pin, LOW);
  197. digitalWrite(yellow2Pin, LOW);
  198. digitalWrite(green2Pin, HIGH);
  199. }
  200.  
  201. }//end of loop
  202.  
  203.  
  204.  
  205.  
Advertisement
Add Comment
Please, Sign In to add comment