Go Down

Topic: Value = value * 1.8 and convert to string? (Read 436 times) previous topic - next topic

cabecinhas

Oct 27, 2013, 08:52 pm Last Edit: Oct 28, 2013, 01:50 pm by cabecinhas Reason: 1
Hi,  folks I am very new to arduino and I would like to see if anybody could help me on that...


Well.  I am reading the serial and sending the info to a string variable.

I am doing this because I have learned the serial is readed char by char  so I sent the values to a string variable.

This variable is comma delimited so,  using strtok  I can pick what I need.

One of the results is the speed(that comes in knots).
I need to convert this speed in km/h so it will be...

Speed = Speed * 1. 8

My question is...  How can I get the value(that is in String) , use the (*  1 . 8)
and convert it again into string?

Any help will be appreciated.


Thank you guys...

K5CZ

#1
Oct 27, 2013, 09:25 pm Last Edit: Oct 27, 2013, 09:27 pm by K5CZ Reason: 1
To convert float to a string I use this:

Code: [Select]

char buffer[16];
String  StringVariable = dtostrf(FloatVariable,5,2,buffer);


Also look for strtof and atof
Arduino Uno R3, Arduino Mega 2560 R3, Ethermega (R3), and some Ethernet Shields (R3), Chronodots, TMP36 sensors, DS18B20 sensors, LCDs/TFTs, RS232C to learn and play...

cabecinhas

#2
Oct 28, 2013, 02:43 am Last Edit: Oct 28, 2013, 01:50 pm by cabecinhas Reason: 1
Hi. I Did and the result was kind of splitted.. showing wrong values...

I just wanted to convert a String Variable into a float (because my value have decimal.)
After convert into a float, I need to do(value = value * 1.8)
After that, I need to convert into a String again because I need to print it.

---- heres the code I am using:
Code: [Select]
 
#include <SoftwareSerial.h>

SoftwareSerial mySerial(6, 7); // RX, TX

void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  Serial.println("Hi there!");
 
  String info_from_gps ="2";
// I want to multiply the "100" value that comes as String by 1.8
// So I want to obtain 3.6 from this value
// I think I will have to convert this value to float because there will have decimals on result , right?
/// Well after that I need to print the result(3.6), so I think I will need to convert the result as string, right?

// Could you please give me an example about how to do this?
   
}

void loop() // run over and over
{
  //
}





I need to convert the velocidade  to a String variable

Any Idea?

THank you so much

PaulS

The first thing that you need to clarify is if you are talking about strings or Strings. There is a world of difference between them.

Quote
heres the code I am using:

http://snippers-r-us.com might be able to help you (though I have my doubts). Here, we expect to see ALL of your code.

cabecinhas

#4
Oct 28, 2013, 01:49 pm Last Edit: Oct 28, 2013, 01:51 pm by cabecinhas Reason: 1
Hi, Paul. Thank you for asnwering me!

Here's a code that explais what I need to do..

Thank you very much.

Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial mySerial(6, 7); // RX, TX

void setup()  
{
 // Open serial communications and wait for port to open:
 Serial.begin(115200);
 Serial.println("Hi there!");
 
 String info ="2";
// I want to multiply the "2" value that comes as String by 1.8
// So I want to obtain 3.6 from this value
// I think I will have to convert this value to float because there will have decimals on result , right?
/// Well after that I need to print the result(3.6), so I think I will need to convert the result as string, right?

// Could you please give me an example about how to do this?
 
}

void loop() // run over and over
{
 //
}

AWOL

Ditch the String class, use C strings and "atof"to convert the string to decimal.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

cabecinhas


Ditch the String class, use C strings and "atof"to convert the string to decimal.


Hi. AWOL. Thank you for yuor help.

Could you give me an example for this and if possible, an example to transform the result in String again for me to be able to print it?

Thank you so much :)

PaulS

Quote
an example to transform the result in String again for me to be able to print it?

You shouldn't need to convert a string to a String to print it. If you do, get rid of the crippled class that insists that Strings are the only way.

As for an example of using atof(), I'm sure that google has examples.

Go Up