Jump to content
43oh

Recommended Posts

  • 7 months later...

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
}
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

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

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

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

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...