1
votes

I am new to Arduino. I want to display data read from the sensor on the LCD, first row fixed and 2nd row changing with sensor value. But I see first row for a while and then all garbage.

Here is the full code for reference:

#include <LiquidCrystal.h>
char ch;
int Contrast=155;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int analogInPin1 = A0;  // Analog input pin that the potentiometer is attached to
const int analogInPin2 = A1;  // Analog input pin that the potentiometer is attached to
const int analogInPin3 = A2;  // Analog input pin that the potentiometer is attached to

int sensorValue1 = 0;        // value read from the pot
int outputValue1 = 0;        // value output to the PWM (analog out)

int sensorValue2 = 0;        // value read from the pot
int outputValue2 = 0;        // value output to the PWM (analog out)

int sensorValue3 = 0;        // value read from the pot
int outputValue3 = 0;        // value output to the PWM (analog out)

void setup() 
{
    Serial.begin(9600);

    analogWrite(6,Contrast); // setting contrast using code
    analogWrite(9,28836); // setting backlight led on

    // set up the LCD's number of columns and rows: 
    lcd.begin(16, 2);

    // Print a message to the LCD.
    lcd.setCursor(0,0);
    lcd.print(" CAR1 CAR2 CAR3 ");

    delay(200);
}

void loop() 
{
    // read the analog in value:
    sensorValue1 = analogRead(analogInPin1);
    // map it to the range of the analog out:
    outputValue1 = map(sensorValue1, 0, 1023, 0, 255);
    delay(250);
    sensorValue2 = analogRead(analogInPin2);
    // map it to the range of the analog out:
    outputValue2 = map(sensorValue2, 0, 1023, 0, 255);
    delay(250);  
    sensorValue3 = analogRead(analogInPin3);
    // map it to the range of the analog out:
    outputValue3 = map(sensorValue3, 0, 1023, 0, 255);
    delay(250);
    lcd.setCursor(0,1);

    lcd.print(outputValue1);
    lcd.print("  ");
    lcd.print(outputValue2);
    lcd.print("  ");
    lcd.print(outputValue3);
}
2
I guess you should also update the first line with the fixed text, within the second loopOrElse
Please show declaration of 'lcd.print()'. It's not clear what it takes as a parameter and C does not have overloads.Martin James
I cleared the whole lcd in loop and printed both lines. its working but now it goes blank in few seconds. What could be the problem. I have to reset arduino to see lcd data. and it again goes blank.Kushal Shukla

2 Answers

0
votes

My guess would be lcd.print only prints strings. If there is a lcd.printf, that's the way. If not then you'll have to change the integer values to a string by using itoa() or sprintf().

0
votes

The print-functions has different definitions for argumenttypes much like Serial.print. It is good practice to clear the lcd bevor overriding it. For example if the outputval's are the first time 1011 and the second time 1 you will get the first time. 1011 1011 1011 and the second. 1 1 1 that gives you 1 1 11011 1011

The easy way is to call lcd.clear() to clear the hole lcd and write the first line again. Alternative you kann write " "(Spaces) in the second line see: https://forum.arduino.cc/index.php?topic=212460.0