# Help with programming on a display

I'm new to Arduino and had an application where I need to read a voltage 0 to 5v and apply a function to that voltage and then send the output to an LCD to be displayed (Making a controller/display for a Pfeiffer compact full-range vacuum gauge). I have an LCD compatible with the LiquidCrystal library, but all the examples and explanation I can find is either too detailed and complex or too general and beginner oriented. For example, I could find little info on how to output a variable to the LCD. I'm experienced in other languages besides C and am having trouble with the API stuff. Input would be appreciated. Thanks.

antigravityd:
For example, I could find little info on how to output a variable to the LCD.

Here's a few lines from some old LCD code of mine:

``````float gravity;

// then later on in loop()
//do some calcs to get gravity, and then:

lcd.setCursor(0,1);
lcd.print("Gravity: ");
lcd.print(gravity);
``````

Yeah Arduino really needs a page that describes the Print class.
Here is the closest thing to that.
https://www.arduino.cc/en/Serial/Print
Everything shows using using the serial port object but the Print class also works for the LiquidCrystal lcd object.
Just change "Serial" to the name of your lcd ojbect.

--- bill

I’ve come up with a little something for my purpose. I don’t have an Arduino uplink cable so I can’t test the code. Can someone tell me if this is right?

#include <LiquidCrystal.h> //importing library

LiquidCrystal lcd(12,11,10,9,8,7); //defining which inputs to use for lcd

void setup(
//do I need any setup? Its a constant measuring application
) {
}

void loop() {

lcd.begin(16,2); //lcd digit specification

float Read = analogRead(A0); // defining a variable that is the analog value of analog pin 0

float P = pow(10, 1.667 * (2 * Read) - 11.46); // a math operation, changing vin to a pressure reading

lcd.print(P); // outputing variable value to lcd
}

lcd.begin needs to be in setup. You shouldn't call that over and over.

analogRead returns an int. Putting it into a float doesn't do anything to improve the resolution, only waste two bytes of memory.

If you run this, it will print the number calculated in P to 2 decimal places and then print it again and again and again and again over and over at light speed. Two problems, first it's too fast to read and the screen will just flicker. So you add a delay to slow it down to maybe once a second and you see (say the pressure is 29.92)

29.9229.9229.9229.9229.92

Because every time you call print it just goes right where it left off. You need to clear the screen and put the cursor back to the right spot. Or at least put the cursor back and clear out the reading by printing spaces. See the examples in the lcd library you are using.

How does this look?

#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,10,9,8,7);

void setup() {
lcd.begin(16,2);
}

void loop() {

delay(100);

lcd.clear();

lcd.setCursor(0,1);

float P = pow(10, 1.667 * (2 * Read) - 11.46);
lcd.print(P);
}

I need high precision in the math’s output, as the pressures I’m measuring are like 2.2E-4 Torr and whatnot. Does P store to that much precision?

P is going to have about 6 digits of precision. But that means 6 digits, not 6 places after the decimal. You expressed a number as 2.2E-4 so I'm assuming you understand how scientific notation works. I'm talking about the number of digits in that number.

An unsigned long has 9 digits of precision, but it only handles integers. That's bad cause you need decimals right? Wrong. If you are working with 2.2E-4 Torr then why not work with 2.2E2 uTorr. Or even 2.2E5 nTorr? You're only using 2 of 9 possible digits of precision there. With unsigned long you can store a number like 21975887 pTorr and that takes your number and adds 6 more decimal places.

If you then want to display it in Torr or whatever it is simple to add a decimal to it after you convert to ascii to send to the screen.

***Check my math. But the concept remains the same even if I dropped a exponent somewhere.

Will P be output in scientific notation? For the moment, I only need ballpark precision. In fact, the controller for the gauge that I'm too broke to afford only has 5 display digits.

antigravityd:
Will P be output in scientific notation? For the moment, I only need ballpark precision. In fact, the controller for the gauge that I'm too broke to afford only has 5 display digits.

It will be stored in binary. You can output it in any format you want. There's a function called sprintf that will let you build a string up out of anything in any format. There are lots of functions that let you build whatever you want. There are no limits here on something like formatting.

I mean if you want to write the code for it you could output your numbers in ancient Egyptian hieroglyphs if you wanted.