Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: TrafficLights
- - Source Code compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-02-26 10:45:15
- - Source Code generated by: Mercy
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Ultrasonic sensor measures distance. If distance */
- /* is less than 30 cm run the high density function, */
- /* if distance is less than 30 cm run the low density */
- /* function */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* High-density function - turn on green1, and red2, */
- /* the rest stay off. Do this for 20 seconds. Then */
- /* turn off everything and turn on yellow1 and */
- /* yellow2 for 5 seconds. then turn off everything. */
- /* Afterwads turn on red 1 and green 1 for 20 sec and */
- /* turn of */
- /****** SYSTEM REQUIREMENT 3 *****/
- /* Low-density function - turn on green1, and red2, */
- /* the rest stay off. Do this for 10 seconds. Then */
- /* turn off everything and turn on yellow1 and */
- /* yellow2 for 5 seconds. then turn off everything. */
- /* Afterwads turn on red 1 and green 1 for 10 sec and */
- /* turn of */
- /****** SYSTEM REQUIREMENT 4 *****/
- /* High density -The seven segment should count down */
- /* from 9 to 0 with a delay of 2 seconds */
- /****** SYSTEM REQUIREMENT 5 *****/
- /* High density -The seven segment should count down */
- /* from 9 to 0 with a delay of 1 seconds */
- /****** END SYSTEM REQUIREMENTS *****/
- #include <Arduino.h>
- #include <WiFi.h>
- #include "AdafruitIO_WiFi.h"
- #include <Adafruit_MQTT.h>
- #include <Adafruit_MQTT_Client.h>
- #define SEVEN_SEG_A 22
- #define SEVEN_SEG_B 23
- #define SEVEN_SEG_C 32
- #define SEVEN_SEG_D 25
- #define SEVEN_SEG_E 26
- #define SEVEN_SEG_F 27
- #define SEVEN_SEG_G 33
- #define GREEN_1 4
- #define GREEN_2 16
- #define RED_1 17
- #define RED_2 5
- #define YELLOW_1 18
- #define YELLOW_2 19
- #define PUSH_BUTTON 15
- #define TRIGGER_PIN 12
- #define ECHO_PIN 14
- #define WIFI_SSID "Sheila"
- #define WIFI_PASS "123456789"
- #define MQTT_SERVER "io.adafruit.com"
- #define MQTT_PORT 1883
- #define MQTT_USERNAME "shei2000"
- #define MQTT_KEY "xxx"
- #define ADAFRUIT_IO_USERNAME "shei2000"
- #define ADAFRUIT_IO_KEY "xxx"
- WiFiClient espClient;
- Adafruit_MQTT_Client mqtt(&espClient, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_KEY);
- Adafruit_MQTT_Publish green1_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/green1");
- Adafruit_MQTT_Publish green2_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/green2");
- Adafruit_MQTT_Publish red1_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/red1");
- Adafruit_MQTT_Publish red2_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/red2");
- Adafruit_MQTT_Publish yellow1_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/yellow1");
- Adafruit_MQTT_Publish yellow2_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/yellow2");
- Adafruit_MQTT_Publish pushbutton_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/pushbutton");
- Adafruit_MQTT_Publish ultrasonic_feed = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/feeds/ultrasonic");
- AdafruitIO_WiFi io(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY, WIFI_SSID, WIFI_PASS);
- int distance = 0;
- int countdown = 0;
- bool push_button_state = false;
- bool push_button_pressed = false;
- void setup() {
- Serial.begin(115200);
- delay(10);
- pinMode(SEVEN_SEG_A, OUTPUT);
- pinMode(SEVEN_SEG_B, OUTPUT);
- pinMode(SEVEN_SEG_C, OUTPUT);
- pinMode(SEVEN_SEG_D, OUTPUT);
- pinMode(SEVEN_SEG_E, OUTPUT);
- pinMode(SEVEN_SEG_F, OUTPUT);
- pinMode(SEVEN_SEG_G, OUTPUT);
- pinMode(GREEN_1, OUTPUT);
- pinMode(GREEN_2, OUTPUT);
- pinMode(RED_1, OUTPUT);
- pinMode(RED_2, OUTPUT);
- pinMode(YELLOW_1, OUTPUT);
- pinMode(YELLOW_2, OUTPUT);
- pinMode(PUSH_BUTTON, INPUT_PULLUP);
- pinMode(TRIGGER_PIN, OUTPUT);
- pinMode(ECHO_PIN, INPUT);
- // connectToWiFi();
- Serial.println("Connecting to Wi-Fi");
- io.connect();
- while(io.status() < AIO_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.println("Connected to Adafruit IO");
- }
- void loop() {
- // Read ultrasonic sensor
- distance = getUltrasonicDistance();
- //ultrasonic_feed.publish(distance);
- // Send data to Adafruit IO
- AdafruitIO_Feed *ultrasonicFeed = io.feed("distance");
- ultrasonicFeed->save(distance);
- // Check push button state
- if (digitalRead(PUSH_BUTTON) == LOW && !push_button_pressed) {
- push_button_state = !push_button_state;
- pushbutton_feed.publish(push_button_state ? "ON" : "OFF");
- push_button_pressed = true;
- countdown = 10;
- displayCountdown();
- } else if (digitalRead(PUSH_BUTTON) == HIGH) {
- push_button_pressed = false;
- }
- if (push_button_state) {
- if (countdown > 0) {
- countdown--;
- displayCountdown();
- } else {
- push_button_state = false;
- pushbutton_feed.publish("OFF");
- // Resume based on ultrasonic readings
- resumeTrafficLights();
- }
- } else {
- // Control traffic lights based on ultrasonic readings
- controlTrafficLights();
- }
- }
- void controlTrafficLights() {
- if (distance >= 30) {
- // Turn on green1 and red2
- digitalWrite(GREEN_1, HIGH);
- digitalWrite(RED_2, HIGH);
- delay(6000); // 6 seconds
- AdafruitIO_Feed *green1Feed = io.feed("ON");
- AdafruitIO_Feed *humidityFeed = io.feed("humidity");
- temperatureFeed->save(temperature);
- humidityFeed->save(humidity);
- } else {
- // Turn on green1 and red2
- digitalWrite(GREEN_1, HIGH);
- digitalWrite(RED_2, HIGH);
- delay(10000); // 10 seconds
- }
- digitalWrite(GREEN_1, LOW);
- digitalWrite(RED_2, LOW);
- // Turn on yellow1 and yellow2
- digitalWrite(YELLOW_1, HIGH);
- digitalWrite(YELLOW_2, HIGH);
- delay(3000); // 3 seconds
- digitalWrite(YELLOW_1, LOW);
- digitalWrite(YELLOW_2, LOW);
- // Turn on red1 and green2
- if (distance >= 30) {
- // Turn on red1 and green2
- digitalWrite(RED_1, HIGH);
- digitalWrite(GREEN_2, HIGH);
- delay(6000); // 6 seconds
- } else {
- // Turn on red1 and green2
- digitalWrite(RED_1, HIGH);
- digitalWrite(GREEN_2, HIGH);
- delay(10000); // 10 seconds
- }
- digitalWrite(RED_1, LOW);
- digitalWrite(GREEN_2, LOW);
- }
- void resumeTrafficLights() {
- if (distance >= 30) {
- digitalWrite(GREEN_1, HIGH);
- digitalWrite(RED_2, HIGH);
- } else {
- digitalWrite(GREEN_1, HIGH);
- digitalWrite(RED_2, HIGH);
- }
- }
- int getUltrasonicDistance() {
- digitalWrite(TRIGGER_PIN, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIGGER_PIN, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIGGER_PIN, LOW);
- long duration = pulseIn(ECHO_PIN, HIGH);
- int distance_cm = duration * 0.034 / 2;
- serial.print(distance_cm)
- return distance_cm;
- }
- void displayCountdown() {
- digitalWrite(SEVEN_SEG_A, LOW);
- digitalWrite(SEVEN_SEG_B, LOW);
- digitalWrite(SEVEN_SEG_C, LOW);
- digitalWrite(SEVEN_SEG_D, LOW);
- digitalWrite(SEVEN_SEG_E, LOW);
- digitalWrite(SEVEN_SEG_F, LOW);
- digitalWrite(SEVEN_SEG_G, LOW);
- switch(countdown) {
- case 0:
- digitalWrite(SEVEN_SEG_A, HIGH);
- digitalWrite(SEVEN_SEG_B, HIGH);
- digitalWrite(SEVEN_SEG_C, HIGH);
- digitalWrite(SEVEN_SEG_D, HIGH);
- digitalWrite(SEVEN_SEG_E, HIGH);
- digitalWrite(SEVEN_SEG_F, HIGH);
- break;
- case 1:
- digitalWrite(SEVEN_SEG_B, HIGH);
- digitalWrite(SEVEN_SEG_C, HIGH);
- break;
- case 2:
- digitalWrite(SEVEN_SEG_A, HIGH);
- digitalWrite(SEVEN_SEG_B, HIGH);
- digitalWrite(SEVEN_SEG_D, HIGH);
- digitalWrite(SEVEN_SEG_E, HIGH);
- digitalWrite(SEVEN_SEG_G, HIGH);
- break;
- case 3:
- digitalWrite(SEVEN_SEG_A, HIGH);
- digitalWrite(SEVEN_SEG_B, HIGH);
- digitalWrite(SEVEN_SEG_C, HIGH);
- digitalWrite(SEVEN_SEG_D, HIGH);
- digitalWrite(SEVEN_SEG_G, HIGH);
- break;
- case 4:
- digitalWrite(SEVEN_SEG_B, HIGH);
- digitalWrite(SEVEN_SEG_C, HIGH);
- digitalWrite(SEVEN_SEG_F, HIGH);
- digitalWrite(SEVEN_SEG_G, HIGH);
- break;
- case 5:
- digitalWrite(SEVEN_SEG_A, HIGH);
- digitalWrite(SEVEN_SEG_C, HIGH);
- digitalWrite(SEVEN_SEG_D, HIGH);
- digitalWrite(SEVEN_SEG_F, HIGH);
- digitalWrite(SEVEN_SEG_G, HIGH);
- break;
- case 6:
- digitalWrite(SEVEN_SEG_A, HIGH);
- digitalWrite(SEVEN_SEG_C, HIGH);
- digitalWrite(SEVEN_SEG_D, HIGH);
- digitalWrite(SEVEN_SEG_E, HIGH);
- digitalWrite(SEVEN_SEG_F, HIGH);
- digitalWrite(SEVEN_SEG_G, HIGH);
- break;
- case 7:
- digitalWrite(SEVEN_SEG_A, HIGH);
- digitalWrite(SEVEN_SEG_B, HIGH);
- digitalWrite(SEVEN_SEG_C, HIGH);
- break;
- case 8:
- digitalWrite(SEVEN_SEG_A, HIGH);
- digitalWrite(SEVEN_SEG_B, HIGH);
- digitalWrite(SEVEN_SEG_C, HIGH);
- digitalWrite(SEVEN_SEG_D, HIGH);
- digitalWrite(SEVEN_SEG_E, HIGH);
- digitalWrite(SEVEN_SEG_F, HIGH);
- digitalWrite(SEVEN_SEG_G, HIGH);
- break;
- case 9:
- digitalWrite(SEVEN_SEG_A, HIGH);
- digitalWrite(SEVEN_SEG_B, HIGH);
- digitalWrite(SEVEN_SEG_C, HIGH);
- digitalWrite(SEVEN_SEG_D, HIGH);
- digitalWrite(SEVEN_SEG_F, HIGH);
- digitalWrite(SEVEN_SEG_G, HIGH);
- break;
- }
- }
- void connectToWiFi() {
- Serial.print("Connecting to Wi-Fi");
- WiFi.begin(WIFI_SSID, WIFI_PASS);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("Connected!");
- }
- void MQTT_connect() {
- int8_t ret;
- if (mqtt.connected()) {
- return;
- }
- Serial.print("Connecting to MQTT...");
- while ((ret = mqtt.connect()) != 0) {
- Serial.println(mqtt.connectErrorString(ret));
- Serial.println("Retrying MQTT connection in 5 seconds...");
- mqtt.disconnect();
- delay(5000);
- }
- Serial.println("MQTT Connected!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement