I'm looking to display the position a potentiometer is set, which increases/decreases between 0.00 and 6.00 as it turns, like so:
I know how to display the value between 0-1023, but I'm not too sure how to do display the position code wise.
y= m(x-x0) + b
x range is 0 to 1023
y range 0 to 6
x0 = input_min = 0 (as in 0 to 1023)
b = output_min = 0 (as in 0 to 6)
m = output_range/input_range = (output_max-output_min)/(input_max-input_min) = 6/1023
Get used to this formula if you expect to be converting from ADC to real world values.
Here is another method using the map function. Since map only works with integers you can’t get floats (numbers with decimal fraction parts). If you want 0 to 6.00 to 2 decimal places just map 0 to 1023 to 0 to 600 and divide by 100.0 to display 0 to 6.00.
This code illustrates the above.
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
const byte potPin = A0;
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
static unsigned long timer = 0;
unsigned long interval = 100;
if (millis() - timer >= interval)
timer = millis();
int analogIn = analogRead(potPin);
float mappedPot = map(analogIn, 0, 1023, 0, 600) / 100.00;
lcd.print(mappedPot); // display 0 to 6.00
lcd.print(" "); // overwrite old data
Thanks a lot, guys, I got it working.
Sometimes you get “junk” on the screen from previous values. For example, you first printed 10.00 then decreased to 9.50. Instead of seeing 9.50, you see 9.500, with the last zero as a leftover from the 10.00. A lazy way is to print a couple extra spaces right after you output a number. A more elegant solution is to use sprintf to format a printout, one whole line maybe one whole screen at a time. Then print on screen.
My blog post a while ago: