Pages: [1]   Go Down
Author Topic: How can i change string to a percent for LCD display ?  (Read 621 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

here the code:

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);
  lcd.print(messages);
}



void setup() {
  lcd.begin(16,2);
  lcd.clear();
  text_screen(messages[0], 0, 0);
  text_screen(messages[1], 0, 1);
  delay(2000);

  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.
        break;
      }

      lcd.setCursor(i, 1);
      lcd.write(62);
      delay(1000);
    }
  }


  lcd.clear();
  text_screen(messages[2], 0, 0);
  delay(1000);


  float sensor_value_AnalogIn = 7;

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

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

  lcd.clear();
  text_screen(messages[3], 0, 0);
  delay(2000);

 
}

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

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

  if(x<0)
    x = 0;

  x = concentration(x);
 

  delayMicroseconds(100);

  //Float to string conversion.

char buffer[10];     

  dtostrf(x, 10, 3, buffer);

 lcd.clear();

  for (int i=0; i<strlen(buffer); i+=1)
  {
    lcd.setCursor(i,0);
    lcd.write(buffer[i]);
 
  }
   
 text_screen(messages[4], 10, 0);

 }
 
  delay(2000);
 
}

Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 108
Posts: 4021
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 108
Posts: 4021
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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% ?

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Thanks
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 108
Posts: 4021
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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) ?

Quote
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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. http://www.cplusplus.com/reference/cstdlib/atof/
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 - http://forum.arduino.cc/index.php?topic=166041.msg1241170#msg1241170 -
(print.zip is in the attachment, do not forget to copy your original print.cpp and print.h to an other folder )

Quote
556585472200,00
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)
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 108
Posts: 4021
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Question I have which number is 100%?
Let's hope you have more luck than me in finding out.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: