Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill-in information from your Blynk Template here
- #define BLYNK_TEMPLATE_ID "TMPL3bTnPK_y"
- #define BLYNK_DEVICE_NAME "ZoneCommand"
- #define BLYNK_FIRMWARE_VERSION "0.1.9"
- #define BLYNK_PRINT Serial
- //#define BLYNK_DEBUG
- #define APP_DEBUG
- // Uncomment your board, or configure a custom board in Settings.h
- //#define USE_WROVER_BOARD
- //#define USE_TTGO_T7
- #include "BlynkEdgent.h"
- //Widget Setups
- WidgetLED led1(V4);
- WidgetLED led2(V5);
- WidgetLED led3(V6);
- WidgetLED led4(V7);
- WidgetLED led5(V8);
- WidgetLED led6(V9);
- WidgetLED led7(V10);
- const int ledArray[] = {V7,V8,V9,V10};
- int ledStatus[] = {0,0,0,0};
- // Set Pin Assignments For Output Pins
- // MOSFETS:
- const int output[] = {4,16,17,18};
- //Motor Drivers:
- //U4 Motor Driver:
- const int mtrOutA1 = 22;
- const int mtrOutA2 = 23;
- //U7 Motor Driver:
- const int mtrOutB1 = 19;
- const int mtrOutB2 = 21;
- //Set Pin Assignments for Input Pins
- //Analog Inputs:
- //Moisture Sensors:
- const int sensPin[] = {35,34,39,36};
- //Water Level Sensors:
- const int wtrLvlTop = 26;
- const int wtrLvlBtm = 27;
- //Zone Loop Controls
- int zoneNumber = 0;
- int blynkZone = 0;
- int triggerHigh[] = {30,30,30,30};
- int triggerLow[] = {70,70,70,70};
- bool zoneActive[] = {true, true, true, true};
- bool zoneManual[] = {false,false,false,false};
- bool zoneAuto[] = {true,true,true,true};
- // Sensor Data:
- int sensor[] = {0, 0, 0, 0}; // Sensor reading values array
- int topWtrLvl = 0;
- int btmWtrLvl = 0;
- int lowWater = 700;
- int highWater = 500;
- // Timer Variables
- const int manualDayTimer[] = {86400000, 86400000, 86400000, 86400000}; // delay for once a day manual watering mode
- unsigned long lastManualDayTimer[] = {0, 0, 0, 0,};
- unsigned long pumpTimer[] = {60000, 60000, 60000, 60000}; //Pump timers in array
- unsigned long lastPumpTimer[] = {0, 0, 0, 0}; // last value of Pump timers in array
- BlynkTimer timer;
- void setup()
- {
- // Set OUT Pins to Output
- pinMode(output[0], OUTPUT);
- pinMode(output[1], OUTPUT);
- pinMode(output[2], OUTPUT);
- pinMode(output[3], OUTPUT);
- // Set Sensor Pins to Input
- pinMode(sensPin[0], INPUT);
- pinMode(sensPin[1], INPUT);
- pinMode(sensPin[2], INPUT);
- pinMode(sensPin[3], INPUT);
- // set Water Level Pins to Input
- pinMode(wtrLvlTop, INPUT);
- pinMode(wtrLvlBtm, INPUT);
- //Blynk Timers
- BlynkEdgent.begin();
- timer.setInterval(500L,blynkData);
- timer.setInterval(1000L,waterLevel);
- //timer.setInterval(500, zoneWater);
- Serial.begin(115200);
- delay(100);
- }
- void sensorRead()
- {
- for (zoneNumber = 0; zoneNumber < 3; zoneNumber++) {
- if (zoneActive[zoneNumber]) {
- sensor[zoneNumber] = analogRead(sensPin[zoneNumber]);
- sensor[zoneNumber] = map(sensor[zoneNumber], 0, 4095, 0, 100);
- if (sensor[zoneNumber] > triggerLow[zoneNumber]) {
- zoneActive[zoneNumber] = false;
- digitalWrite(zoneActive[zoneNumber], HIGH);
- timer.setTimeout(60000, []()
- {
- zoneActive[zoneNumber] = true;
- digitalWrite(zoneActive[zoneNumber], LOW);
- });
- }
- Serial.println(sensor[zoneNumber]);
- }
- }
- for (zoneNumber = 3; zoneNumber > 0; zoneNumber = 0) {
- if (zoneActive[zoneNumber]) {
- sensor[zoneNumber] = analogRead(sensPin[zoneNumber]);
- sensor[zoneNumber] = map(sensor[zoneNumber], 0, 4095, 0, 100);
- if (sensor[zoneNumber] > triggerLow[zoneNumber]) {
- zoneActive[zoneNumber] = false;
- digitalWrite(zoneActive[zoneNumber], HIGH);
- timer.setTimeout(60000, []()
- {
- zoneActive[zoneNumber] = true;
- digitalWrite(zoneActive[zoneNumber], LOW);
- });
- }
- Serial.println(sensor[zoneNumber]);
- }
- }
- }
- void blynkData()
- {
- Blynk.virtualWrite(V0, sensor[0]);
- Blynk.virtualWrite(V1, sensor[1]);
- Blynk.virtualWrite(V2, sensor[2]);
- Blynk.virtualWrite(V3, sensor[3]);
- for (blynkZone = 0; blynkZone < 3; blynkZone++) {
- ledStatus[blynkZone] = digitalRead(output[blynkZone]);
- if (ledStatus[blynkZone] == HIGH) {
- Blynk.virtualWrite(ledArray[blynkZone], HIGH);
- }
- else {
- Blynk.virtualWrite(ledArray[blynkZone], LOW);
- }
- }
- for (blynkZone = 3; blynkZone > 0; blynkZone = 0) {
- ledStatus[blynkZone] = digitalRead(output[blynkZone]);
- if (ledStatus[blynkZone] == HIGH) {
- Blynk.virtualWrite(ledArray[blynkZone], HIGH);
- }
- else {
- Blynk.virtualWrite(ledArray[blynkZone], LOW);
- }
- }
- }
- void waterLevel()
- {
- int topWtrLvl = analogRead(wtrLvlTop);
- int btmWtrLvl = analogRead(wtrLvlBtm);
- if (btmWtrLvl > lowWater) {
- digitalWrite(mtrOutA1, HIGH);
- digitalWrite(mtrOutA2, LOW);
- led1.on();
- led2.on();
- }
- if (topWtrLvl < highWater) {
- digitalWrite(mtrOutA1, LOW);
- digitalWrite(mtrOutA2, LOW);
- led1.off();
- led2.off();
- led3.on();
- }
- }
- void loop() {
- BlynkEdgent.run();
- timer.run();
- sensorRead();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement