I want to lcd.print the output of a potentiometer to one decimal place. The following code displays the 1-5 range I would like, I would just like it to be expressed with one decimal place added.
My apologies for the simple question and likely awkward code, I am new to programming. In searching online and studying examples I have found some answers say to use float and some say it is not the best way.
#include <LiquidCrystal.h> // include the library code
#define meterPin 5
#define potentiometerPin A1
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); // initialize the library with the numbers of the interface pins
void setup()
{
pinMode(meterPin, OUTPUT);
lcd.begin(16, 2); // set up the LCD's number of columns and rows
Serial.begin(9600);
}
void loop()
{
int potentiometerValue = analogRead(potentiometerPin);
int meterPower = map(potentiometerValue, 0, 1020, 0, 255); //the power range of the meter
int lcdRange = map(potentiometerValue, 0, 1020, 0, 5); // the 1-5 range of the display on the LCD
lcd.setCursor(6, 0); // set the cursor to column 6, line 0
lcd.print(lcdRange); // print out the value on LCD Display
lcd.setCursor(8, 0); // set the cursor to column 8, line 0
lcd.print("MJ");
analogWrite(meterPin, meterPower);
delay(100);
lcd.clear();
}
For the above values if you want more resolution then you can map the integer range from the pot to a float range. Then you can print whatever resolution value that you want to.
You'll need a float. I'm not sure if you can "force" map() to return a float but if you define ldcRange as a float then lcdRange = 5 * PotentiometerValue / 1020 should work.
But, I've forgotten some of those rules so you might have to use lcdRange = (float)5 * PotentiometerValue.
You'll also have to figure-out how to print just one decimal place.
#include <LiquidCrystal.h> // include the library code
#define potentiometerPin A1
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); // initialize the library with the numbers of the interface pins
void setup()
{
lcd.begin(16, 2); // set up the LCD's number of columns and rows
Serial.begin(9600);
}
void loop()
{
float Percentage = (analogRead(potentiometerPin) / 1020.0) * 5;
float PercentageRound = Percentage + 0.005;
lcd.setCursor(0, 0); // set the cursor to column 0, line 0
lcd.print(PercentageRound); // print out the value on LCD Display
delay(100);
lcd.clear();
}
I would know that int is a keyword and not integer and accordingly I don't feel comfort to relate int with integer though the relation could be established.
Thanks to everyone that helped, below is the code that works for my project. It's probably awkward but I can learn from and try to improve it.
#include <LiquidCrystal.h> // include the library code
#define potentiometerPin A1
#define meterPin 5
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); // initialize the library with the numbers of the interface pins
void setup()
{
lcd.begin(16, 2); // set up the LCD's number of columns and rows
Serial.begin(9600);
}
void loop()
{
float Percentage = (analogRead(potentiometerPin) / 1020.0) * 5;
float PercentageRound = Percentage + 0.005;
int meterPower = (analogRead(potentiometerPin) / 1020.0) * 255; //the power range of the meter
lcd.setCursor(5, 0); // set the cursor to column 5, line 0
lcd.print(PercentageRound, 1); //print 1 decimal place // print out the value on LCD Display
lcd.setCursor(9, 0); // set the cursor to column 9, line 0
lcd.print("MJ");
analogWrite(meterPin, meterPower);
delay(100);
lcd.clear();
}
I wouldn't call 0-5 percentage. 0-100% would be.
Why 1020. A 10-bit A/D goes to 1023.
Seems like a "fix" for the lower end of the scale.
Try int percentage = map(analogRead(potentiometerPin), 0, 1024, 0, 51); // 0-50
and then lcd.print(percentage / 10, 1); // divide by 10 and display with one decimal place
I would have used
int meterPower = analogRead(potentiometerPin) >> 2; // 0-255 (10-bit to 8-bit)
Why clear the LCD after displaying 0.1 seconds. Does your brain work that fast?
You normally clear just before you write new data.
And, clearing the whole display takes time, so the display flickers.
It would be better to just overwrite the old data.
with maybe one or two spaces, to clear longer old data.
But that's a lot faster than clearing the whole screen.
Leo..
Thank you for taking the time to help me with the code. This is a great opportunity for me to learn from the errors caused by my lack of experience. I like to learn by doing as well as lessons and tutorials so I know I make mistakes. It really helps to be corrected.