Advertisement
pleasedontcode

**Smart Control** rev_01

Dec 14th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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: **Smart Control**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-14 19:24:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall initialize components in the */
  21.     /* setup function and execute the main logic */
  22.     /* continuously in the loop function, ensuring */
  23.     /* efficient resource management and responsiveness */
  24.     /* to connected components. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Arduino.h>
  31. #include <Wire.h>
  32. #include <Adafruit_SSD1306.h>
  33. #include <Adafruit_GFX.h>
  34. #include <BlynkSimpleEsp32.h>
  35. #include <BlynkTimer.h>
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void backwashMode();
  41. void normalMode();
  42. void measurePressure();
  43. void stopSystem();
  44.  
  45. /****** BLYNK CREDENTIALS *****/
  46. #define BLYNK_TEMPLATE_ID "TMPL3GbuIMEg9"
  47. #define BLYNK_TEMPLATE_NAME "motor"
  48. #define BLYNK_AUTH_TOKEN "WfLmKbPWmBlpx0un2R2eufubQ-VStAym"
  49.  
  50. // Wi-Fi credentials
  51. char ssid[] = "ME4";
  52. char pass[] = "********"; // Use your actual password
  53.  
  54. // Pin definitions
  55. const int SWITCH1_PIN = 13;  // Use pin 13 for switch 1
  56. const int SWITCH2_PIN = 12;  // Use pin 12 for switch 2
  57. const int PRESSURE_SENSOR = 34; // Analog pin for pressure sensor
  58.  
  59. // Relay control pins
  60. const int relayPins[] = {32, 33, 27, 14, 5, 18, 19, 23};
  61. const int svPins[] = {32, 33, 14, 5, 18, 19, 23};
  62. const int pumpPin = 27;
  63. const int sv7Pin = 19;
  64.  
  65. // Variables
  66. bool isSystemRunning = false;
  67. bool isBackwashMode = false;
  68. bool isNormalMode = false;
  69. unsigned long ufWashStartTime;
  70. int timerId;
  71. float pressurePsi;
  72.  
  73. // OLED display
  74. Adafruit_SSD1306 display(128, 64, &Wire, -1);
  75. BlynkTimer timer;
  76. const int DEBOUNCE_DELAY = 50; // Adjust this value as needed
  77.  
  78. void setup(void)
  79. {
  80.     Serial.begin(9600);  // Initialize serial communication
  81.     Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);  // Initialize Blynk
  82.  
  83.     // Initialize pins
  84.     pinMode(SWITCH1_PIN, INPUT);
  85.     pinMode(SWITCH2_PIN, INPUT);
  86.     for (int i = 0; i < 8; i++) {
  87.         pinMode(relayPins[i], OUTPUT);
  88.         digitalWrite(relayPins[i], LOW); // Initialize relays to LOW
  89.     }
  90.     pinMode(sv7Pin, OUTPUT);
  91.     digitalWrite(sv7Pin, LOW);
  92.  
  93.     // Initialize OLED display
  94.     if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  95.         Serial.println(F("OLED init failed!"));
  96.         while (1); // Loop forever if OLED initialization fails
  97.     }
  98.     display.clearDisplay();
  99.     display.setTextSize(1);
  100.     display.setTextColor(SSD1306_WHITE);
  101.     display.setCursor(0, 0);
  102.     display.println("System Idle");
  103.     display.display();
  104. }
  105.  
  106. void loop(void)
  107. {
  108.     Blynk.run(); // Run Blynk tasks
  109.  
  110.     // Read switch states
  111.     int switch1State = digitalRead(SWITCH1_PIN);
  112.     int switch2State = digitalRead(SWITCH2_PIN);
  113.     Serial.println("Switch 1 State: " + String(switch1State));
  114.     Serial.println("Switch 2 State: " + String(switch2State));
  115.  
  116.     static unsigned long lastSwitch1Change = 0;
  117.     static unsigned long lastSwitch2Change = 0;
  118.  
  119.     // Debounce logic for switch 1
  120.     if (switch1State != digitalRead(SWITCH1_PIN)) {
  121.         lastSwitch1Change = millis();
  122.     }
  123.     // Debounce logic for switch 2
  124.     if (switch2State != digitalRead(SWITCH2_PIN)) {
  125.         lastSwitch2Change = millis();
  126.     }
  127.  
  128.     // Check switch 1 state
  129.     if (millis() - lastSwitch1Change > DEBOUNCE_DELAY) {
  130.         if (switch1State == HIGH && switch2State == LOW) {
  131.             // Switch 1 is pressed, toggle the system state
  132.             if (isSystemRunning) {
  133.                 stopSystem();
  134.             } else {
  135.                 backwashMode();
  136.             }
  137.         }
  138.     }
  139.     // Check switch 2 state
  140.     if (millis() - lastSwitch2Change > DEBOUNCE_DELAY) {
  141.         if (switch1State == LOW && switch2State == HIGH) {
  142.             // Switch 2 is pressed, toggle the system state
  143.             if (isSystemRunning) {
  144.                 stopSystem();
  145.             } else {
  146.                 normalMode();
  147.             }
  148.         }
  149.     }
  150. }
  151.  
  152. void backwashMode() {
  153.     isBackwashMode = true;
  154.     digitalWrite(svPins[0], HIGH); // SV1
  155.     digitalWrite(svPins[1], HIGH); // SV2
  156.     digitalWrite(pumpPin, HIGH); // Pump
  157.     digitalWrite(svPins[4], HIGH); // SV5
  158.     timerId = timer.setInterval(1000L, measurePressure);
  159.     isSystemRunning = true;
  160. }
  161.  
  162. void normalMode() {
  163.     isNormalMode = true;
  164.     digitalWrite(pumpPin, HIGH); // Pump
  165.     digitalWrite(svPins[3], HIGH); // SV4
  166.     digitalWrite(svPins[2], HIGH); // SV3
  167.     digitalWrite(svPins[5], HIGH); // SV6
  168.     digitalWrite(sv7Pin, HIGH); // SV7
  169.     ufWashStartTime = millis();
  170.     timerId = timer.setInterval(1000L, measurePressure);
  171.     isSystemRunning = true;
  172.  
  173.     // Turn off SV7 after 30 seconds
  174.     timer.setTimeout(30000L, []() {
  175.         digitalWrite(sv7Pin, LOW);
  176.     });
  177. }
  178.  
  179. void measurePressure() {
  180.     // Measure pressure and update Blynk
  181.     int sensorValue = analogRead(PRESSURE_SENSOR);
  182.     pressurePsi = (sensorValue / 4095.0) * 1.4 * 145.038;
  183.     Blynk.virtualWrite(V1, pressurePsi); // Update Blynk virtual pin
  184.  
  185.     // Update OLED display
  186.     static unsigned long lastDisplayUpdate = 0;
  187.     if (millis() - lastDisplayUpdate > 1000) { // Update display every 1 second
  188.         lastDisplayUpdate = millis();
  189.         display.clearDisplay();
  190.         display.setTextSize(1);
  191.         display.setTextColor(SSD1306_WHITE);
  192.         display.setCursor(0, 0);
  193.         display.println("Pressure: " + String(pressurePsi, 2) + " PSI");
  194.         if (isBackwashMode) {
  195.             display.println("Mode: Backwash");
  196.         } else if (isNormalMode) {
  197.             display.println("Mode: Normal");
  198.         }
  199.         display.display();
  200.     }
  201. }
  202.  
  203. void stopSystem() {
  204.     isSystemRunning = false;
  205.     digitalWrite(pumpPin, LOW); // Pump
  206.     for (int i = 0; i < 8; i++) {
  207.         digitalWrite(relayPins[i], LOW);
  208.     }
  209.     digitalWrite(sv7Pin, LOW);
  210.     timer.deleteTimer(timerId);
  211.     display.clearDisplay();
  212.     display.setTextSize(1);
  213.     display.setTextColor(SSD1306_WHITE);
  214.     display.setCursor(0, 0);
  215.     display.println("System Stopped");
  216.     display.display();
  217. }
  218.  
  219. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement