Looking for help with my AEROPONICS WATERING SYSTEM code

AEROPONICS WATERING SYSTEM

Aim: To open a SOLENOID VALVE to water my plants every 5 mins for 30 secs using a 12v PUMP & accumulator to building the pressure of the water up to 100-80 psi and using a pressure transducer to read the pressure and turning ON/OFF the relay to power the 12v PUMP( ON < 80psi OFF > 100psi)

here is the code i have try to compile so far to no joy

   // multi task turn on relay On for 30secs Off for 5 minutes
    // and the current state of the RELAY
    
    const int relayPin1 =  6; 
    const int relayPin2 =  7;      // the number of the RELAY pin
    int relayState = LOW;             // relayState used to set the RELAY
    unsigned long previousMillis = 0;        // will store last time RELAY was updated
    const long OnTime = 30000;           // milliseconds of on-time
    const long OffTime = 300000;          // milliseconds of off-time
     
    void setup() 
    {
      // set the digital pin as output:
      pinMode(relayPin2, OUTPUT);
      pinMode(relayPin1, OUTPUT);   
    
     Serial.begin(9600);
} 
    void loop()
    {
      // check to see if it's time to change the state of the RELAY
      unsigned long currentMillis = millis();
     
      if((relayState == HIGH) && (currentMillis - previousMillis >= OnTime))
      {
        relayState = LOW;  // Turn it off
        previousMillis = currentMillis;  // Remember the time
        digitalWrite(relayPin2, relayState);  // Update the actual Relay
      }
      else if ((relayState == LOW) && (currentMillis - previousMillis >= OffTime))
      {
        relayState = HIGH;  // turn it on
        previousMillis = currentMillis;   // Remember the time
        digitalWrite(relayPin2, relayState);    // Update the actual RELAY
      }
int sensorVal=analogRead(A1);
Serial.print("Sensor Value: ");
Serial.print(sensorVal);

float voltage = (sensorVal*5.0)/1024.0; Serial.print("Volts: "); Serial.print(voltage); float pressure_pascal = (3.0*((float)voltage-0.47))*1000000.0;
float pressure_bar = pressure_pascal/10e5; Serial.print("Pressure = "); Serial.print(pressure_bar); Serial.println(" bars"); Serial.print("Pressure = "); delay(100);
    
    
 if (pressure_bar < 5.74); digitalWrite (relayPin1, HIGH); else if (pressure_bar > 7.04) digitalWrite(relayPin1, LOW);
    }

this is the error message i am getting

Arduino: 1.8.1 (Linux), Board: "Arduino/Genuino Uno"

/home/swigg/Arduino/relay_pressure/relay_pressure.ino: In function 'void loop()':
relay_pressure:44: error: 'else' without a previous 'if'
  if (pressure_bar < 5.74); digitalWrite (relayPin1, HIGH); else if (pressure_bar > 7.04) digitalWrite(relayPin1, LOW);
                                                            ^
exit status 1
'else' without a previous 'if'

if you have the time and could run your eye over it and maybe see where i have gone wrong . it would be very much appreciated. and advice if i could do something better as i am new to this and last but not least credits for the milis code goes to learn.adafruit.com

kind regards
NZPPAK

If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it’s ok to add it as an attachment. Under no circumstances should you use pastebin!

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Same goes for the error message. Post on the forum using code tags.

If you take the time to read the error message it’s quire clear. Here’s the error message:

/home/swigg/Arduino/relay_pressure/relay_pressure.ino: In function 'void loop()':
relay_pressure:44: error: expected ')' before ';' token
  if (pressure_bar < 5.74) digitalWrite (relayPin1, HIGH;) else if ((pressure_bar > 7.04) digitalWrite(relayPin1, LOW;)
                                                        ^

Let’s break it down:

/home/swigg/Arduino/relay_pressure/relay_pressure.ino: In function 'void loop()':
relay_pressure:44

So the error is in the file relay_pressure.ino at line 44.

error: expected ')' before ';' token

The compiler expected to find a ) in that line before the ;

  if (pressure_bar < 5.74) digitalWrite (relayPin1, HIGH;) else if ((pressure_bar > 7.04) digitalWrite(relayPin1, LOW;)
                                                        ^

That’s line 44 with a little arrow that points right to the problem semicolon. Now see if you can figure out how to fix it.

Arduino: 1.8.1 (Linux), Board: "Arduino/Genuino Uno"

/home/swigg/Arduino/relay_pressure/relay_pressure.ino: In function 'void loop()':
relay_pressure:44: error: 'else' without a previous 'if'
  if (pressure_bar < 5.74); digitalWrite (relayPin1, HIGH); else if (pressure_bar > 7.04) digitalWrite(relayPin1, LOW);
                                                            ^
exit status 1
'else' without a previous 'if'

is th next error please bare with me i am new to this :slight_smile: i do really appreciate your guys help

Put every statement on a separate line. There is no point in mashing a bunch of code into one line.

In this case the error message is slightly less helpful. You will often find that the code an error message point you to is fine and the problem actually is a little before that code. That is the case here.

You should always use curly brackets to surround the code that is controlled by an if statement. This is not absolutely necessary if there is only one statement to be controlled but it makes you code much easier to read and less prone to bugs.

The problem is that you have put a semicolon at the end of your if statement. With brackets your code translates to this:

  if (pressure_bar < 5.74) {
    ;
  }
  digitalWrite (relayPin1, HIGH);
  else if (pressure_bar > 7.04) {
    digitalWrite(relayPin1, LOW);
  }

else if() must alway follow directly after the if() block but because digitalWrite() ended up outside of your if() block that causes the else if() to be an error. Really the code you intended was:

  if (pressure_bar < 5.74) {
    digitalWrite (relayPin1, HIGH);
  }
  else if (pressure_bar > 7.04) {
    digitalWrite(relayPin1, LOW);
  }

Of course you could also write this as:

if (pressure_bar < 5.74) digitalWrite (relayPin1, HIGH); else if (pressure_bar > 7.04) digitalWrite(relayPin1, LOW);

(note the semicolon after the if statement was removed). But as you’ve seen, writing code this way causes problems. Putting each statement on a separate line doesn’t use any more memory on your Arduino.

firstly thanks very much for your help !

i tried the code it complied :smiley: its worked

but i have run into a problem when i run the code it . when the pressure is 0.00 the sensor doesn’t turn on the relay to high , also i manual bypassed the Arduino. plugged pump into battery got pressure into the ranges of 5 bar and 7bar then replugged the Arduino up to check if the cut off would work but the relay turn stay on high but doesn’t turn off once sensor reads 7bar what have i done wrong with my if and else if statements :(?

kind regards

Are you getting the pressure readings you’re quoting from the Serial.print()s in your sketch?

nzppak:
what have i done wrong with my if and else if statements :(?

We don't know, because we can't see your code

yes thats correct pert. its reading it correctly but its just not turning on the relay if there is zero pressure in the system . and its also not turning it off at 7bar . i only know about the 7 bar cut off cause i charged the accumulator manually and have a manual pressure gauge

http://imgur.com/a/vKrNy is what the prototype looks like

EDIT:
Never mind. :blush:

Please post your updated sketch.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.