Search code examples
loopsif-statementarduinoarduino-nano

Read a variable and printing it on a monitor - Arduino


I am trying to read a variable and print it on the display But something is probably wrong with IF I would appreciate your assistance

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); 

int Value_pin_A0; // global variable to store the sensor value

void setup() {
  Serial.begin(9600); //initialize serial communication at 9600 bits per second
}

void loop() {
  //the loop routine runs over and over again forever
  int sensorValue = analogRead(A0); //read the input on analog pin 0
  Value_pin_A0 = sensorValue;
  Serial.println(sensorValue); //print out the value you read
  if ((Value_pin_A0 > 800)) {
    lcd.setCursor(0, 0);                  // move cursor to   (3, 0)
    lcd.print("               ");        // print message at (0, 0)
    lcd.setCursor(0, 0);                // move cursor to   (3, 0)
    lcd.print("Value over 800");         // print message at (0, 0)
    delay(500);                       // display the above for two seconds
  }
  if ((Value_pin_A0 < 799)) {
    lcd.setCursor(0, 0);                  // move cursor to   (3, 0)
    lcd.print("               ");        // print message at (0, 0)
    lcd.setCursor(0, 0);                // move cursor to   (3, 0)
    lcd.print("Value less 799");         // print message at (0, 0)
    delay(500);                       // display the above for two seconds
  }
}

print it on the display


Solution

  • In your code you need to initialise the LCD and also turn on the backlight. Also you need to check that you have the correct I2C address for your LCD. For the unit that you appear to be using this is usually either 0x27 or 0x3f. If this is wrong you will see nothing.

    In the code below I am using an Arduino Uno with and I2C LCD with address 0x3f, and I am displaying the value read from A0, so if you have no connection on A0 you should see a continuously changing value (I am getting around 270 - 285).

    #include <LiquidCrystal_I2C.h>
    
    LiquidCrystal_I2C lcd(0x3f, 16, 2);  // Set the LCD I2C address - ususally 0x27 or 0x3F
    
    void setup() {
        lcd.init();      
        lcd.backlight();
        
        lcd.clear();              
        lcd.print("Hello world");
    }
    
    void loop() {
        int sensorValue = analogRead(A0);
        lcd.setCursor(0, 1);  // Display analogue reading on line 2
        lcd.print(String(sensorValue) + "      ");
        delay(500);
    }
    

    If you see nothing on the LCD you may have your I2C connections wrong. You should have SDA on A4 and SCL on A5.

    You have said that the above code is working but you want a version with an IF statement to check whether the reading on A0 is above or below 800 - so the version below satisfies that requirement.

    #include <LiquidCrystal_I2C.h>
    
    LiquidCrystal_I2C lcd(0x3f, 16, 2);  // Set the LCD I2C address - ususally 0x27 or 0x3F
    
    void setup() {
        lcd.init();      
        lcd.backlight();
        
        lcd.clear();              
        lcd.print("Hello world");
    }
    
    void loop() {
        int sensorValue = analogRead(A0);
        lcd.setCursor(0, 1);  // Analogue reading on line 2
        if (sensorValue >= 800) lcd.print(">= 800 ");
        else lcd.print("< 800 ");
        delay(500);
    }