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: **Smart Control**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-12-14 19:24:37
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The system shall initialize components in the */
- /* setup function and execute the main logic */
- /* continuously in the loop function, ensuring */
- /* efficient resource management and responsiveness */
- /* to connected components. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Arduino.h>
- #include <Wire.h>
- #include <Adafruit_SSD1306.h>
- #include <Adafruit_GFX.h>
- #include <BlynkSimpleEsp32.h>
- #include <BlynkTimer.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void backwashMode();
- void normalMode();
- void measurePressure();
- void stopSystem();
- /****** BLYNK CREDENTIALS *****/
- #define BLYNK_TEMPLATE_ID "TMPL3GbuIMEg9"
- #define BLYNK_TEMPLATE_NAME "motor"
- #define BLYNK_AUTH_TOKEN "WfLmKbPWmBlpx0un2R2eufubQ-VStAym"
- // Wi-Fi credentials
- char ssid[] = "ME4";
- char pass[] = "********"; // Use your actual password
- // Pin definitions
- const int SWITCH1_PIN = 13; // Use pin 13 for switch 1
- const int SWITCH2_PIN = 12; // Use pin 12 for switch 2
- const int PRESSURE_SENSOR = 34; // Analog pin for pressure sensor
- // Relay control pins
- const int relayPins[] = {32, 33, 27, 14, 5, 18, 19, 23};
- const int svPins[] = {32, 33, 14, 5, 18, 19, 23};
- const int pumpPin = 27;
- const int sv7Pin = 19;
- // Variables
- bool isSystemRunning = false;
- bool isBackwashMode = false;
- bool isNormalMode = false;
- unsigned long ufWashStartTime;
- int timerId;
- float pressurePsi;
- // OLED display
- Adafruit_SSD1306 display(128, 64, &Wire, -1);
- BlynkTimer timer;
- const int DEBOUNCE_DELAY = 50; // Adjust this value as needed
- void setup(void)
- {
- Serial.begin(9600); // Initialize serial communication
- Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // Initialize Blynk
- // Initialize pins
- pinMode(SWITCH1_PIN, INPUT);
- pinMode(SWITCH2_PIN, INPUT);
- for (int i = 0; i < 8; i++) {
- pinMode(relayPins[i], OUTPUT);
- digitalWrite(relayPins[i], LOW); // Initialize relays to LOW
- }
- pinMode(sv7Pin, OUTPUT);
- digitalWrite(sv7Pin, LOW);
- // Initialize OLED display
- if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
- Serial.println(F("OLED init failed!"));
- while (1); // Loop forever if OLED initialization fails
- }
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0, 0);
- display.println("System Idle");
- display.display();
- }
- void loop(void)
- {
- Blynk.run(); // Run Blynk tasks
- // Read switch states
- int switch1State = digitalRead(SWITCH1_PIN);
- int switch2State = digitalRead(SWITCH2_PIN);
- Serial.println("Switch 1 State: " + String(switch1State));
- Serial.println("Switch 2 State: " + String(switch2State));
- static unsigned long lastSwitch1Change = 0;
- static unsigned long lastSwitch2Change = 0;
- // Debounce logic for switch 1
- if (switch1State != digitalRead(SWITCH1_PIN)) {
- lastSwitch1Change = millis();
- }
- // Debounce logic for switch 2
- if (switch2State != digitalRead(SWITCH2_PIN)) {
- lastSwitch2Change = millis();
- }
- // Check switch 1 state
- if (millis() - lastSwitch1Change > DEBOUNCE_DELAY) {
- if (switch1State == HIGH && switch2State == LOW) {
- // Switch 1 is pressed, toggle the system state
- if (isSystemRunning) {
- stopSystem();
- } else {
- backwashMode();
- }
- }
- }
- // Check switch 2 state
- if (millis() - lastSwitch2Change > DEBOUNCE_DELAY) {
- if (switch1State == LOW && switch2State == HIGH) {
- // Switch 2 is pressed, toggle the system state
- if (isSystemRunning) {
- stopSystem();
- } else {
- normalMode();
- }
- }
- }
- }
- void backwashMode() {
- isBackwashMode = true;
- digitalWrite(svPins[0], HIGH); // SV1
- digitalWrite(svPins[1], HIGH); // SV2
- digitalWrite(pumpPin, HIGH); // Pump
- digitalWrite(svPins[4], HIGH); // SV5
- timerId = timer.setInterval(1000L, measurePressure);
- isSystemRunning = true;
- }
- void normalMode() {
- isNormalMode = true;
- digitalWrite(pumpPin, HIGH); // Pump
- digitalWrite(svPins[3], HIGH); // SV4
- digitalWrite(svPins[2], HIGH); // SV3
- digitalWrite(svPins[5], HIGH); // SV6
- digitalWrite(sv7Pin, HIGH); // SV7
- ufWashStartTime = millis();
- timerId = timer.setInterval(1000L, measurePressure);
- isSystemRunning = true;
- // Turn off SV7 after 30 seconds
- timer.setTimeout(30000L, []() {
- digitalWrite(sv7Pin, LOW);
- });
- }
- void measurePressure() {
- // Measure pressure and update Blynk
- int sensorValue = analogRead(PRESSURE_SENSOR);
- pressurePsi = (sensorValue / 4095.0) * 1.4 * 145.038;
- Blynk.virtualWrite(V1, pressurePsi); // Update Blynk virtual pin
- // Update OLED display
- static unsigned long lastDisplayUpdate = 0;
- if (millis() - lastDisplayUpdate > 1000) { // Update display every 1 second
- lastDisplayUpdate = millis();
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0, 0);
- display.println("Pressure: " + String(pressurePsi, 2) + " PSI");
- if (isBackwashMode) {
- display.println("Mode: Backwash");
- } else if (isNormalMode) {
- display.println("Mode: Normal");
- }
- display.display();
- }
- }
- void stopSystem() {
- isSystemRunning = false;
- digitalWrite(pumpPin, LOW); // Pump
- for (int i = 0; i < 8; i++) {
- digitalWrite(relayPins[i], LOW);
- }
- digitalWrite(sv7Pin, LOW);
- timer.deleteTimer(timerId);
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0, 0);
- display.println("System Stopped");
- display.display();
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement