PUMP AUTOMATION

I have a pump project I am trying to automate them using arduino mega 2560.

I have 5 pumps and 2 UV systems to control and I have 3 ultra sonic sensors (Hc-Sr04) and 4 1000 gallon water tanks.

the sensors are already installed to each tank, In one area I have the tanks parallel so only 1 tank in that area has a sensor.

The code I wrote seems to only be reading 1 sensor and its fluctuating with a wide range I have it set to iches and I have a fluctuation of 19 to 89 inches displaying on my serial print.

due to limited materials now I have 3 pumps in one area that Ill just use 1 contactor to control all 3 pumps for now. (ozone pumps) pulling from the river tank to fill the ozone tank.

2 River pumps, 2 UV systems, to one tank with 1 sensor.

2 transfer pumps from ozone tank to filling area tanks (2 tanks 1 sensor)

I am not sure if there is an issue with my code but I have a link to it, could someone help me to check for faults.

const int trigPin1=2;
const int echoPin1= 3;
const int trigPin2 =4;
const int echoPin2= 5;
const int trigPin3 =6;
const int echoPin3 =7;

#define sourcepump1 9
#define sourcepump2 10
#define transferpump1 29
#define transferpump2 33
#define ozonepump 11
#define uvlamp1 13
#define uvlamp2 12

long duration;
float distanceInch1;
float distanceInch2;
float distanceInch3;
const int maxDistant = 50;
const int minDistant = 12;


void setup() {
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
   pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
   pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(uvlamp1, OUTPUT);
  pinMode(uvlamp2, OUTPUT);
  pinMode(ozonepump, OUTPUT);
  pinMode(transferpump1, OUTPUT);
  pinMode(transferpump2, OUTPUT);
  pinMode(sourcepump1, OUTPUT);
  pinMode(sourcepump2, OUTPUT);
}

void loop() {
  long duration1, distance1;
  digitalWrite(trigPin1, LOW);  // Added this line
  delayMicroseconds(5); // Added this line
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distanceInch1 = (duration1/2) / 74;
}


if(distanceInch1  > maxDistant) {
  digitalWrite(sourcepump1, HIGH);
  digitalWrite(uvlamp1, HIGH);
  }
if (distanceInch1  <= minDistant)
{
  digitalWrite(sourcepump1, LOW);
  digitalWrite(uvlamp1, LOW);
}

if(distanceInch1  > maxDistant) 
{
  digitalWrite(sourcepump2, HIGH);
  digitalWrite(uvlamp2, HIGH); 
}
if (distanceInch1  <= minDistant) 
{
  digitalWrite(sourcepump2, LOW);
  digitalWrite(uvlamp2, LOW);
}

long duration2, distance2;
  digitalWrite(trigPin2, LOW);  // Added this line
  delayMicroseconds(5); // Added this line
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distanceInch2= (duration2/2) / 74;


if(distanceInch2  > maxDistant)
{
  digitalWrite(transferpump1, HIGH);
  digitalWrite(transferpump2, HIGH);
}
if (distanceInch2  <= minDistant) 
{
  digitalWrite(transferpump1, LOW);
  digitalWrite(transferpump2, LOW);
}

 long duration3, distance3;
  digitalWrite(trigPin3, LOW);  // Added this line
  delayMicroseconds(5); // Added this line
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distanceInch3= (duration3/2) / 74;


if (distanceInch3 > maxDistant){
  digitalWrite(ozonepump, HIGH);}
  
if (distanceInch3  <= minDistant) {
  digitalWrite(ozonepump, LOW);
  }

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Use the autoformat tool to indent the code and you should see that the loop() function is closed with a } at the wrong place leaving the rest of the code outside of any function.

Most people won't follow an external link, due to the bother and the security risk. Please post your code inside your sketch, framed in code tags as explained in the sticky threads at the top of the forum. If the code is too big to fit, add it as an attachment.

I have a pump project I am trying to automate them using arduino mega 2560.

I have 5 pumps and 2 UV systems to control and I have 3 ultra sonic sensors (Hc-Sr04) and 4 1000 gallon water tanks.

the sensors are already installed to each tank, In one area I have the tanks parallel so only 1 tank in that area has a sensor.

The code I wrote seems to only be reading 1 sensor and its fluctuating with a wide range I have it set to iches and I have a fluctuation of 19 to 89 inches displaying on my serial print.

due to limited materials now I have 3 pumps in one area that Ill just use 1 contactor to control all 3 pumps for now. (ozone pumps) pulling from the river tank to fill the ozone tank.

2 River pumps, 2 UV systems, to one tank with 1 sensor.

2 transfer pumps from ozone tank to filling area tanks (2 tanks 1 sensor)

I am not sure if there is an issue with my code but I have a link to it, could someone help me to check for faults.

const int trigPin1=2;
const int echoPin1= 3;
const int trigPin2 =4;
const int echoPin2= 5;
const int trigPin3 =6;
const int echoPin3 =7;

#define sourcepump1 9
#define sourcepump2 10
#define transferpump1 29
#define transferpump2 33
#define ozonepump 11
#define uvlamp1 13
#define uvlamp2 12

long duration;
float distanceInch1;
float distanceInch2;
float distanceInch3;
const int maxDistant = 50;
const int minDistant = 12;


void setup() {
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
   pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
   pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(uvlamp1, OUTPUT);
  pinMode(uvlamp2, OUTPUT);
  pinMode(ozonepump, OUTPUT);
  pinMode(transferpump1, OUTPUT);
  pinMode(transferpump2, OUTPUT);
  pinMode(sourcepump1, OUTPUT);
  pinMode(sourcepump2, OUTPUT);
  Serial.begin (9600);
}

void loop() {
  long duration1, distance1;
  digitalWrite(trigPin1, LOW);  // Added this line
  delayMicroseconds(5); // Added this line
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distanceInch1 = (duration1/2) / 74;
}


if(distanceInch1  > maxDistant) {
  digitalWrite(sourcepump1, HIGH);
  digitalWrite(uvlamp1, HIGH);
  }
if (distanceInch1  <= minDistant)
{
  digitalWrite(sourcepump1, LOW);
  digitalWrite(uvlamp1, LOW);
}

if(distanceInch1  > maxDistant) 
{
  digitalWrite(sourcepump2, HIGH);
  digitalWrite(uvlamp2, HIGH); 
}
if (distanceInch1  <= minDistant) 
{
  digitalWrite(sourcepump2, LOW);
  digitalWrite(uvlamp2, LOW);
}

long duration2, distance2;
  digitalWrite(trigPin2, LOW);  // Added this line
  delayMicroseconds(5); // Added this line
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distanceInch2= (duration2/2) / 74;


if(distanceInch2  > maxDistant)
{
  digitalWrite(transferpump1, HIGH);
  digitalWrite(transferpump2, HIGH);
}
if (distanceInch2  <= minDistant) 
{
  digitalWrite(transferpump1, LOW);
  digitalWrite(transferpump2, LOW);
}

 long duration3, distance3;
  digitalWrite(trigPin3, LOW);  // Added this line
  delayMicroseconds(5); // Added this line
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distanceInch3= (duration3/2) / 74;


if (distanceInch3 > maxDistant){
  digitalWrite(ozonepump, HIGH);}
  {
if (distanceInch3  <= minDistant) 
  digitalWrite(ozonepump, LOW);}

LOL

I liked his last line of code

}}}}}}}}}}

reminds me of LISP.

a7

That code is incomplete

I realize there is an error in it but I have been follow the same sequence all the way down so I am not sure whats wrong.

You've got a ton of code that isn't in a function.

@enzyb

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

I realize there is an error in it but I have been follow the same sequence all the way down so I am not sure whats wrong.

I told you one thing that is wrong in reply#2, but you have done nothing to fix it.

I made a new post and it seems to be deleted so not sure what to do

I am now just realizing that the post was moved into this post, also I did what you said and that issue is fixed, thanks for that.

const int trigPin1 = 2;
const int echoPin1 = 3;
const int trigPin2 = 4;
const int echoPin2 = 5;
const int trigPin3 = 6;
const int echoPin3 = 7;

#define sourcepump1 9
#define sourcepump2 10
#define transferpump1 29
#define transferpump2 33
#define ozonepump 11
#define uvlamp1 13
#define uvlamp2 12

long duration;
float distanceInch1;
float distanceInch2;
float distanceInch3;
const int maxDistant = 50;
const int minDistant = 12;


void setup() {
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(uvlamp1, OUTPUT);
  pinMode(uvlamp2, OUTPUT);
  pinMode(ozonepump, OUTPUT);
  pinMode(transferpump1, OUTPUT);
  pinMode(transferpump2, OUTPUT);
  pinMode(sourcepump1, OUTPUT);
  pinMode(sourcepump2, OUTPUT);
}

void loop() {
  long duration1, distance1;
  digitalWrite(trigPin1, LOW);  // Added this line
  delayMicroseconds(5); // Added this line
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distanceInch1 = (duration1 / 2) / 74;


if (distanceInch1  > maxDistant) {
  digitalWrite(sourcepump1, HIGH);
  digitalWrite(uvlamp1, HIGH);
}
if (distanceInch1  <= minDistant)
{
  digitalWrite(sourcepump1, LOW);
  digitalWrite(uvlamp1, LOW);
}

if (distanceInch1  > maxDistant)
{
  digitalWrite(sourcepump2, HIGH);
  digitalWrite(uvlamp2, HIGH);
}
if (distanceInch1  <= minDistant)
{
  digitalWrite(sourcepump2, LOW);
  digitalWrite(uvlamp2, LOW);
}

long duration2, distance2;
digitalWrite(trigPin2, LOW);  // Added this line
delayMicroseconds(5); // Added this line
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distanceInch2 = (duration2 / 2) / 74;


if (distanceInch2  > maxDistant)
{
  digitalWrite(transferpump1, HIGH);
  digitalWrite(transferpump2, HIGH);
}
if (distanceInch2  <= minDistant)
{
  digitalWrite(transferpump1, LOW);
  digitalWrite(transferpump2, LOW);
}

long duration3, distance3;
digitalWrite(trigPin3, LOW);  // Added this line
delayMicroseconds(5); // Added this line
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin3, LOW);
duration3 = pulseIn(echoPin3, HIGH);
distanceInch3 = (duration3 / 2) / 74;


if (distanceInch3 > maxDistant) {
  digitalWrite(ozonepump, HIGH);
}

if (distanceInch3  <= minDistant) {
  digitalWrite(ozonepump, LOW);
}
}

overall how does this code look, is it practical for the setup I have explained?

I am curious how much testing you did as you wrote the program? OR did you wait until now to think about testing?

Paul

Hi enzyb,

building an elecronic system from scratch is something completely different than pluggin in a external harddisc or a pprinter or installing an app on a smartphone.
The things mentioned above are highly standardised with very high quality-standards that assure flawlessly functioning by just pluggin in.

Building up such a system with ultrasonic sensors where you have to measure runtime of the signal yourself
requires a lot of basic testing. So if you have installed the ultrasonic sensor already in the tank just pull them out for initial testing on the bench.

For the logic of your code. What microcontroller board are you using? Does it offer an analog-input?

If yes you could simulate the distance-sensor by a potentiometer to see if the rest is working as expected.
maybe switching on off the pumps is not good for testing. So I would connect low-current-LEDs with a matching current-limiting resistor to the IO-Pins to see the the logical state of the IO-pins.
Or adding Serial-Output which gets sendet once every 5 seconds using non-delaying timer by using the function millis()

best regards

Stefan

I have looked up a datasheet of this sensor
the datasheet explains:

the triggerinput needs an falling edge to start a measuring.
The minimal time is 10 microseconds and could be as long as 20 milliseconds as only after 20 milliseconds the next measuring can start.

So I would set the pulseduration to at least 20 microseconds or better to 20 milliseconds to avoid re-triggering before the device is ready for the next measuring.

After triggering after additional 250 microseconds the sensor creates an ultrasonic-pulse which is 200 microseconds long. Then the echo-pin goes immediately high and the duration of this high-pulse is equal to the runtime. So a pulseIn(pinno,high seems to be right)

I guess testing the measuring will be much easier on the bench than inside the tank.

best regards

Stefan

Good day I actually did testing outside the tank before but only with 1 sensor as the project was for 1 tank at the time, after testing then installing I had issues with the sensor in the tank and then within that same week they bought more tanks and I had 2 more sensors so I just connected them to the same board since I was using a mega.

The code worked fine outside the tank.`

Not really followed this topic but with the U/S sensors consider spread factors / beam width / atmosphere inside the tank density (condensation etc)

Just a passing thought.