SoftwareSerial.h>

Hi all,
Im trying to receive data (numbers with decimals points) from the master Arduino Board to Salve Arduino board.

  1. I need data tranfer with 2 decimals points so I tired useing wire.h library but it transfer only Integers. So it cancels out decimals.

  2. Now I'm trying with softwareSerial.h, this gives me with decimals points. but cannot use that Char number to control other components. please find codes and help me out.

Master code,

#include <SoftwareSerial.h>
SoftwareSerial softSerial(8, 9);

void setup()  
{
  softSerial.begin(9600);
  Serial.begin(9600);
} 
void loop()  
{ 
  softSerial.print(251313.212);
  Serial.println(251313.212);
  delay(1000);

  
}



Slave code with Char serial print ( cannot print with new line, then it prints vertically (but thats not the main issue) 

#include <SoftwareSerial.h>
SoftwareSerial softSerial(8, 9);

char number = ' ';
int LED = 2;

void setup()
{
softSerial.begin(9600);
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop()
{
if (softSerial.available())
{
char number = softSerial.read();

Serial.print(number);

}
}


The issue is this..

#include <SoftwareSerial.h>
SoftwareSerial softSerial(8, 9);

char number  = ' ';
int LED = 2;

void setup()
{
  softSerial.begin(9600);
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}
void loop()
{
  if (softSerial.available())
  {
    char number = softSerial.read();

    if (number >50)
    {
       Serial.print(number);
    }
  }
}

Looks that you have not understood the concept of data types in programming languages...

A variable of type char can only hold one byte (a value of 0 up to 255.

Although this is always a number, its interpretation by a compiler may be a character (depending on how the code uses it).

Serial communication always transmits byte by byte (or character by character). If you transmit e.g. "3.141" using print or println the receiver gets single characters (numbers in accordance with a table called ASCII = American Standard Code for Information Interchange). see https://theasciicode.com.ar/ascii-printable-characters/number-three-ascii-code-51.html

So it receives a number of 51 for a "3" , a number of 46 for a "." etc.

You have to collect those characters until a control character has been received and convert it back to a float.

Or you must send the data in binary format not as characters... :slight_smile:

I recommend to buy a book on C and/or C++ and study the basics!

It's worth it!

1 Like

@yeshantcc
Upload the following sketch (your one slightly modified).

Comment: You are sending 251313.212 (assume with Newline as end character) which you must receive and save in an char-type null-ended array. After that you convert the ASCII-coded float number into "numerical float: using atof() function.

#include <SoftwareSerial.h>
SoftwareSerial softSerial(8, 9);

char myData[20];
float number;

int LED = 2;

void setup()
{
  softSerial.begin(9600);
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}
void loop()
{
  if (softSerial.available())
  {
      byte m = softSerial.readBytesUntil('\n', myData, 20);
      number[m] = '\0';  //insert null-byte
      Serial.println(number);  //shows: 251313.212

      number = atof(myData);
      Serial.println(number, 3);    //shows: 251313.212 accurately? No! Why not? 

    if (number >50.0)
    {
       digitalWrite(LED, HIGH);
    }
  }
}

-128 to +127

===> receives a number 33 (hex base) for a '3', which when displayed in decimal format appears as 51.

How? Any guidance?

1 Like

For definition of "byte" see https://en.wikipedia.org/wiki/Byte

Quote:

The modern de facto standard of eight bits, as documented in ISO/IEC 2382-1:1993, is a convenient power of two permitting the binary-encoded values 0 through 255 for one byte—2 to the power of 8 is 256.[8] The international standard IEC 80000-13 codified this common meaning.

Of course you can use 8 bit as unsigned byte with -128 .. 127 but that again is just interpretation ...

===> receives a number 33 (hex base) for a '3', which when displayed in decimal format appears as 51.

Serial does never receive a hex number, it receives binary states (HIGH or LOW) and interpretes them in a defined way. hex or binary or whatever you like is just a different kind of representation for humans.

How? Any guidance?

Yes, just read:

"I recommend to buy a book on C and/or C++ and study the basics!"

Are you annoyed because I left providing a solution to you?

It depends on the platform - some have the char type unsigned by default.

1 Like

Thank You, It did work

You mean like the following (for character A):
185-00

If so, then the receiver receives 010000101, discards the Start and Stop bits, and saves the character code 01000001 (which is 41 in hex base).