Me again, I'm now trying to create an char array from float value but fixed with a space.
I've tried the safe string library and dtostrf function
This is my test code I only need 1 value but added for so I can see all at once without having to change each value and upload each time.
/*
Format: <sign> XXX.XX
Famtat: -, ,0,1,.,3,6
formaat: byte 0 either +/- sign
foarmat: byte 1 empty space
foarmat: byte 2 = 0 (this can be value from 0-9)
foarmat: byte 3 = 1 (this can be value from 0-9)
foarmat: byte 4 = . decimal point not moved
foarmat: byte 5 = 3 (this can be value from 0-9)
foarmat: byte 4 = 6 (this can be value from 0-9)
*/
#include "SafeString.h"
const byte numChars = 9; //Number of bits to get
float floatVal1 = - 1.36; //example of negative counting
float floatVal2 = 1.36; // example of positive couning
float floatVal3 = - 11.36; //example of negative counting
float floatVal4 = 11.36; // example of positive couning
char Incoming1[numChars]; // an array to store the received data
char Incoming2[numChars]; // an array to store the received data
char Incoming3[numChars]; // an array to store the received data
char Incoming4[numChars]; // an array to store the received data
void setup() {
Serial.begin(115200);
for (int i = 3; i > 0; i--) {
Serial.print(' '); Serial.print(i);
delay(500);
Serial.println();
}
/*
SafeString::setOutput(Serial); // for error msgs
createSafeStringFromCharArray(sfReading1, Incoming1); // wraps in a SafeString and keeps the current text if any
sfReading1 = "";
sfReading1.print(floatVal1, 2);
createSafeStringFromCharArray(sfReading2, Incoming2); // wraps in a SafeString and keeps the current text if any
sfReading2 = "";
sfReading2.print(floatVal2, 2);
createSafeStringFromCharArray(sfReading3, Incoming3); // wraps in a SafeString and keeps the current text if any
sfReading3 = "";
sfReading3.print(floatVal3, 2);
createSafeStringFromCharArray(sfReading4, Incoming4); // wraps in a SafeString and keeps the current text if any
sfReading4 = "";
sfReading4.print(floatVal4, 2);
Serial.print("SFString floatVal 1:");
Serial.print(Incoming1 ); // Print btye 2
Serial.println();
Serial.print("SFStringe floatVal 2:");
Serial.print(Incoming2 ); // Print btye 2
Serial.println();
Serial.print("SFString floatVal 3:");
Serial.print(Incoming3 ); // Print btye 2
Serial.println();
Serial.print("SFString floatVal 4:");
Serial.print(Incoming4 ); // Print btye 2
Serial.println();
*/
dtostrf(floatVal1, 7, 2, Incoming1); // tried chaging 7 to 6 to 5
dtostrf(floatVal2, 7, 2, Incoming2); // L
dtostrf(floatVal3, 7, 2, Incoming3); // L
dtostrf(floatVal4, 7, 2, Incoming4); // L
Serial.print("dtstore floatVal 1:");
Serial.print(Incoming1 ); // Print btye 2
Serial.println();
Serial.print("dtstore floatVal 2:");
Serial.print(Incoming2 ); // Print btye 2
Serial.println();
Serial.print("dtstore floatVal 3:");
Serial.print(Incoming3 ); // Print btye 2
Serial.println();
Serial.print("dtstore floatVal 4:");
Serial.print(Incoming4 ); // Print btye 2
Serial.println();
}
void loop() {
}
This is the output set to seven and five just moves it closer to the beginning and when the value is negative I lose some digits.
dtstore floatVal 1: -1.36
dtstore floatVal 2: 1.36
dtstore floatVal 3: -11.36
dtstore floatVal 4: 11.36
But I need to it be like this for the output: Note _ is just an empty space.
dtstore floatVal 1:-_01.36 //negative value lower than 10.00
dtstore floatVal 2:+_01.36 // positive value lower than 10.00
dtstore floatVal 3:-_11.36 //negative value higher than 10.00
dtstore floatVal 4:+_11.36 //negative value higher than 10.00
It's kind of working but I need the space in between and showing - sign (if negative value). I get the same result if I use safestring library.
Is there an easier way to get the float value to match my required output ?