SoftSerial and Float

Is there a way that mySerial.print() (softserial lib) can print a float number in ASCII, similar to the way the hardware serial() function works?

I have been testing a temperature sensor from Modern Devices and the application works, by sending the data over the serial port to my arduino serial monitor, running on my PC. But that uses a different serial library (SoftwareSerial).

This is the code for the temp sensor module:

#include "Wire.h"
#include <LibTemperature.h>

LibTemperature temp = LibTemperature(0);

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

void loop() {
 Serial.print("Temp: ");
 Serial.print(temp.GetTemperature());
 Serial.println(" degC");
 delay(100);
}

I now wish to send this information to my TouchShield, using the SoftwareSerial library (myPrint.serial(), for instance). However the same instructions mySerial.print() doesn’t compile and crashes with an ambiguious float type, if I send a float number. I know that the other library handles floats because I have tested it using the built in library. I have already tested my communications between the arduino and the TSS, using the software lib and it works fine, using ASCII. But now I want to transfer the float number to the TSS and can’t see how to do it.

And this was my code for doing that:

#include "Wire.h"
#include <LibTemperature.h>

#define RX_PIN 3
#define TX_PIN 2

LibTemperature temp = LibTemperature(0);

SoftwareSerial mySerial = SoftwareSerial(RX_PIN, TX_PIN);

void setup()
{
   pinMode(RX_PIN, INPUT);
   pinMode(TX_PIN, OUTPUT);

   mySerial.begin(9600);
}

int iBtn;
int iLastBtn = 0;

void loop()
{
   mySerial.print("Temp: ");
  mySerial.print(temp.GetTemperature());
  mySerial.println(" degC");
  delay(100);
  
}

temp.GetTemperature() returns a float. It works in the first instance but won’t compile in the second because of the float type.

There doesn’t seem to be a conversion from float to ascii and the softserial liberary doesn’t make the conversion like the print() function does using the main serial liberary.

Anyone know how to pass a float number over the SoftwareSerial port, similar to the way it works when comunicating on the USB port, using the print() function?

Just BTW: Temperature sensors have a precision of around 1K, rarely outside the range -50 °C upto 120°C. I wonder what use Floats can have in such a situation, where a Byte seems fine...

Just BTW: Temperature sensors have a precision of around 1K, rarely outside the range -50 °C upto 120°C. I wonder what use Floats can have in such a situation, where a Byte seems fine…

Doesn’t really matter the use, the function returns a type float and that’s what I have to deal with. :wink:

I did look at the core print function vs. the print function in SoftSerial and see where they do the conversion. I guess I can just add that to the SoftSerial library.

BTW, I accidentally put this in the hardware forum. Probably should be moved to the software forum.

You could switch to NewSoftSerial, instead of SoftwareSerial. NewSoftSerial derives from Print, the same as the hardware Serial class.

@deSilva You might want to look at the range of values that can be stored in a byte. Need a bigger hammer to get -50 into a byte.

Need a bigger hammer to get -50 into a byte.

I must have a really big hammer - my bytes store -128 to 127. 8-)

I must have a really big hammer - my bytes store -128 to 127.

Well the Arduino defined byte type is:

"A byte stores an 8-bit unsigned number, from 0 to 255. "

Lefty

Who's talking about datatypes? I'm talking about memory, as in "my Arduino has 1k bytes of SRAM".

Need a bigger hammer to get -50 into a byte.

Who's talking about datatypes? I'm talking about memory, as in "my Arduino has 1k bytes of SRAM".

I must have a really big hammer - my bytes store -128 to 127.

Never heard of a negative address value, nor a 1KB address value stored in a byte. ;)

Lefty

Never heard of a negative address

Really?
You have lived a sheltered life.
The Inmos transputer had an address space that began at MOSTNEG INT.

Also one of the joys of C is that negative indices are allowed in array accesses - really useful for table lookups.

OK, I give up trying to understand the topic and responses, it’s beyond my sheltered exposure. I thought it was about the range of values that an Arduino byte could hold. Forgot all about the Inmos transputer. :wink:

Lefty

..nor a 1KB address value stored in a byte.

But this would be not sooo unusual. The program area of theAVRs is addressed as 16 bit chunks, so 1/2 KB fit.. The CDC mainframes I worked with some decades ago had 60 bit words per address, so one 8 bit-byte would address 256*60/8 = 1920 bytes => Bingo!

I said 8-bit-bytes because CDC considered 6 bits to be a byte; they had to be parsimonious with core memory that time....

Who's talking about datatypes? I'm talking about memory, as in "my Arduino has 1k bytes of SRAM".

I'm running this on a Mega, so I have 8K. ;D

I tried NewSoftSerial. Seems to work. At least it doesn't give me an error on the "float". However, it's still not properly. Might be the way I'm using the function.

I tested it first with:

mySerial.print("12.34"); and that works. Then with mySerial.print((float) 12.34); and that works. But when I try mySerial.print(GetTemperature()); it doesn't pass anything.

However, I'll have more time to test it tomorrow and see what's going on. Only spent about 10 minutes with it today. :)

Just had a look at the code... deep sigh Seems the author also worked with a CDC6600 some time age...

float LibTemperature::GetTemperature(void) {
    uint8_t in[2];
    float frac = 0.0;
    uint8_t bit;

    setPtrLoc(0x00);                //high-byte
    in[0] = getRegisterValue();
    in[0] = getRegisterValue();

    setPtrLoc(0x10);                //low-byte
    in[1] = getRegisterValue();
    in[1] = getRegisterValue();
    in[1] >>=4;                     //shift-off the unused bits

    /* Assemble the fraction */
    bit = in[1] & 0x01;
    frac += (bit * 0.5) * (bit * 0.5) * (bit * 0.5) * (bit * 0.5);

    in[1] >>= 1;
    bit = in[1] & 0x01;
    frac += (bit * 0.5) * (bit * 0.5) * (bit * 0.5);

    in[1] >>= 1;
    bit = in[1] & 0x01;
    frac += (bit * 0.5) * (bit * 0.5);

    in[1] >>= 1;
    bit = in[1] & 0x01;
    frac += (bit * 0.5);

    /* Add the MSB to the fraction */
    frac += in[0];

    return frac;
}