Oi Jovens! Tudo bem?
Esse dias estava querendo converter um dado float para string, para transmitir esse dados com outros dados concatenados, e tal dado concatenado iria enviar para Xbee. Mas, dei uma boa busca na net e vi muita coisa complicada e muita coisa não funcionou. Mas então consegui chegar a solução assim:
1º) Baixe a Biblioteca FloatToString : FloatToString.zip
2º) Adicione a biblioteca (Na IDE do Arduino vá em: "Sketch/Importar biblioteca/Add Libary" e selecione o arquivo acima que você baixou .zip. OU Vá na Pasta Arduino\libraries e descompacte lá.
3º)Feche e Abra a IDE.
4º)Abra esse exemplo: examples.zip
Olha no exemplo:
char buffer[25];
String s = floatToString(buffer, 1000000.321, 5)
Explicação:
Comentar
NÃO FUNCIONA
Galera, se vcs simplesmente baixarem essa lib não vai funcionar
Como o próprio FELIPE falou, tem que editar a biblioteca
Eu demorei um pouco pra entender, mas depois entendi e vou explicar mais detalhadamente.
Primeiro vocês tem que abrir o local aonde a lib está instalada, se é na pasta C:\Program Files (x86)\Arduino\libraries ou em C:\Users\Seu User\Documents\Arduino\libraries
Depois, abram a pasta FloatToString
Dentro dela está o arquivo FloatToString.h
Abram ele com o editor de texto (QUALQUER UM, EX: SUBLIME, BLOCO DE NOTAS, NOTEPAD++)
Selecionem todo o código que está lá dentro e apaguem.
Depois, peguem o código que está aqui em baixo e colem lá dentro, aí é só salvar e rodar que vai estar funcionando certinho.
// floatToString.h
//
// Tim Hirzel
// tim@growdown.com
// March 2008
// float to string
//
// If you don't save this as a .h, you will want to remove the default arguments
// uncomment this first line, and swap it for the next. I don't think keyword arguments compile in .pde files
//char * floatToString(char * outstr, float value, int places, int minwidth=, bool rightjustify) {
char * floatToString(char * outstr, float value, int places, int minwidth=0, bool rightjustify=false) {
// this is used to write a float value to string, outstr. oustr is also the return value.
int digit;
float tens = 0.1;
int tenscount = 0;
int i;
float tempfloat = value;
int c = 0;
int charcount = 1;
int extra = 0;
// make sure we round properly. this could use pow from <math.h>, but doesn't seem worth the import
// if this rounding step isn't here, the value 54.321 prints as 54.3209
// calculate rounding term d: 0.5/pow(10,places)
float d = 0.5;
if (value < 0)
d *= -1.0;
// divide by ten for each decimal place
for (i = 0; i < places; i++)
d/= 10.0;
// this small addition, combined with truncation will round our values properly
tempfloat += d;
// first get value tens to be the large power of ten less than value
if (value < 0)
tempfloat *= -1.0;
while ((tens * 10.0) <= tempfloat) {
tens *= 10.0;
tenscount += 1;
}
if (tenscount > 0)
charcount += tenscount;
else
charcount += 1;
if (value < 0)
charcount += 1;
charcount += 1 + places;
minwidth += 1; // both count the null final character
if (minwidth > charcount){
extra = minwidth - charcount;
charcount = minwidth;
}
if (extra > 0 and rightjustify) {
for (int i = 0; i< extra; i++) {
outstr[c++] = ' ';
}
}
// write out the negative if needed
if (value < 0)
outstr[c++] = '-';
if (tenscount == 0)
outstr[c++] = '0';
for (i=0; i< tenscount; i++) {
digit = (int) (tempfloat/tens);
itoa(digit, &outstr[c++], 10);
tempfloat = tempfloat - ((float)digit * tens);
tens /= 10.0;
}
// if no places after decimal, stop now and return
// otherwise, write the point and continue on
if (places > 0)
outstr[c++] = '.';
// now write out each decimal place by shifting digits one by one into the ones place and writing the truncated value
for (i = 0; i < places; i++) {
tempfloat *= 10.0;
digit = (int) tempfloat;
itoa(digit, &outstr[c++], 10);
// once written, subtract off that digit
tempfloat = tempfloat - (float) digit;
}
if (extra > 0 and not rightjustify) {
for (int i = 0; i< extra; i++) {
outstr[c++] = ' ';
}
}
outstr[c++] = '\0';
return outstr;
}
OBS: Para tirar a prova real, é só abrir a FloatToString.h, na primeira linha vai ter um link pra um site, no final do link tem um ->
Copiem todo o link junto com essa seta, colem em um navegador
Lá estará o mesmo código que eu colei acima (Peguei o código de lá)
ABÇ
ola
sei que o topico é antigo, mas tenho uma duvida
hoje transmito meu float usando o xbee em 4 bytes
assim
payload[1] = (b >> 24) & 0xFF;
payload[2] = (b >> 16) & 0xFF;
payload[3] = (b >> 8) & 0xFF;
payload[4]= b & 0xFF;
se converte-lo para uma string é somente usar o código floattostring...e substituir a variável do meu código pela variavel do floattostring?? envio como 4 bytes?? estou usando o labview no receptor para "tratar"os dados..
obrigado
Ótima solução amigo, mas estou com problemas com a biblioteca
C:\Program Files (x86)\Arduino\libraries\FloatToString/FloatToString.h:2:1: error: expected unqualified-id before '<' token
se você puder me ajudar eu agradeceria, obrigado!
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por
Você precisa ser um membro de Laboratorio de Garagem (arduino, eletrônica, robotica, hacking) para adicionar comentários!
Entrar em Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)