0
#include <LiquidCrystal.h>
#define trigPin 12              // the trig pin to digital pin 12
#define echoPin 13              // the Echo pin to digital pin 13 
#define led 8
#include <Servo.h>
Servo servoMain; // Define our Servo

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600); // Starts the serial communication
  lcd.begin(16, 2);  
  servoMain.attach(11); // servo on digital pin 11   
}

void loop() {
  servoMain.write(5);  // Turn Servo Left to 5 degrees
  delay(1000); // Wait 1 second
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 3) {
    digitalWrite(led, HIGH);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("PLEASE REFILL!");
    lcd.setCursor(0, 1);
  } else {
    digitalWrite(led, LOW);
  }
  if (distance < 6) {
    digitalWrite(led, HIGH);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("U May Refill Now");
    lcd.setCursor(0, 1);
  } else {
    digitalWrite(led, LOW);
  }
  if (distance < 8) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Take it easy");
    lcd.setCursor(0, 1);
  }
  if (distance <= 16) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("FULL!!");
    lcd.setCursor(0, 1);
  }
  if (distance > 16 || distance <= 0) {
    Serial.println("Out of range");
  } else {
    Serial.print(distance);
    Serial.println(" cm");
  }
}
delay(1000);

} }

13
  • 2
    A big chunk of your code isn't contained in a function. – Majenko May 2 '17 at 15:33
  • 1
    By placing it in a function. I suspect you really wanted to have it inside the loop() function, not after it. – Majenko May 2 '17 at 15:35
  • 2
    I think you should learn a programming language first. I konw the Arduino IDE is nothing compared to VisualStudio, Xcode or all the other fancy IDEs but knowing your language and reading errors carefully will help. – Kwasmich May 2 '17 at 15:43
  • 1
    You should also learn basic formatting rules, or at the very least use the autoformat button in the IDE. It saves many headaches getting your indenting right from the start. – Majenko May 2 '17 at 15:44
  • 1
    Pressing ctrl-T in Arduino IDE helps. As you can see after reformatting, your code is flawed. Fix that first. – dda May 2 '17 at 16:16
1

At line 82 you have the closing bracket of the loop function. Then after that you have:

  delay(1000);

}
}

Which is invalid.

If you format your code properly it's easy to see. Fix that and it will compile. Will it do what you want is an other question...

#include <LiquidCrystal.h>
#define trigPin 12        // the trig pin to digital pin 12
#define echoPin 13        // the Echo pin to digital pin 13
#define led 8
#include <Servo.h>
Servo servoMain; // Define our Servo

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600); // Starts the serial communication
  lcd.begin(16, 2);
  servoMain.attach(11); // servo on digital pin 11

}
void loop()
{
  servoMain.write(5); // Turn Servo Left to 5 degrees
  delay(1000);     // Wait 1 second

  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  if (distance < 3) {
    digitalWrite(led, HIGH);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("PLEASE REFILL!");
    lcd.setCursor(0, 1);
  }
  else {
    digitalWrite(led, LOW);
  }

  if (distance < 6) {
    digitalWrite(led, HIGH);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("U May Refill Now");
    lcd.setCursor(0, 1);
  }
  else {
    digitalWrite(led, LOW);
  }

  if (distance < 8) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Take it easy");
    lcd.setCursor(0, 1);
  }  

  if (distance <= 16) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("FULL!!");
    lcd.setCursor(0, 1);
  }

  if (distance > 16 || distance <= 0) {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
}
delay(1000);

}
}
1
  • I suspect that will always say FULL!! no matter the level. Think about if distance is 2, that's less than 16 isn't it? Maybe you need some else if action. – Delta_G Jun 1 '17 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.