Converting analog readout to percentage

I dont seem to understand this
Trying to print to lcd in percentage's instead of 0 to 1023
My range is 200 to 1000
This is what I have but does not seem to work

/*
   
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
  Second Potentiometer connection with Arduino
 * Potentiometer 5v to Arduino 5v
 * Potentiometer GND to Arduino GND
 * Potentiometer Wiper/Vout to Arduino A0(Analog pin 0)
*/

// include the library code:
#include <LiquidCrystal.h>
int sensorValue = 0;
float value;
float percentage;


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

void setup() 
{
  pinMode(A0, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop() 
{
  // read the input on analog pin 0:
  sensorValue = analogRead(A0);
   percentage =100-(value-0) /( 1023.0-0) * 100; 
  lcd.println(percentage);
  delay(700);
  // set the cursor to column 0, line 0:
  lcd.setCursor(0, 0);
  // print out the value at LCD Display:
  lcd.print("level ");
  lcd.print(percentage);
  // print out the value at Serial Monitor:
  Serial.println(sensorValue);
  delay(1000);
  lcd.clear();  
}

Does using the map() function work?

percentage = map(sensorValue, 0, 1023, 200, 1000);

for some reason it displays 200 to 100 instead of 0 to 100

percentage =100-(value-0) /( 1023.0-0) * 100;

This line doesn't make any sense to me. I don't see value getting set anywhere. Also, when you say the range is 200 to 1000 do you mean that you should only see analog values between 200 and 1000 or you need to convert the range 0 to 1023 to the range 200 to 1000?

There are some mistakes in your code:

  • You declare value but you do not relate it with anything, so it is not used.
  • As percentage is not related to sensorValue but to value, it makes no sense when it is displayed on the LCD.
  • You should map sensorValue from 0 to 100 using sensorValue variable, like this: [code]percentage = map(sensorValue, 0, 1023, 0, 100);[/code]
percentage = map(sensorValue, 200, 1023, 0, 100);

I have to characters after.00 is that from my sketch

/*
   
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
  Second Potentiometer connection with Arduino
 * Potentiometer 5v to Arduino 5v
 * Potentiometer GND to Arduino GND
 * Potentiometer Wiper/Vout to Arduino A0(Analog pin 0)
*/

// include the library code:
#include <LiquidCrystal.h>
int sensorValue = 0;
float value;
float percentage;


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

void setup() 
{
  pinMode(A0, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop() 
{
  // read the input on analog pin 0:
  sensorValue = analogRead(A0);
  percentage = map(sensorValue, 200, 1023, 0, 100);
  lcd.println(percentage);
  delay(500);
  // set the cursor to column 0, line 0:
  lcd.setCursor(0, 0);
  // print out the value at LCD Display:
  lcd.print("level ");
  lcd.print(percentage);
  // print out the value at Serial Monitor:
  Serial.println(sensorValue);
  delay(50);
  lcd.clear();  
}

erase value, which you do not use and change int and float to byte.

Then change:
percentage = map(sensorValue, 200, 1023, 0, 100); to
percentage = map(sensorValue, 0, 1023, 0, 100);

Thanks but it reads from 0 to 24 and still have those strange charaters

/*
   
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
  Second Potentiometer connection with Arduino
 * Potentiometer 5v to Arduino 5v
 * Potentiometer GND to Arduino GND
 * Potentiometer Wiper/Vout to Arduino A0(Analog pin 0)
*/

// include the library code:
#include <LiquidCrystal.h>
byte sensorValue = 0;

byte percentage;


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

void setup() 
{
  pinMode(A0, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop() 
{
  // read the input on analog pin 0:
  sensorValue = analogRead(A0);
  percentage = map(sensorValue, 0, 1023, 0, 100);
  lcd.println(percentage);
  delay(500);
  // set the cursor to column 0, line 0:
  lcd.setCursor(0, 0);
  // print out the value at LCD Display:
  
  lcd.print(percentage);
  // print out the value at Serial Monitor:
  Serial.println(sensorValue);
  delay(50);
  lcd.clear();  
}

@missdrew, Iguess that I misunderstood the OP requirement.

The LiquidCrystal library does not support println, just print. Control the cursor with setCursor().

Whoops, ignore me. :flushed:

Try:

percentage = 100 * (sensorValue - 200) / (1000 - 200.0);

Or better:

const int min = 200, max = 1000;
const float span = max - min, hundred = 100.0;
percentage = hundred * (sensorValue - min) / span;

Thanks guys. The funny characters are gone from the end of the line but as I turn the potentiometer it goes from 0 to 24 (first half) then starts a 0 again to 24(for the second half)

/*
   
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
  Second Potentiometer connection with Arduino
 * Potentiometer 5v to Arduino 5v
 * Potentiometer GND to Arduino GND
 * Potentiometer Wiper/Vout to Arduino A0(Analog pin 0)
*/

// include the library code:
#include <LiquidCrystal.h>
byte sensorValue = 0;

byte percentage;


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

void setup() 
{
  pinMode(A0, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop() 
{
  // read the input on analog pin 0:
  sensorValue = analogRead(A0);
const int min = 0, max = 1023;
const float span = max - min, hundred = 100.0;
percentage = hundred * (sensorValue - min) / span;
  lcd.print(percentage);
  delay(500);
  // set the cursor to column 0, line 0:
  lcd.setCursor(0, 0);
  // print out the value at LCD Display:
  
  lcd.print(percentage);
  // print out the value at Serial Monitor:
  Serial.println(sensorValue);
  delay(50);
  lcd.clear();  
}

Change:

To

float percentage;

As mentioned , Those characters are the carriage return/new line type stuff being printed to the display .

Change println to print.

Thats a no go jca34f the display only displays "inf"
If i turn the pot off it displays "nan"

/*
   
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
  Second Potentiometer connection with Arduino
 * Potentiometer 5v to Arduino 5v
 * Potentiometer GND to Arduino GND
 * Potentiometer Wiper/Vout to Arduino A0(Analog pin 0)
*/

// include the library code:
#include <LiquidCrystal.h>
float sensorValue = 0;
float percentage;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
{
  pinMode(A0, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop() 
{
  // read the input on analog pin 0:
  sensorValue = analogRead(A0);
const int min = 0, max = 1023;
const float span = max - max, hundred = 100.0;
percentage = hundred * (sensorValue - min) / span;

  lcd.print(percentage);
  delay(500);
  // set the cursor to column 0, line 0:
  lcd.setCursor(0, 0);
  // print out the value at LCD Display:
  lcd.print(percentage);
  // print out the value at Serial Monitor:
  Serial.print(sensorValue);
  delay(50);
  lcd.clear();  
}

span = max - max

Oops ...

Thanks PieterP
Working now
Just one more question. How do I insert "level at" before the number

Hi,
Can you post your now working code please?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Thanks guys got it working good.

/*
   
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
  Second Potentiometer connection with Arduino
 * Potentiometer 5v to Arduino 5v
 * Potentiometer GND to Arduino GND
 * Potentiometer Wiper/Vout to Arduino A0(Analog pin 0)
*/

// include the library code:
#include <LiquidCrystal.h>
float sensorValue = 0;
float percentage;

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

void setup() 
{
  pinMode(A0, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
  lcd.print("Tank Level:");
}

void loop() 
{
  // read the input on analog pin 0:
  sensorValue = analogRead(A0);
const int min = 0, max = 1023;
const float span = max - min, hundred = 100.0;
percentage = hundred * (sensorValue - min) / span;
lcd.setCursor(0, 0);
lcd.print("Tank Level:");
lcd.setCursor(4,1);
  lcd.print(percentage);
  lcd.print("%");
  delay(2000);
 
// print out the value at Serial Monitor:
  Serial.print(sensorValue);
  delay(50);
  lcd.clear();  
}