Advertisement
pleasedontcode

IoT Traffic Light System

Apr 22nd, 2024
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.22 KB | Source Code | 0 0
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: TrafficLights
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-02-26 10:45:15
  15.     - Source Code generated by: Mercy
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** SYSTEM REQUIREMENTS *****/
  20. /****** SYSTEM REQUIREMENT 1 *****/
  21.     /* Ultrasonic sensor measures distance. If distance */
  22.     /* is less than 30 cm run the high density function, */
  23.     /* if distance is less than 30 cm run the low density */
  24.     /* function */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* High-density function - turn on green1, and red2, */
  27.     /* the rest stay off. Do this for 20 seconds. Then */
  28.     /* turn off everything and turn on yellow1 and */
  29.     /* yellow2 for 5 seconds.  then turn off everything. */
  30.     /* Afterwads turn on red 1 and green 1 for 20 sec and */
  31.     /* turn of */
  32. /****** SYSTEM REQUIREMENT 3 *****/
  33.     /* Low-density function - turn on green1, and red2, */
  34.     /* the rest stay off. Do this for 10 seconds. Then */
  35.     /* turn off everything and turn on yellow1 and */
  36.     /* yellow2 for 5 seconds. then turn off everything. */
  37.     /* Afterwads turn on red 1 and green 1 for 10 sec and */
  38.     /* turn of */
  39. /****** SYSTEM REQUIREMENT 4 *****/
  40.     /* High density -The seven segment should count down */
  41.     /* from 9 to 0 with a delay of 2 seconds */
  42. /****** SYSTEM REQUIREMENT 5 *****/
  43.     /* High density -The seven segment should count down */
  44.     /* from 9 to 0 with a delay of 1 seconds */
  45. /****** END SYSTEM REQUIREMENTS *****/
  46.  
  47. #include <Arduino.h>
  48. #include <WiFi.h>
  49. #include "AdafruitIO_WiFi.h"
  50. #include <Adafruit_MQTT.h>
  51. #include <Adafruit_MQTT_Client.h>
  52.  
  53. #define SEVEN_SEG_A 22
  54. #define SEVEN_SEG_B 23
  55. #define SEVEN_SEG_C 32
  56. #define SEVEN_SEG_D 25
  57. #define SEVEN_SEG_E 26
  58. #define SEVEN_SEG_F 27
  59. #define SEVEN_SEG_G 33
  60.  
  61. #define GREEN_1 4
  62. #define GREEN_2 16
  63. #define RED_1 17
  64. #define RED_2 5
  65. #define YELLOW_1 18
  66. #define YELLOW_2 19
  67.  
  68. #define PUSH_BUTTON 15
  69. #define TRIGGER_PIN 12
  70. #define ECHO_PIN 14
  71.  
  72. #define WIFI_SSID "Sheila"
  73. #define WIFI_PASS "123456789"
  74.  
  75. #define MQTT_SERVER "io.adafruit.com"
  76. #define MQTT_PORT 1883
  77. #define MQTT_USERNAME "shei2000"
  78. #define MQTT_KEY "xxx"
  79.  
  80. #define ADAFRUIT_IO_USERNAME "shei2000"
  81. #define ADAFRUIT_IO_KEY "xxx"
  82.  
  83. WiFiClient espClient;
  84. Adafruit_MQTT_Client mqtt(&espClient, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_KEY);
  85.  
  86. Adafruit_MQTT_Publish green1_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/green1");
  87. Adafruit_MQTT_Publish green2_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/green2");
  88. Adafruit_MQTT_Publish red1_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/red1");
  89. Adafruit_MQTT_Publish red2_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/red2");
  90. Adafruit_MQTT_Publish yellow1_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/yellow1");
  91. Adafruit_MQTT_Publish yellow2_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/yellow2");
  92. Adafruit_MQTT_Publish pushbutton_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/pushbutton");
  93. Adafruit_MQTT_Publish ultrasonic_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/ultrasonic");
  94.  
  95. AdafruitIO_WiFi io(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY, WIFI_SSID, WIFI_PASS);
  96.  
  97.  
  98. int distance = 0;
  99. int countdown = 0;
  100. bool push_button_state = false;
  101. bool push_button_pressed = false;
  102.  
  103. void setup() {
  104.   Serial.begin(115200);
  105.   delay(10);
  106.  
  107.   pinMode(SEVEN_SEG_A, OUTPUT);
  108.   pinMode(SEVEN_SEG_B, OUTPUT);
  109.   pinMode(SEVEN_SEG_C, OUTPUT);
  110.   pinMode(SEVEN_SEG_D, OUTPUT);
  111.   pinMode(SEVEN_SEG_E, OUTPUT);
  112.   pinMode(SEVEN_SEG_F, OUTPUT);
  113.   pinMode(SEVEN_SEG_G, OUTPUT);
  114.  
  115.   pinMode(GREEN_1, OUTPUT);
  116.   pinMode(GREEN_2, OUTPUT);
  117.   pinMode(RED_1, OUTPUT);
  118.   pinMode(RED_2, OUTPUT);
  119.   pinMode(YELLOW_1, OUTPUT);
  120.   pinMode(YELLOW_2, OUTPUT);
  121.   pinMode(PUSH_BUTTON, INPUT_PULLUP);
  122.   pinMode(TRIGGER_PIN, OUTPUT);
  123.   pinMode(ECHO_PIN, INPUT);
  124.  
  125.  // connectToWiFi();
  126.  
  127. Serial.println("Connecting to Wi-Fi");
  128.   io.connect();
  129.   while(io.status() < AIO_CONNECTED) {
  130.     delay(500);
  131.     Serial.print(".");
  132. }
  133. Serial.println();
  134.  
  135.   Serial.println("Connected to Adafruit IO");
  136. }
  137.  
  138. void loop() {
  139.   // Read ultrasonic sensor
  140.   distance = getUltrasonicDistance();
  141.   //ultrasonic_feed.publish(distance);
  142.  
  143.  
  144.  // Send data to Adafruit IO
  145.   AdafruitIO_Feed *ultrasonicFeed = io.feed("distance");
  146.   ultrasonicFeed->save(distance);
  147.  
  148.  
  149.  
  150.   // Check push button state
  151.   if (digitalRead(PUSH_BUTTON) == LOW && !push_button_pressed) {
  152.     push_button_state = !push_button_state;
  153.     pushbutton_feed.publish(push_button_state ? "ON" : "OFF");
  154.     push_button_pressed = true;
  155.     countdown = 10;
  156.     displayCountdown();
  157.   } else if (digitalRead(PUSH_BUTTON) == HIGH) {
  158.     push_button_pressed = false;
  159.   }
  160.  
  161.   if (push_button_state) {
  162.     if (countdown > 0) {
  163.       countdown--;
  164.       displayCountdown();
  165.     } else {
  166.       push_button_state = false;
  167.       pushbutton_feed.publish("OFF");
  168.       // Resume based on ultrasonic readings
  169.       resumeTrafficLights();
  170.     }
  171.   } else {
  172.     // Control traffic lights based on ultrasonic readings
  173.     controlTrafficLights();
  174.   }
  175. }
  176.  
  177. void controlTrafficLights() {
  178.   if (distance >= 30) {
  179.     // Turn on green1 and red2
  180.     digitalWrite(GREEN_1, HIGH);
  181.     digitalWrite(RED_2, HIGH);
  182.     delay(6000); // 6 seconds
  183.     AdafruitIO_Feed *green1Feed = io.feed("ON");
  184.   AdafruitIO_Feed *humidityFeed = io.feed("humidity");
  185.  
  186.   temperatureFeed->save(temperature);
  187.   humidityFeed->save(humidity);
  188.  
  189.   } else {
  190.     // Turn on green1 and red2
  191.     digitalWrite(GREEN_1, HIGH);
  192.     digitalWrite(RED_2, HIGH);
  193.     delay(10000); // 10 seconds
  194.    
  195.   }
  196.   digitalWrite(GREEN_1, LOW);
  197.   digitalWrite(RED_2, LOW);
  198.  
  199.   // Turn on yellow1 and yellow2
  200.   digitalWrite(YELLOW_1, HIGH);
  201.   digitalWrite(YELLOW_2, HIGH);
  202.   delay(3000); // 3 seconds
  203.   digitalWrite(YELLOW_1, LOW);
  204.   digitalWrite(YELLOW_2, LOW);
  205.  
  206.   // Turn on red1 and green2
  207.   if (distance >= 30) {
  208.     // Turn on red1 and green2
  209.     digitalWrite(RED_1, HIGH);
  210.     digitalWrite(GREEN_2, HIGH);
  211.     delay(6000); // 6 seconds
  212.   } else {
  213.     // Turn on red1 and green2
  214.     digitalWrite(RED_1, HIGH);
  215.     digitalWrite(GREEN_2, HIGH);
  216.     delay(10000); // 10 seconds
  217.   }
  218.   digitalWrite(RED_1, LOW);
  219.   digitalWrite(GREEN_2, LOW);
  220. }
  221.  
  222. void resumeTrafficLights() {
  223.   if (distance >= 30) {
  224.     digitalWrite(GREEN_1, HIGH);
  225.     digitalWrite(RED_2, HIGH);
  226.   } else {
  227.     digitalWrite(GREEN_1, HIGH);
  228.     digitalWrite(RED_2, HIGH);
  229.   }
  230. }
  231.  
  232. int getUltrasonicDistance() {
  233.   digitalWrite(TRIGGER_PIN, LOW);
  234.   delayMicroseconds(2);
  235.   digitalWrite(TRIGGER_PIN, HIGH);
  236.   delayMicroseconds(10);
  237.   digitalWrite(TRIGGER_PIN, LOW);
  238.   long duration = pulseIn(ECHO_PIN, HIGH);
  239.   int distance_cm = duration * 0.034 / 2;
  240.   serial.print(distance_cm)
  241.   return distance_cm;
  242. }
  243.  
  244. void displayCountdown() {
  245.   digitalWrite(SEVEN_SEG_A, LOW);
  246.   digitalWrite(SEVEN_SEG_B, LOW);
  247.   digitalWrite(SEVEN_SEG_C, LOW);
  248.   digitalWrite(SEVEN_SEG_D, LOW);
  249.   digitalWrite(SEVEN_SEG_E, LOW);
  250.   digitalWrite(SEVEN_SEG_F, LOW);
  251.   digitalWrite(SEVEN_SEG_G, LOW);
  252.  
  253.   switch(countdown) {
  254.     case 0:
  255.       digitalWrite(SEVEN_SEG_A, HIGH);
  256.       digitalWrite(SEVEN_SEG_B, HIGH);
  257.       digitalWrite(SEVEN_SEG_C, HIGH);
  258.       digitalWrite(SEVEN_SEG_D, HIGH);
  259.       digitalWrite(SEVEN_SEG_E, HIGH);
  260.       digitalWrite(SEVEN_SEG_F, HIGH);
  261.       break;
  262.     case 1:
  263.       digitalWrite(SEVEN_SEG_B, HIGH);
  264.       digitalWrite(SEVEN_SEG_C, HIGH);
  265.       break;
  266.     case 2:
  267.       digitalWrite(SEVEN_SEG_A, HIGH);
  268.       digitalWrite(SEVEN_SEG_B, HIGH);
  269.       digitalWrite(SEVEN_SEG_D, HIGH);
  270.       digitalWrite(SEVEN_SEG_E, HIGH);
  271.       digitalWrite(SEVEN_SEG_G, HIGH);
  272.       break;
  273.     case 3:
  274.       digitalWrite(SEVEN_SEG_A, HIGH);
  275.       digitalWrite(SEVEN_SEG_B, HIGH);
  276.       digitalWrite(SEVEN_SEG_C, HIGH);
  277.       digitalWrite(SEVEN_SEG_D, HIGH);
  278.       digitalWrite(SEVEN_SEG_G, HIGH);
  279.       break;
  280.     case 4:
  281.       digitalWrite(SEVEN_SEG_B, HIGH);
  282.       digitalWrite(SEVEN_SEG_C, HIGH);
  283.       digitalWrite(SEVEN_SEG_F, HIGH);
  284.       digitalWrite(SEVEN_SEG_G, HIGH);
  285.       break;
  286.     case 5:
  287.       digitalWrite(SEVEN_SEG_A, HIGH);
  288.       digitalWrite(SEVEN_SEG_C, HIGH);
  289.       digitalWrite(SEVEN_SEG_D, HIGH);
  290.       digitalWrite(SEVEN_SEG_F, HIGH);
  291.       digitalWrite(SEVEN_SEG_G, HIGH);
  292.       break;
  293.     case 6:
  294.       digitalWrite(SEVEN_SEG_A, HIGH);
  295.       digitalWrite(SEVEN_SEG_C, HIGH);
  296.       digitalWrite(SEVEN_SEG_D, HIGH);
  297.       digitalWrite(SEVEN_SEG_E, HIGH);
  298.       digitalWrite(SEVEN_SEG_F, HIGH);
  299.       digitalWrite(SEVEN_SEG_G, HIGH);
  300.       break;
  301.     case 7:
  302.       digitalWrite(SEVEN_SEG_A, HIGH);
  303.       digitalWrite(SEVEN_SEG_B, HIGH);
  304.       digitalWrite(SEVEN_SEG_C, HIGH);
  305.       break;
  306.     case 8:
  307.       digitalWrite(SEVEN_SEG_A, HIGH);
  308.       digitalWrite(SEVEN_SEG_B, HIGH);
  309.       digitalWrite(SEVEN_SEG_C, HIGH);
  310.       digitalWrite(SEVEN_SEG_D, HIGH);
  311.       digitalWrite(SEVEN_SEG_E, HIGH);
  312.       digitalWrite(SEVEN_SEG_F, HIGH);
  313.       digitalWrite(SEVEN_SEG_G, HIGH);
  314.       break;
  315.     case 9:
  316.       digitalWrite(SEVEN_SEG_A, HIGH);
  317.       digitalWrite(SEVEN_SEG_B, HIGH);
  318.       digitalWrite(SEVEN_SEG_C, HIGH);
  319.       digitalWrite(SEVEN_SEG_D, HIGH);
  320.       digitalWrite(SEVEN_SEG_F, HIGH);
  321.       digitalWrite(SEVEN_SEG_G, HIGH);
  322.       break;
  323.   }
  324. }
  325.  
  326. void connectToWiFi() {
  327.   Serial.print("Connecting to Wi-Fi");
  328.   WiFi.begin(WIFI_SSID, WIFI_PASS);
  329.   while (WiFi.status() != WL_CONNECTED) {
  330.     delay(500);
  331.     Serial.print(".");
  332.   }
  333.   Serial.println("Connected!");
  334. }
  335.  
  336. void MQTT_connect() {
  337.   int8_t ret;
  338.   if (mqtt.connected()) {
  339.     return;
  340.   }
  341.   Serial.print("Connecting to MQTT...");
  342.   while ((ret = mqtt.connect()) != 0) {
  343.     Serial.println(mqtt.connectErrorString(ret));
  344.     Serial.println("Retrying MQTT connection in 5 seconds...");
  345.     mqtt.disconnect();
  346.     delay(5000);
  347.   }
  348.   Serial.println("MQTT Connected!");
  349. }
  350.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement