parseInt problem with decimal

Hi guys. I have a problem with paresInt. the problem is Decimal numbers not showing correctly. For example, instead of displaying the number 20070.84, it incorrectly displays the number 8420070.

the code:

int age2;

#include<SoftwareSerial.h>

SoftwareSerial periodavg(12, 14);

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

void loop() {
  age2 = periodavg.parseInt();
  Serial.println(age2);
}

thanks for any help

The Int in parseInt refers to "integer"

1 Like

The clue is in the name of the function
It parses an integer from the input, not a float

1 Like

ok I tried with parseFloat but it showing numbers like this: 0.00 -1.00 ......

Show your new sketch. (A value of 0.00 could be the result of a timeout.)

1 Like
float age2;

#include<SoftwareSerial.h>

SoftwareSerial periodavg(12, 14);

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

void loop() {
  age2 = periodavg.parseFloat();
  Serial.println(age2);
}

The sketch looks fine. I don't see any reason why the output would not be either "0.00" for a timeout or the floating-point number you receive from 'periodavg'.

What does the data stream from 'periodavg' contain? How often are the values sent? What characters are between numbers?

1 Like

I changed the sender side from Serial.print to Serial.println and the problem is solved.

thanks all for help.

1 Like

if you were not adding a separator and kept sending floating point numbers like 20070.84 the receiving arduino would see an input as 20070.8420070.8420070.8420070.8420070.8420070.84
it's then easy to get in trouble decoding this

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.