Lcd wont read serial value

i made an code for a scale and it goes to 9 kilometers. I transferred the centimeters to kilograms in manually but the arduino always says is 9 Kilogram

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
int trigPin = 9;    // TRIG pin
int echoPin = 8;    // ECHO pin
int buttonpin = 2;

float duration_us, distance_cm;

void setup()
{
  Serial.begin(9600); //Serial baud rate
  lcd.begin (16, 2);
  lcd.init(); // initialize the lcd
  lcd.backlight();

  lcd.home ();
  lcd.print(distance_cm);
  //delay(2000);
  //lcd.clear();
  // configure the trigger pin to output mode
  pinMode(trigPin, OUTPUT);
  // configure the echo pin to input mode
  pinMode(echoPin, INPUT);
  pinMode(buttonpin, INPUT_PULLUP);
  ;
}

void loop()
{
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10000);
  digitalWrite(trigPin, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echoPin, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;
  if(digitalRead(buttonpin) ==LOW)
  {
    delay(30);
    if(digitalRead(buttonpin) ==LOW)
    {
      if( distance_cm >= 19.90){
       lcd.clear();
      lcd.print("0 kilogram");
    }
    if(18.00<=distance_cm<19.90){
       lcd.clear();
      lcd.print("500 gram");
  } if(17.00<=distance_cm<18.00){
       lcd.clear();
      lcd.print("1.500 kilogram");} 
      if(16.00<=distance_cm<17.00){
       lcd.clear();
      lcd.print("2 kilogram");} 
      if(15.00<=distance_cm<16.00){
       lcd.clear();
      lcd.print("2.5 kilogram");} 
      if(14.00<=distance_cm<15.00){
       lcd.clear();
      lcd.print("3 kilogram");}
       if(13.00<=distance_cm<14.00){
       lcd.clear();
      lcd.print("3.5 kilogram");}
      if(12.00<=distance_cm<13.00){
       lcd.clear();
      lcd.print("4 kilogram");}
      if(11.00<=distance_cm<12.00){
       lcd.clear();
      lcd.print("4.5 kilogram");}
       if(10.00<=distance_cm<11.00){
       lcd.clear();
      lcd.print("5 kilogram");}
       if(9.00<=distance_cm<10.00){
       lcd.clear();
      lcd.print("5.5 kilogram");}
       if(8.00<=distance_cm<9.00){
       lcd.clear();
      lcd.print("6 kilogram");}
       if(7.00<=distance_cm<8.00){
       lcd.clear();
      lcd.print("6.5 kilogram");} 
      if(6.00<=distance_cm<7.00){
       lcd.clear();
      lcd.print("7 kilogram");}
       if(5.00<=distance_cm<6.00){
       lcd.clear();
      lcd.print("7.5 kilogram");}
       if(4.00<=distance_cm<5.00){
       lcd.clear();
      lcd.print("8 kilogram");}
       if(3.00<=distance_cm<4.00){
       lcd.clear();
      lcd.print("8.5 kilogram");
       }
        if(2.00<=distance_cm<3.00){
       lcd.clear();
      lcd.print("9 kilogram");
        }
 


}}}

Compound comparisons like this are not allowed in C. You need something like
if(2.00<=distance_cm and distance_cm <3.00){

Rather than have a bunch of independent if statements, you should make that a series of if/else statements, starting with the largest value and decreasing. That way, you only have to do one comparison. You can also factor out the lcd.clear() statement so your if() statements don't need brackets anymore..

      lcd.clear();
      if ( distance_cm >= 19.90)     lcd.print("0 kilogram");
      else if ( distance_cm > 18.00) lcd.print("500 gram");
      else if ( distance_cm > 17.00) lcd.print("1.500 kilogram");
      else if ( distance_cm > 16.00) lcd.print("2 kilogram");
      else if ( distance_cm > 15.00) lcd.print("2.5 kilogram");
      else if ( distance_cm > 14.00) lcd.print("3 kilogram");
      ...

You get the pattern...

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.