maksms82 0 Posted May 31, 2013 Share Posted May 31, 2013 Is supported in Energia dtostrf? Quote Link to post Share on other sites
Bernard 7 Posted January 19, 2014 Share Posted January 19, 2014 Hi, same problem here ... dtostrf is ok on arduino as a newbee I would be happy to get information Thank you Quote Link to post Share on other sites
spirilis 1,265 Posted January 19, 2014 Share Posted January 19, 2014 Huh, doing a quick google search, apparently dtostrf is a unique function that only comes with the AVR libc. Energia probably doesn't support it but it doesn't sound hard to emulate. Bernard 1 Quote Link to post Share on other sites
energia 485 Posted January 19, 2014 Share Posted January 19, 2014 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 } spirilis and Bernard 2 Quote Link to post Share on other sites
Bernard 7 Posted January 19, 2014 Share Posted January 19, 2014 Hi, Thank you very much spirilis and Energia Salutations Quote Link to post Share on other sites
Bernard 7 Posted January 21, 2014 Share Posted January 21, 2014 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 Quote Link to post Share on other sites
spirilis 1,265 Posted January 21, 2014 Share Posted January 21, 2014 Have you established that data is getting over to the receiver side? E.g. radio.print("Hi!") Quote Link to post Share on other sites
Bernard 7 Posted January 21, 2014 Share Posted January 21, 2014 Hi, I am sorry and apologize for that, I was not clear enough . Sending a string is ok . Thank you -Bernard Quote Link to post Share on other sites
Bernard 7 Posted January 21, 2014 Share Posted January 21, 2014 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 Quote Link to post Share on other sites
energia 485 Posted January 21, 2014 Share Posted January 21, 2014 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 Quote Link to post Share on other sites
spirilis 1,265 Posted January 21, 2014 Share Posted January 21, 2014 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.) Quote Link to post Share on other sites
Bernard 7 Posted January 21, 2014 Share Posted January 21, 2014 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. Quote Link to post Share on other sites
energia 485 Posted January 22, 2014 Share Posted January 22, 2014 Thanks for updating the post! What was crashing it? sprintf or something else? sprintf should not cause any issues. If it does then it is a bug and needs to be looked at. Quote Link to post Share on other sites
Bernard 7 Posted January 22, 2014 Share Posted January 22, 2014 Hi, I am sorry, sprintf is ok now, I did not allocate enough for buffer . Thank you and salutations. Quote Link to post Share on other sites
Bernard 7 Posted January 22, 2014 Share Posted January 22, 2014 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 spirilis 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.