Hi, i am stuck on one aspect of my code. The project is an alternative to a soil moisture sensor setup, meaning there isn’t one used, instead the pump is a dual manual or on a timer only each day, also it is tagged with other devices which works perfectly with my code, i also am trying to incorporate the hc-sr04 for a water level alarm, but the way i have the code in my sketch only runs once with a result depending on which variable was met, can somebody please help with the missing code to enable this to work. so it’s only for the alarmpin part of the sketch to work as the rest works perfectly.
just to clarify that i am using an active low relay.
i am also using OTA which works fine too.
#define BLYNK_PRINT Serial
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define echoPin 4 // Echo Pin white wire
#define trigPin 5 // Trigger Pin blue wire
const int pump = 12;
const int alarmPin = 13;
const int waterLevelThreshold = 4;
char server[] = "*************";
char ssid[] = "*************";
char pass[] = "*************";
char auth[] = "*********************";
void setup()
{
pinMode(pump, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(alarmPin, OUTPUT);
digitalWrite(pump, HIGH); // LED OFF on bootup
digitalWrite(alarmPin, LOW); // LED OFF on bootup
Serial.begin(115200);
Blynk.begin(auth, ssid, pass, server);
ArduinoOTA.setHostname("*************"); // OPTIONAL ArduinoOTA.begin();
ArduinoOTA.begin();
Blynk.virtualWrite(V1, 1); // button set to OFF on bootup
long waterLevel;
waterLevel = WaterLevel();
}
long WaterLevel()
{
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance <=12)
{
digitalWrite(alarmPin, LOW);
}
if (distance >= 13){
digitalWrite(alarmPin, HIGH);
}
return distance;
delay(2000);
}
// WeMos on board LED is active LOW
BLYNK_WRITE(V0){ // Timer widget
if (param.asInt() == 0) {
Blynk.virtualWrite(V1, 0); // button set to ON from timer
Serial.println("Timer ON activated");
}
else{
Blynk.virtualWrite(V1, 1); // button set to OFF from timer
Serial.println("Timer OFF activated");
}
Blynk.syncVirtual(V1); // sync virtualWrite 0 / 1 to button widget
}
BLYNK_WRITE(V1){ // Button widget in SWITCH mode
if (param.asInt() == 0) {
digitalWrite(pump, LOW);
Serial.println("LED ON");
}
else{
digitalWrite(pump, HIGH);
Serial.println("LED OFF");
}
}
BLYNK_WRITE(V2){ // Timer widget
if (param.asInt() == 0) {
Blynk.virtualWrite(V0, 0); // button set to ON from timer
Serial.println("Timer ON activated");
}
else{
Blynk.virtualWrite(V0, 1); // button set to OFF from timer
Serial.println("Timer OFF activated");
}
Blynk.syncVirtual(V0); // sync virtualWrite 0 / 1 to button widget
Blynk.syncVirtual(V1); // sync virtualWrite 0 / 1 to button widget
{
}
}
void loop()
{
Blynk.run();
ArduinoOTA.handle();
}