How to solve extra output not needed[SOLVED]

IDE 2.2.1
UNO R3

In this project I am receiving two floats to be displayed on a LCD(see code below):


byte lcdDisplayAddress = 0x27;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define LCD2004 true

LiquidCrystal_I2C LCD(lcdDisplayAddress, 20, 4);  // set the LCD address to 0x27 for a 20 chars and 4 line display


#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
//this will compile for Arduino UNO, Pro and older boards
int _sck = 13;
int _miso = 12;
int _mosi = 11;
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
//this will compile for Arduino Mega
int _sck = 52;
int _miso = 50;
int _mosi = 51;
#endif

#if defined(ARDUINO) && ARDUINO >= 100  // Arduino v1.0 and newer
#define I2C_WRITE Wire.write
#define I2C_READ Wire.read
#else  // Arduino Prior to v1.0
#define I2C_WRITE Wire.send
#define I2C_READ Wire.receive
#endif

void setup() {
  Serial.begin(9600);
  while (!Serial) {};  // Leonardo: wait for Serial Monitor
  Serial.println("\nXtrack Receiver");
#ifdef LCD2004
  Wire.begin();
  LCD.init();  // initialize the lcd
  LCD.backlight();

  LCD.setCursor(0, 0);               // Set the cursor on the third column and first row.
  LCD.print("XTRACK-Connect Test");  //
#endif
}

float AXrotor;
float EYrotor;

void loop() {
  while (Serial.available()) {  // If anything comes in Serial (USB)

    AXrotor = Serial.parseFloat();
    EYrotor = Serial.parseFloat();
    Serial.println(AXrotor);
    Serial.println(EYrotor);
#ifdef LCD2004
    LCD.setCursor(0, 1);
    LCD.print(AXrotor);
    LCD.setCursor(0, 2);
    LCD.print(EYrotor);
#endif
  }
}

If I enter, using the serialmonitor, this number: 123.45,678.90
the output looks like this:

06:19:06.695 -> Xtrack Receiver
06:19:52.039 -> 123.45
06:19:52.039 -> 678.90
06:19:54.030 -> 0.00
06:19:54.030 -> 0.00

The first three are as expected but the last two lines are not. How do I supress the last two lines??

By default serial monitor sends a CR> and a <LF>. Those result in the additional values that you see.

You can change the line-ending settings of the serial monitor to 'none'. Disadvantage is that it might slow your program down. parseInt() reads till it finds a character that doesn't belong to a float value and by not sending <CR> and <LF> it will wait for a timeout.

I suggest that you read Robin's Serial Input Basics - updated to get some ideas.

1 Like

That did it

I am using parseFloat NOT parseInt

Will have a look at that

Apologies for that; this applies to all parseXXX functions and readXXX functions.

Hello kunstmaan

Take a view for:

Serial.parseFloat(lookahead)

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