Automatic watering system with LCD Display

I am trying to make a automatic watering system using a soil moisture sensor. When it drops below a certain point activates a pump. While showing the soil moisture on a LCD display.
So far I have managed to successfully get the LCD display working without the pump and relay connected.
I have also go the pump and relay working without the LCD display.
I have tried to combine both codes into one, it works fine until the pump is activated.
When the pump is activated the LCD has an E#R# on the bottom right hand section of the LCD and doesn't update.
Can anyone help me with the code, I think it might be something simple that I am missing seeing as I am a real amateur and self taught using google.

//#include <LiquidCrystal.h> unquote this for LCD display without I2c
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#include <LiquidCrystal.h>
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int j=0;
int prev=0;
int pres=0;
int motorPin = 8; // pin that turns on the motor
int blinkPin = 13; // pin that turns on the LED
int watertime = 5; // how long it will be watering (in seconds)
int waittime = 1; // how long to wait between watering (in minutes)

void setup()
{
pinMode(motorPin, OUTPUT); // set Pin 8 to an output
pinMode(blinkPin, OUTPUT); // set pin 13 to an output
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print(" Soil Moisture ");
Serial.begin(9600);
}

void loop()
{
j=analogRead(A0);
j=map(j,0,982,148,0);
pres=j;
if(j>100)
j=100;
else if(j<0)
j=0;
lcd.setCursor(6,1);
lcd.print(j);
lcd.print("% ");
Serial.println(j);

prev=j;
delay(500);

int moisturePin = analogRead(A0); //read analog value of moisture sensor
int moisture = ( 100 - ( (moisturePin / 1023.00) * 100 ) ); //convert analog value to percentage
Serial.println(moisture);
if (moisture < 40) { //change the moisture threshold level based on your calibration values
digitalWrite(motorPin, HIGH); // turn on the motor
digitalWrite(blinkPin, HIGH); // turn on the LED
delay(watertime * 1000); // multiply by 1000 to translate seconds to milliseconds
}
else {
digitalWrite(motorPin, LOW); // turn off the motor
digitalWrite(blinkPin, LOW); // turn off the LED
delay(waittime * 60000); // multiply by 60000 to translate minutes to milliseconds
}

}

Statistically, posts that cite that event prior to a failure, are usually related to power surges from the pump turn on, resetting or crashing the Arduino. In other words, a hardware problem.

Try testing it with an LED connected instead of a pump.

see post #2

Yes, edit the original post and enclose your code in code tags would be the first place to start.

Example of code in code tags:

#include <LiquidCrystal.h>
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int j=0;
int prev=0;
int pres=0;
int motorPin = 8; // pin that turns on the motor
int blinkPin = 13; // pin that turns on the LED
int watertime = 5; // how long it will be watering (in seconds)
int waittime = 1; // how long to wait between watering (in minutes)

void setup()

Thanks aarg

So I have confirmed the problem is a power surge.
Any ideas on how I would stop this.

Well, the first thing you should do is to read the instructions for posting in the forum. :face_with_raised_eyebrow:

You can then edit your first post above to properly show your code.

The next point you will note, is to fully describe what you have, all the parts, how you are connecting them, how you are arranging the wiring, with diagrams and photos as that tends to reveal what actual mistakes you have made. :grin:

+1 on posting that image of your project.

Hello
Why do you use the delay() function?

Line 40: delay(500);
	Line 48: delay(watertime * 1000); // multiply by 1000 to translate seconds to milliseconds
	Line 53: delay(waittime * 60000); // multiply by 60000 to translate minutes to milliseconds