expected unqualified-id before 'if'

im trying to make an automatic irrigation using arduino and im having a little problem combining my lcd code and the spayer code. what does it meant by this error code messages?
this is the error code messages
Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: “Arduino/Genuino Uno”

MoistureSensorNokia:45: error: expected unqualified-id before ‘if’

if (percentValue < 60)

^

MoistureSensorNokia:49: error: expected unqualified-id before ‘else’

else {digitalWrite(waterPump, LOW);}

^

exit status 1
expected unqualified-id before ‘if’

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.
here’s is the code

MoistureSensorNokia.ino (1.18 KB)

This section of code is not part of a function:

if (percentValue < 60)
  {
    digitalWrite(waterPump, HIGH);
  }
 else {digitalWrite(waterPump, LOW);}

I expect it was supposed to be part of the function above it, although the name convertToPercent doesn’t suggest that a waterpump is being controlled too.

Mind the curly barckets in this function

int convertToPercent(int value)
{
  int percentValue = 0;
  percentValue = map(value, 1023, 0, 0, 100);
  if(percentValue>100)
    percentValue = 100;
  return percentValue;

}  // this closes the convertToPercent function

// the rest is hanging outside of a function

if (percentValue < 60)
  {
    digitalWrite(waterPump, HIGH);
  }
 else {digitalWrite(waterPump, LOW);}  // this curly bracket  has no matching opening curly

What I tend to do when I get difficult to spot syntax error is this sort of thing…

Comment out all the code in your functions and move the /* down one line at a time until you get the compile error again.

The line immediately before the /* you moved is where the syntax error is.

#include <LCD5110_Graph.h> 

LCD5110 lcd(8,9,10,12,11);

extern unsigned char BigNumbers[];
extern uint8_t ui[];

int sensorPin = A0;  
int sensorValue = 0;  
int percent = 0;
String percentString ="0";
int stringLength = 0;
int waterPump = 13;

void setup() 
{
/*
  lcd.InitLCD();
  lcd.setFont(BigNumbers);
  delay(1000);
  pinMode(waterPump, OUTPUT);
*/
}

void loop() 
{
/*
  lcd.clrScr();
  
  lcd.drawBitmap(0, 0, ui, 84, 48);
  sensorValue = analogRead(sensorPin);
  percent = convertToPercent(sensorValue);
  percentString = String(percent);
  stringLength = percentString.length();
  displayPercent(stringLength);
  
  lcd.update();
  delay(1000);
 */
} 

int convertToPercent(int value)
{
/*
  int percentValue = 0;
  percentValue = map(value, 1023, 0, 0, 100);
  if(percentValue>100)
    percentValue = 100;
  return percentValue;
  */
}
if (percentValue < 60)
{
    digitalWrite(waterPump, HIGH);
}
else
{
	digitalWrite(waterPump, LOW);
}

  
void displayPercent(int length)
{
  switch(length)
  {
    case 1:  lcd.print(percentString,38,19); break;
    case 2:  lcd.print(percentString,24,19); break;
    case 3:  lcd.print(percentString,10,19); break;
    default:  lcd.print(percentString,0,19); break;
  }
}

For future reference:

Common compiler errors caused by mismatched brackets:

"does not name a type" or "expected declaration before" or "expected unqualified-id before" Usually means you forgot a '{' or put in an extra '}' in the previous function. Since all of the open brackets have been closed, the compiler is looking for further global declarations (variables or functions). If it finds something that looks like executable code instead of a global declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.

"a function-definition is not allowed here" Usually means you forgot a '}' or put in an extra '{' in the previous function. Since a set of brackets has not been closed yet the compiler is looking for more code to put in the function. You can't declare a function inside a function so if the compiler finds a function definition it emits an error. Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.

"expected '}' at end of input" Usually means you forgot a '}' or put in an extra '{' in the last function in the sketch. Since a set of brackets has not been closed yet, the compiler is looking for more code to put in the function. When it hits the end of the file instead it emits an error. Make sure that the brackets in the last function are in matching pairs '{' followed by '}'.

"expected primary-expression before '}' token" Usually means you have an incomplete statement before a '}'. The block statement (between '{' and matching '}') can only contain complete statements.