Push button relay control

Hi, I will try to not have much pain in the butt. So what I'm trying to do is simple except I can not figure out the best way to do it. Im working on an art project and i need to turn a relay on
I need help. I took a project that has me just a little confused on how to combine all the different functions I need to happen. I can make them all happen separately but combining them has thrown me for a loop. I want the push button to activate the relay and the relay will turn itself off when the water is full.
Thank you for reading and helping.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#define echopin 9 // PIN HC
#define trigpin 8 // PIN HC
const int button = 2;
const int relay = 13;
int buttonState = 0;
int maximumRange = 150;
long duration, distance;
void setup()
{
lcd.init();
lcd.init();
lcd.backlight();
lcd.begin(20,4);
Serial.begin (9600);
pinMode (trigpin, OUTPUT);
pinMode (echopin, INPUT );
pinMode (4, OUTPUT);
pinMode (13,OUTPUT);

pinMode(relay, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button, INPUT_PULLUP);
}

void loop ()
{
{

digitalWrite(trigpin,LOW);
delayMicroseconds(2);

digitalWrite(trigpin,HIGH);
delayMicroseconds(10);

duration=pulseIn (echopin,HIGH);

distance= duration/58.2;
delay (50);
Serial.println(distance);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("AUTO PROCESS SYSTEMS");
lcd.setCursor(0,1);
lcd.print("--------------------");
lcd.setCursor(1,2);
lcd.print("Current Level: ");
lcd.print(distance);
delay(100);

}

if (distance >= 25 ){
digitalWrite (7,LOW);// connect to relay(motor)
digitalWrite (13,LOW);
lcd.setCursor(2,3);
lcd.print("Motor Started!!!");
delay(100);
}

else if (distance <=5) {
digitalWrite (7,HIGH); // connect to relay(motor)
digitalWrite (13,HIGH);
lcd.setCursor(3,3);
lcd.print("Tank is FULL!!");
delay(100);

}
buttonState = digitalRead(button);
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW:
if (buttonState == LOW) {
digitalWrite(relay, LOW);
delay (1000);
} else {
digitalWrite(relay, HIGH);
}
}

Hi, I will try to not have much pain in the butt.

You can have as much pain in the butt as you want. We'd prefer that you not BE a pain in the butt. Not being a pain in the butt means posting code properly.

It means not littering your code with useless curly braces and excess white space.

It means explaining what the code you posted actually does, and how that differs from what you expect.

It means NOT using delay().

When the button is pressed, you want the relay to latch on and stay on until about the time the message "Tank is FULL!!" appears on the LCD. Is that it ?

6v6gt:
When the button is pressed, you want the relay to latch on and stay on until about the time the message "Tank is FULL!!" appears on the LCD. Is that it ?

Yes. How does it work?

You could try replacing your button/relay handling logic in the loop with something like:

  static boolean relayOn = false ;
  if ( relayOn == false && digitalRead(button) == LOW ) {   // assuming button when pressed == LOW (could be de-bounced)
     relayOn = true ;
  }
  if ( distance <= 5 ) {  // maybe this could be moved to your exising distance handling logic
    relayOn = false ;
  } 
  if ( relayOn == true ) {
     // switch relay on
     digitalWrite(relay, HIGH); 
  }
  else {
    // switch relay off
    digitalWrite(relay, LOW);
  }

You also appear to be doing something with pin 7 and a relay, although pin 7 is not defined as an output pin with pinMode(). What you intend here is not clear.

Thanks for help.

Same my code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#define echopin 9 // PIN HC
#define trigpin 8 // PIN HC
const int button = 2;
const int relay = 13;
int buttonState = 0;
int maximumRange = 150;
long duration, distance;
void setup()
{
lcd.init();
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Design by GioLangLe!");
lcd.setCursor(0,1);
lcd.print("WWW.GIOLANGLE.COM");
lcd.setCursor(0,2);
lcd.print("Publisher : 06/2017");
lcd.setCursor(0,3);
lcd.print("Version : 0.02A");
lcd.begin(20,4);
Serial.begin (9600);
pinMode (trigpin, OUTPUT);
pinMode (echopin, INPUT );
pinMode (4, OUTPUT);
pinMode (13,OUTPUT);

pinMode(relay, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button, INPUT_PULLUP);
}

void loop ()
{
{

digitalWrite(trigpin,LOW);
delayMicroseconds(2);

digitalWrite(trigpin,HIGH);
delayMicroseconds(10);

duration=pulseIn (echopin,HIGH);

distance= duration/58.2;
delay (50);
Serial.println(distance);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("AUTO PROCESS SYSTEMS");
lcd.setCursor(0,1);
lcd.print("--------------------");
lcd.setCursor(1,2);
lcd.print("Current Level: ");
lcd.print(distance);
// delay(100);

}

static boolean relayOn = false ;
if ( relayOn == false && digitalRead(button) == HIGH ) { // assuming button when pressed == LOW (could be de-bounced)
relayOn = true ;
}
if ( distance <= 5 ) { // maybe this could be moved to your exising distance handling logic
relayOn = false ;
}
if ( distance >= 100 ) { // maybe this could be moved to your exising distance handling logic
relayOn = true ;
}
if ( relayOn == true ) {
// switch relay on
digitalWrite(relay, HIGH);
lcd.setCursor(3,3);
lcd.print("Motor Started!!!");
}
else {
// switch relay off
digitalWrite(relay, LOW);
lcd.setCursor(2,3);
lcd.print("Tank is FULL!!");

}

}

Relay stop when distance <= 5
Relay activated when distance >= 100
And when distance is ~ 105, the button is pressed, the relay will operate and stop at <= 5

In the source code above error somewhere. You can help me, and tell me that.

Please use code tags (NOT quote tags) when posting code.

Remove this section of code then test it and say how the sketch differs from your requirements:

  if ( distance >= 100 ) {  // maybe this could be moved to your exising distance handling logic
    relayOn = true ;
  }