Analog input to LCD, artifacts on LCD syntax code

Hello there

I'm new to arduino and in programming mc in C so be nice with me :slight_smile:

I have made a program that reads a potensiometer and prints the analog value to the LCD.

#include <LiquidCrystal.h>

int sensorPin = 0;
int sensorvalue = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
    // Print a message to the LCD.
  lcd.print("hello, world!");
  Serial.begin(9600);
  }

void loop() {
  sensorvalue = analogRead(sensorPin);
  delay(40);
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("value=");
  //lcd.print(millis()/1000);
  lcd.setCursor(7, 1);
  lcd.print(sensorvalue);
  Serial.println(sensorvalue);
//  lcd.setCursor(0, 1);
}

2 problems arise.
1st The value printed on the LCD when in max position (1023) prints ok but when i lower it it mix the actual value with a random number in back. For example the actual value is 285 and i get 2855. It's like it buffers 4 digits and when the value is 3 digits it keeps 4th also

Second problem has to do with syntax

I want to make the following in order to print everything with a single line but i get an error

call of overloaded print const char [9], int&)' is ambiguous

lcd.print("value=%d",sensorvalue);

Thnx in advance and nice to find u

1st The value printed on the LCD when in max position (1023) prints ok but when i lower it it mix the actual value with a random number in back.

The code is writing new data to the LCD. You need to also erase the old data.

Alternatively, you need to ensure that you always write 4 digits.

lcd.print("value=%d",sensorvalue);

There is no overloaded version of the print function that can do that. You could use sprintf to format the string, and then pass that string to lcd.print.

Doing so would allow you solve both problems, by using the %4d format specifier.

Now u got me

In C you can do that kind of stuff probably there are some differences with C and arduino.

Can you be more explanatory with some code maybe to explain me that kind of stuff u talk about?

In C you can do that kind of stuff

You can do what kind of stuff? Pass arbitrary kinds of data to functions and expect the function to handle it? Not in my 25 years of C programming.

Don't get me wrong i'm a beginner in C but this piece of code runs fine on PC

#include <stdio.h>

int sensorvalue = 1023;

main()
{
      printf("value=%d",sensorvalue);
      system("PAUSE");
      }

So, can you explain me what i did wrong and how i can correct this in arduino because so far i can't understand what is the mistake i make

this piece of code runs fine on PC

can you explain me

And can you show me where the Arduino implements "printf"?

Seriously, if you looked at the source for "printf" in, say, a Linux distrbution, you'd be amazed at how huge it is, and how many levels and layers there are before the string you "printf"ed appeared on whatever output device happened to be connected to stdout.

This is microcontroller-land, where the whole memory of the device would have to be given over to the implementation of vargs, format strings, radix conversion and all the other paraphenalia required for just one simple "printf".

OK i got the point. Is there a source i can get a hint on sprintf.

As far as i undertstand lcd.print is a function call from a library that accepts chars??? So i have to convert the integer value or analog to char in order to display it correctly?

Finally do u suggest reading ANSI C for PC 1st or try to understand C for microcontrollers?

You can google sprintf. It is printf except that it writes to a buffer (an array) instead of a device.

A good understanding of C will be essential. There are some limitations that are imposed when running C on micro controllers. Those limitations will be more easily understood if you understand C first.

OK i will return to my C reading

Thanks for your time