Issue with controlling multiple pumps with different sensors
Posted: Fri Jul 19, 2019 5:17 am
Good day!
I am using the NODEMCU ESP12-E module to control a water pump and 2 peristaltic pumps with via a relay. The water pump will only turn on once my ultrasonic sensor (HC-SR04) measures a certain level. Once a level is reached, the pump will go off again until the sensor picks up that there isn't enough water in tankA.
I have two peristaltic pumps that each pump pH nutrients according to the pH level of tank B. The pH level is measured with an analogue pH sensor.
When I add all the code together to operate simultaneously, everything goes haywire. The water pump turns on and off continuously and disregards the water level from the HCSR04. The PH Sensor also doesn't provide accurate readings and the peristaltic pumps also dont behave well.
Is there any way I can operate everything simultaneously or place each sensor on a time based queue without using a delay function?
my code is as follows:
Regards,
I am using the NODEMCU ESP12-E module to control a water pump and 2 peristaltic pumps with via a relay. The water pump will only turn on once my ultrasonic sensor (HC-SR04) measures a certain level. Once a level is reached, the pump will go off again until the sensor picks up that there isn't enough water in tankA.
I have two peristaltic pumps that each pump pH nutrients according to the pH level of tank B. The pH level is measured with an analogue pH sensor.
When I add all the code together to operate simultaneously, everything goes haywire. The water pump turns on and off continuously and disregards the water level from the HCSR04. The PH Sensor also doesn't provide accurate readings and the peristaltic pumps also dont behave well.
Is there any way I can operate everything simultaneously or place each sensor on a time based queue without using a delay function?
my code is as follows:
Code: Select all
#include <HCSR04.h>
#include "DHT.h"
#define TRIG 14
#define ECHO 12
#define DHTPIN 5 // Digital pin connected to the DHT sensor D1
#define DHTTYPE DHT11
#define Waterpump 0 //Waterpump relay2 D3 Orange
#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00 //deviation compensate
#define pHUp 13 // pHUP D7 relay4 Green
#define pHDown 2 // pHDown D4 relay3 Yellow
#define ArrayLenth 40 //times of collection
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
int pHArrayIndex=0;
float trigger_level=7.0;
DHT dht(DHTPIN,DHTTYPE);
UltraSonicDistanceSensor distanceSensor(TRIG, ECHO); // Initialize sensor that uses digital pins 13 and 12.
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
dht.begin();
///////////////SET pumps as outputs///////////
pinMode(pHUp,OUTPUT);
pinMode(pHDown,OUTPUT);
pinMode(Waterpump,OUTPUT);
/////////////INITIALLY HAVE THE PUMPS OFF///////////////
digitalWrite(pHUp,HIGH);
digitalWrite(pHDown,HIGH);
digitalWrite(Waterpump,HIGH);
}
void loop() {
static float pHValue,voltage;
pHArray[pHArrayIndex++]=analogRead(SensorPin);
if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
voltage = avergearray(pHArray, ArrayLenth)*3.3/1024;
pHValue = 3.5*voltage+Offset;
Serial.print(" pH : ");
Serial.println(pHValue,2);
float W = distanceSensor.measureDistanceCm();
if (pHValue <5.0)
{
digitalWrite(pHUp,LOW);
// delay(20);
// digitalWrite(pHUp,HIGH);
}
if(pHValue>7.5)
{
digitalWrite(pHDown,LOW);
//delay(20);
//digitalWrite(pHDown,HIGH);
}
if(pHValue>=5.0 || pHValue<=7.5)
{
digitalWrite(pHUp,HIGH);
digitalWrite(pHDown,HIGH);
}
Serial.print("Water level: ");
Serial.print(W);
float h=dht.readHumidity();
float t=dht.readTemperature();
if(W>trigger_level)
{
digitalWrite(Waterpump,LOW);
} // If the water level is less than 10cm from the sensor
if (W<4.5)
{
digitalWrite(Waterpump,HIGH); // If the water is >=10cm then we'll turn the pump off
}
}
double avergearray(int* arr, int number){
int i;
int max,min;
double avg;
long amount=0;
if(number<=0){
Serial.println("Error number for the array to avraging!/n");
return 0;
}
if(number<5){ //less than 5, calculated directly statistics
for(i=0;i<number;i++){
amount+=arr[i];
}
avg = amount/number;
return avg;
}else{
if(arr[0]<arr[1]){
min = arr[0];max=arr[1];
}
else{
min=arr[1];max=arr[0];
}
for(i=2;i<number;i++){
if(arr[i]<min){
amount+=min; //arr<min
min=arr[i];
}else {
if(arr[i]>max){
amount+=max; //arr>max
max=arr[i];
}else{
amount+=arr[i]; //min<=arr<=max
}
}//if
}//for
avg = (double)amount/(number-2);
}//if
return avg;
}
Regards,