Repeating 3x part of the code

Hi,
I need a help as I am already scrolling a lot of posts already I can not find my project to work.
I have a basic project with a ultrasonic water level monitor and pump running water from 1 tank below to 2nd tank on top of each other. Water level in top tank hit some level than pump is turned off and water slowly drips down to lower tank until sensor in the top tank reads low water level and pump is ON again.
Basically i want this process to repeat 3 times and then I need pump to work for a bit longer to reach highest level in the top tank and then pump is switched off and LCD shows ALARM, WATER TANK OVERFLOW.
I have tried couple different ways with this code:
for (i = 0; i < n; i++)
but it does not work for me.
I need this process: if(distance<6) , else if(distance>8) to be repeated 3 times and then last else if(distance>4) would finish my program.

#include <LiquidCrystal.h>
#define trigger 9
#define echo 8
#define motor A0

#include <SoftwareSerial.h>

SoftwareSerial BTserial(7, 6); // RX | TX

LiquidCrystal lcd(12,11,5,4,3,2);
float time=0,distance=0;
void setup()

{

lcd.begin(16,2);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
pinMode(motor, OUTPUT);
lcd.print(" Water Level “);
lcd.setCursor(0,1);
lcd.print(” Indicator ");
delay(2000);
BTserial.begin(9600);
}
void loop()
{
lcd.clear();
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
lcd.clear();
lcd.print("Water Space In ");
lcd.setCursor(0,1);
lcd.print("Tank is: ");
lcd.print(distance);
lcd.print(“Cm”); delay(2000);

if(distance<6)
{
digitalWrite(motor, LOW);
lcd.clear();
lcd.print("Water Tank Full ");
lcd.setCursor(0,1);
lcd.print(“Motor Turned OFF”);
delay(2000);
}
else if(distance>8)
{
digitalWrite(motor, HIGH);
lcd.clear();
lcd.print(“LOW Water Level”);
lcd.setCursor(0,1);
lcd.print(“Motor Turned ON”);
delay(2000);

}

else if(distance<4)
{
digitalWrite(motor, LOW);
lcd.clear();
lcd.print(“ALARM TANK OVERFLOW”);
lcd.setCursor(0,1);
lcd.print(“Motor Turned OFF”);
delay(2000);

}
{

BTserial.print(“1234”);

BTserial.print(",");

delay(20);

}
}

Anybody can help mi this one?
Peter

project_Arduino.ino (1.6 KB)

there is a “8” under the smily

your code “logic” does not seem ok.
for example let say distanse =3 . the “if (distanse <6)” will run BUT “else if(distance<4)” will NOT run

I suggest change “else if(distance<4)” to “if(distance<4)”

then it is easy to see that you can :

for (int HowManyTimes =1;HowManyTimes<4lHowManyTimes++)
{
 // your code for "<6" and ">8"
}

if(distance<4) 
{
 //your code for "<4"
}

I also suggest you do a “tools/autoformat” so to see easier the logic blocks of the sketch

Spare a minute and read the “Read this before posting a programming question …” in the main page of “Programming Questions”

Welcome to the Forum. In addition to formatting your code properly, please edit the post to place the code inside code tags. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

Thanks a lot for a quick reply, appreciate it. I will do my best to keep up with a forum standard.

Peter

Thanks a lot GRuser,
I dig deeper and deeper that is my first couple weeks playing with Arduino.

I have implemented suggested code, just changed “HowManyTimes” to “i” for short.

I have spotted couple issues and cleaned code a bit but still is not really working the way I would want it to.

As I mentioned above I want it to start filling upper tank up to the (distance < 6), then it would slowly
drip to reach level (distance > 8) keep this cycle in the loop 3 times and then move to the next step which is PUMP ON until reach (distance < 4) and stop.

I have it tested but it does not keep the pump ON in 4th cycle just stops when it reaches (distance <6)

Improved code below.

I will be grateful for helping me out.

Thanks

#include <LiquidCrystal.h>

#define trigger 9
#define echo 8
#define pump A0


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
float time = 0, distance = 0;
void setup()

{

  lcd.begin(16, 2);
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(pump, OUTPUT);
  lcd.print("  Water Level ");
  lcd.setCursor(0, 1);
  lcd.print("   Indicator  ");
  delay(2000);
  digitalWrite(pump, HIGH);
 


}
void loop()
{
  lcd.clear();
  digitalWrite(trigger, LOW);
  delayMicroseconds(2);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);
  delayMicroseconds(2);
  time = pulseIn(echo, HIGH);
  distance = time * 340 / 20000;
  lcd.clear();
  lcd.print("Water Space In  ");
  lcd.setCursor(0, 1);
  lcd.print("Tank is: ");
  lcd.print(distance);
  lcd.print("Cm"); delay(2000);



  for (int i = 1; i < 4; i++)
  {

    if (distance < 6)
    {
      digitalWrite(pump, LOW);
      lcd.clear();
      lcd.print("Water Tank Full ");
      lcd.setCursor(0, 1);
      lcd.print("Motor Turned OFF");
      delay(1000);
    }

    else if (distance > 8)

    {
      digitalWrite(pump, HIGH);
      lcd.clear();
      lcd.print("LOW Water Level");
      lcd.setCursor(0, 1);
      lcd.print("Motor Turned ON");
      delay(1000);
    }
  }

  digitalWrite(pump, HIGH);

  if (distance < 4)

  {
    digitalWrite(pump, LOW);
    lcd.clear();
    lcd.print("ALARM ");
    lcd.setCursor(0, 1);
    lcd.print("");
    delay(1000);
  }
  delay(10000);

}