How can i change string to a percent for LCD display ?

I want to transform for my Mq-3 sensor a string to a percent (X.XX %) ???

here the code:

#include "arduino.h"
#include <LiquidCrystal.h>
#include <sstream>
#include <string>
#include <iostream>

using namespace std;

float concentration(float x) {
  const float A[] = { 
    2.71494E+02, -3.10999E+02, 6.85051E+02, -3.47587E+02, 7.47499E+01            }; 
  float result;
  float B[4];
  B[0] = x*x;
  B[1] = B[0]*x;  
  B[2] = B[1]*x;
  B[3] = B[2]*x;

  result = A[0]*x+A[1]*B[0]+A[2]*B[1]+A[3]*B[2]+A[4]*B[3];
  return result;

char messages[5][16] = {
  "Heating sensor", "64 seconds", "Autozero", "Ready!", "ppm"};

int L;
float x;
float x0[5];
float x_initial;

LiquidCrystal lcd(12, 11,5, 4, 3, 2); // Wiring microcontroller - LCD:

const int pushbutton = 8; // Pushbutton (normally open). Stops initial heating time when it is pressed. .
const int analogPin = A0; // Reads sensor voltage as a float int the interval (0-1) corresponding to (0 - 3.3V).
float sensor_value_AnalogIn = 7; // Analog output to a multimeter or datalogger (1V = 1000 ppm).

void text_screen( char messages[],  int colum, int row) {
  lcd.setCursor( colum, row);

void setup() {
  text_screen(messages[0], 0, 0);
  text_screen(messages[1], 0, 1);

  for (int j = 0; j<4; j++)
    lcd.clear();  // Heating sensor 4x16 = 64 seconds
    text_screen(messages[0], 0, 0);

    for (int i = 0; i<16; i++){  
      if (pushbutton == 1){   // Pressing pushbutton stops initial heating and enters in measuring mode.

      lcd.setCursor(i, 1);

  text_screen(messages[2], 0, 0);

  float sensor_value_AnalogIn = 7; 

  for (int i=0; i<5; i++)
   x0[i]= analogRead(analogPin);

  x_initial = (x0[0]+x0[1]+x0[2]+x0[3]+x0[4])/5.0; // Autozero. Average of 5 initial measures.

  text_screen(messages[3], 0, 0);


void loop()  {
  x = analogRead(analogPin);

  x = (x-x_initial)*3.3; // Calculate real voltage. 
  float sensor_value_AnalogIn = x/1000;  

    x = 0;

  x = concentration(x);


  //Float to string conversion.

char buffer[10];      

  dtostrf(x, 10, 3, buffer);


  for (int i=0; i<strlen(buffer); i+=1)
 text_screen(messages[4], 10, 0); 


First thing. That code won't compile or auto format. Too many right curly braces.

When it works you appear to have a value returned by the concentration function. Is that what you want to turn into a percentage ? If so, what is it a percentage of ? What is your problem in turning the value into a percentage and displaying it ?

Why all the messing around converting a float to a string ?

Thanks for reply.

For the complilation it was no problem.
The value returned by the concentration function,i s that what i want to turn into a percentage.
For the percentage i want, i will make some mixture with HIGH and LOW concentartion of alcohol than transform the result according these concentartion to a %.

My problem is turning the value in percentage and displaying it.

LCD display a string for this reason i convert the result from float to a string.

I am sorry but I still don't understand what you say about your percentage. Would a value of 100 returned by the concentration() function be 100% ?

LCD display a string

Are you sure ?
What happens when you display a float using lcd.print() ?

thanks for reply,

when i use lcd.print() i receive ovf a lot of time

for the percent i want the result will be converted in % like 5% 6% ... not in number like 556585472200,00 it's very long number without any signification.


when i use lcd.print() i receive ovf a lot of time

What does that mean ? What do you get if you Serial.print(x) ?

for the percent i want the result will be converted in % like 5% 6% ... not in number like 556585472200,00 it's very long number without any signification.

I don't feel that we are getting anywhere with what you mean by a percentage. The number that you quote makes no sense whatsoever.

What exactly are you getting? Because it looks like you have an array of data, and you want to make that array into an actual number.
If that is so, then take that array and put it into atoi() , also no need for , just have "int number = atoi(Array);"
Another thing, it looks like you want a floating number, x.xx%, I think the Arduino IDE now has atof()... I think.

Check out this link, on how to use it. atof - C++ Reference

when i use lcd.print() i receive ovf a lot of time

That means OVerFlow.

The value is too big to print as the standard print algorithm uses unsigned longs to handle float.
So although a float can be 1E-38 .. 1E38 you can only print it between 1E-38 (given enough digits) and 4E9

To solve this I made a mod to be able print in scientific format,
see the thread - Proposed update for the printFloat code of print.cpp - #11 by robtillaart - Libraries - Arduino Forum -
( is in the attachment, do not forget to copy your original print.cpp and print.h to an other folder )


this is a float that cannot be printed as it is bigger that MAXLONG.
Question I have which number is 100%?
Percentage is a relative number x is so much %% of y e.g. 20 = 4 % of 500

to convert a float into % you may divide it by the number that represents 1% (in my example 1% is 5)

Question I have which number is 100%?

Let's hope you have more luck than me in finding out.