I have 3 ultrasonic sensors in 3 tanks and an arduino mega and an 8 channel relay board I need each tank to be filled a certain point and then shut off and only by going to a specific point it will start so that the pumps are constantly on and off.
this is the full code I have written, how does it look.
evanmars:
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;
void setup() {
Serial.begin (9600);
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;
Serial.print ( "SOURCEPUMP ");
Serial.print ( distance1);
delay(1000);
if(distanceInch1 > 50) {
digitalWrite(sourcepump1, HIGH);
digitalWrite(uvlamp1, HIGH); }
if (distanceInch1 <= 12) {
digitalWrite(sourcepump1, LOW);
digitalWrite(uvlamp1, LOW);
if(distanceInch1 > 50) {
digitalWrite(sourcepump2, HIGH);
digitalWrite(uvlamp2, HIGH);
if (distanceInch1 <= 12) {
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;
Serial.print ( "TRANSFER PUMP ");
Serial.print ( distance2);
delay(1000);
if(distanceInch2 > 50) {
digitalWrite(transferpump1, HIGH);
if (distanceInch2 <= 12) {
digitalWrite(transferpump1, LOW);
if(distanceInch2 > 50) {
digitalWrite(transferpump2, HIGH);
if (distanceInch2 <= 12) {
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;
Serial.print ( "OZONE TANK ");
Serial.print ( distance3);
delay(1000);
if(distanceInch3 > 50) {
digitalWrite(ozonepump, HIGH);
if (distanceInch3 <= 12) {
digitalWrite(ozonepump, LOW);
}}}}}}}}}}