Soil Moisture Sensor + Relay Programming issues

Hi Friends,

I am using a mega2560 + sensor shield v.2 + 4channel 12v relay + solenoid valve + soil moisture sensor (see picture)

The project is a self watering garden.

This is the code so far:

/Garden Bed Watering System & Aquaponics Management/
/sensor values = 300 Dry, 301-699 Perfect, 700 Flooded/

/Analogue Input/
int Water_Level_1 = A1;

/* Digital Input*/
int Moisture_Sensor_1 = 5;
int sensorValue = 0;
int Temperature_probe_1 = 6;

/Digital Output/
int Soil_Valve_1 = 1;
int Water_Valve_1 = 2;

void setup() {
pinMode(5, INPUT);
pinMode(6, INPUT);
Serial.begin(9600);
}

void loop() {
Serial.print("sensor = ");
sensorValue = digitalRead(Moisture_Sensor_1);
Serial.println(sensorValue);
}

if(sensorValue <300){
digitalWrite(Soil_Valve_1,HIGH)
}

This is the error -

Arduino: 1.8.4 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

scott_greenhouse:29: error: expected unqualified-id before 'if'

if(sensorValue <300){

^

exit status 1
expected unqualified-id before 'if'

Thanks is advance for your help

Scott

This is because the if is outside of any function. Just place all your code within the setup() or loop() functions. For your specific problem, just remove the curly bracket before the if and place it at the very bottom of the file.

void loop()  {
 Serial.print("sensor = ");
 sensorValue = digitalRead(Moisture_Sensor_1);
 Serial.println(sensorValue);

 if(sensorValue <300) {
   digitalWrite(Soil_Valve_1,HIGH)
 }
}