Skip to main content
added 406 characters in body
Source Link
gre_gor
  • 1.7k
  • 4
  • 18
  • 30

Your slave is sending an array of characters and your master is reading the ASCII values of those characters.

If you send 25.43, Serial.println will send 7 characters 2, 5, ., 4, 3, carriage return and a newline.

The master will read them as 50, 53, 46, 52, 51, 13 and 10. Your code then prints:

50.00
53.00
46.00
52.00
51.00
13.00
10.00

On your master, youthe loop() should replacelook like:

tempCvoid =loop() {
  if (Serial.readavailable();) {

with

tempC    char c = Serial.parseFloatpeek(); // just look at the next character on the serial buffer
    if (c == '\r' || c == '\n') // ignore the carriage return and newline characters
      Serial.read(); // remove carriage returnthe character from the serial buffer by reading it
    else {
      tempC = Serial.readparseFloat(); // removeread newlineand characterparse fromthe string representing the serialfloat buffervalue
      Serial.println(tempC);
    }
  }
}

Your slave is sending an array of characters and your master is reading the ASCII values of those characters.

If you send 25.43, Serial.println will send 7 characters 2, 5, ., 4, 3, carriage return and a newline.

The master will read them as 50, 53, 46, 52, 51, 13 and 10.

On your master, you should replace

tempC = Serial.read();

with

tempC = Serial.parseFloat();
Serial.read(); // remove carriage return character from the serial buffer
Serial.read(); // remove newline character from the serial buffer

Your slave is sending an array of characters and your master is reading the ASCII values of those characters.

If you send 25.43, Serial.println will send 7 characters 2, 5, ., 4, 3, carriage return and a newline.

The master will read them as 50, 53, 46, 52, 51, 13 and 10. Your code then prints:

50.00
53.00
46.00
52.00
51.00
13.00
10.00

On your master, the loop() should look like:

void loop() {
  if (Serial.available()) {
    char c = Serial.peek(); // just look at the next character on the serial buffer
    if (c == '\r' || c == '\n') // ignore the carriage return and newline characters
      Serial.read(); // remove the character from serial buffer by reading it
    else {
      tempC = Serial.parseFloat(); // read and parse the string representing the float value
      Serial.println(tempC);
    }
  }
}
added 189 characters in body
Source Link
gre_gor
  • 1.7k
  • 4
  • 18
  • 30

Your slave is sending an array of characters and your master is reading the ASCII values of those characters.

If you send 25.43, Serial.println will send four7 characters 2, 5, ., 4, 3, carriage return and a newline.

The master will read them as 50, 53, 46, 52, 51, 13 and 10.

On your master, you should replace tempC = Serial.read();

tempC = Serial.read();

with tempC = Serial.parseInt();.

tempC = Serial.parseFloat();
Serial.read(); // remove carriage return character from the serial buffer
Serial.read(); // remove newline character from the serial buffer

Your slave is sending an array of characters and your master is reading the ASCII values of those characters.

If you send 25, Serial.println will send four characters 2, 5, carriage return and a newline.

The master will read them as 50, 53, 13 and 10.

On your master, you should replace tempC = Serial.read(); with tempC = Serial.parseInt();.

Your slave is sending an array of characters and your master is reading the ASCII values of those characters.

If you send 25.43, Serial.println will send 7 characters 2, 5, ., 4, 3, carriage return and a newline.

The master will read them as 50, 53, 46, 52, 51, 13 and 10.

On your master, you should replace

tempC = Serial.read();

with

tempC = Serial.parseFloat();
Serial.read(); // remove carriage return character from the serial buffer
Serial.read(); // remove newline character from the serial buffer
Source Link
gre_gor
  • 1.7k
  • 4
  • 18
  • 30

Your slave is sending an array of characters and your master is reading the ASCII values of those characters.

If you send 25, Serial.println will send four characters 2, 5, carriage return and a newline.

The master will read them as 50, 53, 13 and 10.

On your master, you should replace tempC = Serial.read(); with tempC = Serial.parseInt();.