0
votes

I'm trying to send position of my robot through wireless module with Arduino using Software Serial library. I found that it can only send 1 byte at time. I can't send more than number 255 and I need to send floats till 40000. How I can do that?

Here is an example of my transmitter:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
mySerial.begin(9600);
}

void loop() // run over and over    
{       
  float i=40000;
mySerial.write(i);
  //Serial.println(i);}    
}

my reciever

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); // RX, TX
int i=0;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
mySerial.begin(9600);    
}

void loop() {
if(mySerial.available()){
  i=mySerial.read();
Serial.println(i);    
}
}
1

1 Answers

1
votes

You have to use Serial.println(floatVal); to send it in plain text with any white space separator and receive it by Serial.parseFloat() method.

The write method is good for sending raw data (single character, c-string or some buffer)

If you really want sending floats in its binary form, you have to pass it as an buffer: Serial.write((uint8_t*)&floatVal, sizeof(intVal));. And on the receiver side you have to read whole float too. The read() method reads only one character.