hello,
i have a code to convert float to string i want help to convert it to arduino code.
// Float to string conversion.
stringstream ss (stringstream::in | stringstream::out);
ss <<x;
string reading = ss.str();
L = reading.length();
thanks
hello,
i have a code to convert float to string i want help to convert it to arduino code.
// Float to string conversion.
stringstream ss (stringstream::in | stringstream::out);
ss <<x;
string reading = ss.str();
L = reading.length();
thanks
I think the closest equivalent would be:
float xx;
String reading = xx;
int L = reading.length();
thank you for your reply but the compiler gives this error message : conversion from 'float' to non-scalar type 'String' requested
Bummer. Looks like String only has integer constructors. I hoped it also had a float constructor.
Next thing to try is one of the library conversion routines: ftoa() meaning float to ASCII.
float fvar;
char buffer[15]; // you need some space
fvar = read_adc(0) * (5 / 1024.0);
ftoa(fvar, 2, buffer);
hello,
thanks for your reply.
this the sketch i modify it in order to convert it to Arduino ( UNO) but i have some prb to convert float to string...
it's a sketch for mq3 sensor in order to get the amount of alcohol in the air and convert it in ppm.
thank you for your help.
#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, 10, 5, 4, 3, 2); // Wiring microcontroller - LCD:
int pushbutton = 8; // Pushbutton (normally open). Stops initial heating time when it is pressed. .
int analogPin = 6; // Analog output to a multimeter or datalogger (1V = 1000 ppm).
void text_screen( char message[], int colum, int row) {
lcd.setCursor( colum, row);
lcd.print(message);
}
int main() {
int output = 0.0;
lcd.clear();
text_screen(messages[0], 0, 0);
text_screen(messages[1], 0, 1);
delay(2);
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(1);
}
}
lcd.clear();
text_screen(messages[2], 0, 0);
delay(2);
float sensor_value_AnalogIn = 20; // Reads sensor voltage as a float int the interval (0-1) corresponding to (0 - 3.3V).
for (int i=0; i<5; i++)
{
delay(2);
}
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(2);
while(1)
{
x = analogRead(sensor_value_AnalogIn);
x = (x-x_initial)*3.3; // Calculate real voltage.
if(x<0) x = 0;
x = concentration(x);
output = x/3000; // If analog readings do not match digital readings you can adjust this value (1000 ppm = 1V).
delay (0.1);
// Float to string conversion.
//stringstream ss (stringstream::in | stringstream::out);
//ss <<x;
//string reading = ss.str();
//L = reading.length();
char reading = ss;
L = reading.length();
char reading[4];
lcd.clear();
for (int i=0; i<L; i+=1)
{
lcd.setCursor(i,0);
lcd.write(reading[i]);
text_screen(messages[4], 10, 0);
}
delay(2);
}
}
some tip:
Concentration() can be written this way to minimize the math: 5 multiplications instead of 9
float concentration (float x)
{
const float A[] = { 2.71494E+02, -3.10999E+02, 6.85051E+02, -3.47587E+02, 7.47499E+01 };
return x * ( A[0] + x * ( A[1] + x * ( A[2] + x * ( A[3] + x * A[4] ) ) ) );
}
delay (0.1); will not work (param is an integer type) use DelayMicroseconds(100);
Please press the CTRL-T before copying code: it will reformat the layout to some standard format which makes it better readable.
This should get you a little closer:
#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, 10, 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 = A6; // Analog output to a multimeter or datalogger (1V = 1000 ppm).
void text_screen( char message[], int colum, int row) {
lcd.setCursor( colum, row);
lcd.print(message);
}
void setup() {
lcd.clear();
text_screen(messages[0], 0, 0);
text_screen(messages[1], 0, 1);
delay(2);
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(1);
}
}
lcd.clear();
text_screen(messages[2], 0, 0);
delay(2);
float sensor_value_AnalogIn = 20; // Reads sensor voltage as a float int the interval (0-1) corresponding to (0 - 3.3V).
for (int i=0; i<5; i++)
{
delay(2);
}
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(2);
}
void loop() {
x = analogRead(analogPin);
x = (x-x_initial)*3.3; // Calculate real voltage.
if(x<0)
x = 0;
x = concentration(x);
delayMicroseconds(100);
// Float to string conversion.
char buffer[15]; // you need some space
dtostrf(x, 16, 5, buffer);
lcd.clear();
for (int i=0; i<strlen(buffer); i++)
{
lcd.setCursor(i,0);
lcd.write(buffer[i]);
}
text_screen(messages[4], 10, 0);
}
using namespace std;
Why? You absolutely do not need the entire std namespace. As a matter of fact, you don't need ANY of it.
Thanks o lot for your help.
Best Regards ![]()
turw:
hello,thanks for your reply.
this the sketch i modify it in order to convert it to Arduino ( UNO) but i have some prb to convert float to string...
it's a sketch for mq3 sensor in order to get the amount of alcohol in the air and convert it in ppm.thank you for your help.
#include "arduino.h"
#include <LiquidCrystal.h>
#include
#include
#include
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, 10, 5, 4, 3, 2); // Wiring microcontroller - LCD:
int pushbutton = 8; // Pushbutton (normally open). Stops initial heating time when it is pressed. .
int analogPin = 6; // Analog output to a multimeter or datalogger (1V = 1000 ppm).
void text_screen( char message[], int colum, int row) {
lcd.setCursor( colum, row);
lcd.print(message);
}
int main() {
int output = 0.0;
lcd.clear();
text_screen(messages[0], 0, 0);
text_screen(messages[1], 0, 1);
delay(2);
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(1);
}
}
lcd.clear();
text_screen(messages[2], 0, 0);
delay(2);
float sensor_value_AnalogIn = 20; // Reads sensor voltage as a float int the interval (0-1) corresponding to (0 - 3.3V).
for (int i=0; i<5; i++)
{
delay(2);
}
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(2);
while(1)
{
x = analogRead(sensor_value_AnalogIn);
x = (x-x_initial)*3.3; // Calculate real voltage.
if(x<0) x = 0;
x = concentration(x);
output = x/3000; // If analog readings do not match digital readings you can adjust this value (1000 ppm = 1V).
delay (0.1);
// Float to string conversion.
//stringstream ss (stringstream::in | stringstream::out);
//ss <<x;
//string reading = ss.str();
//L = reading.length();
char reading = ss;
L = reading.length();
char reading[4];
lcd.clear();
for (int i=0; i<L; i+=1)
{
lcd.setCursor(i,0);
lcd.write(reading[i]);
text_screen(messages[4], 10, 0);
}
delay(2);
}
}
by the way how did you got sstream library working on arduino ?
I included and get the error :
fatal error: sstream: No such file or directory
#include
^
compilation terminated.