Converting float into a character

Hi arduino members,

I have a few problem with my code. I don't know what is the problem but the value of my gas sensor which is declared and already calibrated doesnt converted into a character. So for you not to be confused because i am, here is my sample code:

 sensorValue = analogRead(A0);

  float gas = MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG);
  float smoke= MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE);
  float co = MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO);


 if(gas,smoke,co != 0){

 Serial.print("Gas detected");
 delay(1000);
 char sensor[200];


 sprintf(sensor,"gas = %f smoke = %f co = %f",gas,smoke,co);
 Serial.println("SENDING MESSAGE! ...");
 Serial.println(sensor);
^^^^^^^^^^
this is problem i've encountered, pls see the output below.

 GSMTEST.sendSMS(PHONE_NUMBER, sensor);
 delay(1000);

  //send message
  Serial.print("LPG:"); 
  Serial.print(gas);
  Serial.print( "ppm" );
  Serial.print("    ");   
  Serial.print("CO:"); 
  Serial.print(co);
  Serial.print( "ppm" );
  Serial.print("    ");   
  Serial.print("SMOKE:"); 
  Serial.print(smoke);
  Serial.print( "ppm" );
  Serial.print("\n");
  delay(1000);

}

The output that i've encounter is like this,

Output: gas = ? smoke = ? co = ?

Pls help me

if(gas,smoke,co != 0)Tell us about that line.

You do know that by default, %f is disabled for the 8 bitters?

Use dtostrf instead

It is logically says that

if(gas || smoke || co !=0){
//Codes
}

Im sorry but i dont know about that %f with 8 bit something.

Use dtostrf instead

I tried that dtostrf thing, but it the arduino IDE doesnt recognize it, is there anything i can do with it? or something to declare in global for that decimal to string(dtostrf) will work?

Thanks so much for quick response. :slight_smile:

It is logically says that

Code: [Select]

if(gas || smoke || co !=0){
//Codes
}

It does?

I tried that dtostrf thing, but it the arduino IDE doesnt recognize it

That surprises me greatly.

It does?

yes. Probably it works for awhile but i'll change it. But coding for me as a first timer huh? :confused:

Is there anything that i can do?

You can try dtostrf again

You can try dtostrf again

Im sorry, but can you please give me an example? sorry for being noob. The link you give to me seems to be out of this world for me. Can you relate it to my codes?

Thanks :slight_smile:

Put "dtostrf Arduino example" into Google, and make an offering to a deity of your choice.

if(gas,smoke,co != 0)

is the same as

if(co != 0)

Example:

void setup() {
  Serial.begin(250000);
  if (1, 1, 1, 1, 1, 1, 1, 0) {
    Serial.println(F("True"));
  } else {
    Serial.println(F("False"));
  }
}
void loop() {}
False

and you get a bunch of warnings for free

somewhere\CommaOp.ino:3:10: warning: left operand of comma operator has no effect [-Wunused-value]
   if (1, 1, 1, 1, 1, 1, 1, 0) {
          ^
somewhere\CommaOp.ino:3:13: warning: right operand of comma operator has no effect [-Wunused-value]
   if (1, 1, 1, 1, 1, 1, 1, 0) {
             ^
somewhere\CommaOp.ino:3:16: warning: right operand of comma operator has no effect [-Wunused-value]
   if (1, 1, 1, 1, 1, 1, 1, 0) {
                ^
somewhere\CommaOp.ino:3:19: warning: right operand of comma operator has no effect [-Wunused-value]
   if (1, 1, 1, 1, 1, 1, 1, 0) {
                   ^
somewhere\CommaOp.ino:3:22: warning: right operand of comma operator has no effect [-Wunused-value]
   if (1, 1, 1, 1, 1, 1, 1, 0) {
                      ^
somewhere\CommaOp.ino:3:25: warning: right operand of comma operator has no effect [-Wunused-value]
   if (1, 1, 1, 1, 1, 1, 1, 0) {
                         ^
somewhere\CommaOp.ino:3:28: warning: right operand of comma operator has no effect [-Wunused-value]
   if (1, 1, 1, 1, 1, 1, 1, 0) {
                            ^

I'm curious as to what you'd hope to glean about your problem by reading my forum profile.

I'm curious as to what you'd hope to glean about your problem by reading my forum profile.

Interesting, I am just exploring about this forum because im new to this thing. Glad i found a better place about my problem. But still im searching for luck,

is dtostrf can have many float variable? like this thing

char sensor[200];
 dtostrf(gas,smoke,co,1,2,sensor);

and i've got an error of

invalid conversion from 'int' to 'char*' [-fpermissive]

thanks

is dtostrf can have many float variable?

No. One conversion per call.

No. One conversion per call.

Hi AWOL,

Thank you so much! I appreciate ur help although i feel you look some kind of irritated or what. but thanks anyways! :wink:

and for Whandall, i'll do my very best to fix my code. thanks!

Your usage of dtostrf is irritating, as AWOL gave you a link to the documentation of the function.

But still im searching for luck,

Well, stop. Programming is NOT about luck. It involves logic and the ability to read documentation and TRY things.

danArd:
ur help

I wasn't irritated, but I am now.

The handy thing about the World Wide Web is that it comes with handy search tools, and when someone gives you pretty specific search terms, "Bingo!", things happen.
Or maybe not.

OK further hint, sprintf doesn't support %f, but it does support %s.
Or, go lightweight, don't use sprintf, use strcat instead.

Hey guys, i already solved this. That's why im thanking AWOL for this

I wasn't irritated, but I am now.

Sorry for that :slight_smile: Chill and wanna drink some beer?

so here sharing my code if ever there's other people here that have same cases with me

if(sensorValue != 0){
         int y=0;
         Serial.print("Gas detected");
         while(y != 3){
         static char sensor[10];
         static char sensor1[10];
         static char sensor2[10];
         dtostrf(gas,1,2,sensor);
         dtostrf(smoke,1,2,sensor1);
         dtostrf(co,1,2,sensor2);
         Serial.println("SENDING MESSAGE! ...");
         char finalRead[50];
         sprintf(finalRead,"Gas= %s ppm, smoke = %s ppm, co = %s ppm",sensor,sensor1,sensor2);
         Serial.println(finalRead);
         GSMTEST.sendSMS(PHONE_NUMBER,  finalRead );
         tone(BUZZER,1000);
         delay(3000);
         noTone(BUZZER);
         delay(1000);
         
         y++;
 }
  //send message
  }

Thanks everyone and have a nice day ahead!

i wanna

The irritation just keeps growing.

The irritation just keeps growing.

I think you need to drink beer then. toast! :slight_smile:

   static char sensor[10];
         static char sensor1[10];
         static char sensor2[10];

Why “static”?