Jump to content
maksms82

dtostrf in Energia

Recommended Posts

I have not looked at it in detail but it seems that sprintf() for floats is broken in Arduino/AVR. It seems that dtostrf() was implemented to offered as an alternative.

The details are here: http://dereenigne.org/arduino/arduino-float-to-string

 

On Stellaris sprintf() for floats is fully functional and you should use this to convert floats to strings.

// Allocate enough to store PI with with 4 decimals.
char str[5];

void setup()
{
  Serial.begin(115200);

  Serial.print("PI in a string: ");
  // Print PI to a string with 4 decimals
  sprintf(str, "%.4f", PI);
  // Print the string to the serial terminal
  Serial.println(str);
}

void loop()
{
 // Nothing to do here
}

Share this post


Link to post
Share on other sites

hi,

 

Thank you for code.. it works ok  but when i use it  in this code :

/* Stellaris LM4F */


#include <Enrf24.h>
#include <nRF24L01.h>
#include <string.h>
#include <SPI.h>

Enrf24 radio(PA_5,PA_6,PA_7);  // CE, CSN, IRQ + SCK/MOSI/MISO ==> PB_4/PB_7/PB_6

char str[5];


char txaddr[] = {'d','e','v','-','0'}; 
//int intvar;
const char *str_on = "OK";


//void dump_radio_status_to_serialport(uint8_t);

void setup() {
  Serial.begin(9600);

  SPI.setModule(2); // SPI 2
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(1);

  radio.begin();  // Defaults 1Mbps, channel 0, max TX power
  //dump_radio_status_to_serialport(radio.radioState());

  radio.setTXaddress((void*)txaddr);
}

void loop()
{  
  sprintf(str, "%.4f", PI);
  radio.print(str);
  radio.flush();  // Force transmit (don't wait for any more data)
  delay(500);
  
}


Nothing on the receiver side.

 

Google didn't help me .....

 

Thank you

 

Salutations

 

-Bernard

Share this post


Link to post
Share on other sites

Hi again,

 

I have found another way to convert float to string :

void setup()
{
  Serial.begin(9600);
}

char buffer[12];  
                  
void loop()
{
  float value = 150.51;
  itoa(int(value), buffer, 10);
  int dec = (value - (int)value) * 100;
  itoa(abs(dec), buffer, 10);
  Serial.println( value);  
  delay(1000);
}

It compiles on msp430G2553, F5529  but itoa is unknown when compiling on LM4F120 and Tiva C

 

Salutations

Share this post


Link to post
Share on other sites

itoa is not a standard function. e.g it is not included in stdlib.h. The libc for msp430 shipped with Energia implements a custom function for itoa. The libc included in the TivaC/Stellaris arm toolchain does not.

 

I would stick with the sprintf version since you already have that working. If Serial.print() prints the right value then I would check the Enrf24 code.

Where did the Enrf24 library come from?

 

Edit: More details here: http://stackoverflow.com/questions/10162733/atoi-is-a-standard-function-but-itoa-is-not-why

Share this post


Link to post
Share on other sites

Enrf24 is a library I wrote to support the Nordic nRF24L01+ in Energia (there are other Arduino libs out there that do this, but I wanted to take a stab at it "my way" after becoming very familiar with the hardware.)

Share this post


Link to post
Share on other sites

Hi,

 

@@energia : Thank you for information. I have tried to use sprintf but,  when using it,  program crashes and I don't know why .

 

I have found on the web   itoa  code that works .

 

@@spirilis : thank you very much for this nice library , it helps me a lot in my wireless domotic project.

 

Salutations.

Share this post


Link to post
Share on other sites

Hello,

 

I finally got sprintf working. I am able to send and receive datas from BMP085

 

 

here is my working code:

/* Stellaris LM4F120
   nRF24L01 TX
   BMP085   
   SPI module 2
   I2C module 0
   21/01/2014
*/
#include<Wire.h>
#include "BMP085_t.h"  // in a IDE tab .. template from chicken
#include <Enrf24.h>
#include <nRF24L01.h>
#include <string.h>
#include <SPI.h>

Enrf24 radio(PA_5,PA_6,PA_7);  // CE, CSN, IRQ + SCK/MOSI/MISO ==> PB_4/PB_7/PB_6

char txaddr[] = {'d','e','v','-','0','\0'}; 

BMP085<0> PSensor;  

void setup() {
  Serial.begin(9600);
  Wire.setModule(0);
  Wire.begin(); // initialize I2C that connects to sensor
  PSensor.begin(); // initalize pressure sensor
  SPI.setModule(2); // SPI 2
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(1);

  radio.begin();  // Defaults 1Mbps, channel 0, max TX power
  radio.setTXaddress((void*)txaddr);
}

//************************************************************************

void loop()
{ 
  char str[5];
  long value;
  PSensor.refresh(); // read  sensor data BMP085
  PSensor.calculate();   
   
  value = ((PSensor.pressure+50)/100);
  sprintf(str, "%.2d", value); 
  radio.print(str);
  
  radio.print("---");
  
  value = (PSensor.temperature/10);
  sprintf(str, "%.2d", value);
   radio.print(str);
  
  
  radio.flush();  // Force transmit (don't wait for any more data)
  delay(1000);
  
}
//*************************************************************************

LM4F120 on the receiver side.

 

Thank you all who helped me.

 

Salutations

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×