Issue when trying to store a value after Serial.parseFloat()

Hi there everyone!

I have been using this sketch that works perfectly.

So as you can see, you send a value through the serial monitor, this value is stored and then send to a ESC (electronic speed controller)

This is the code:


#include <Servo.h>

Servo ESC; // create servo object to control the ESC

int inputVal = 90; // variable to store the input value

void setup() {

Serial.begin(9600);

// Attach the ESC on pin 9

ESC.attach(9, 1000, 2000); // (pin, min pulse width, max pulse width in microseconds)

}

void loop() {

if (Serial.available()>1) { // check if there is any input available from Serial Plot

inputVal = Serial.parseInt(); // read the input value and store it in the variable

Serial.print("Input value is: "); // print a message to confirm the input value

Serial.println(inputVal);

ESC.write(inputVal); // Send the signal to the ESC (value between 0 and 180)

}

else Serial.println("nothing to read");

delay(100); // wait for a short period to avoid excessive readings

}

I just change my needs for this script and run into issues.

As you can see in the following code, its more or less the same but introducing a float instead of an integer:


#include <VescUart.h>

/** Initiate VescUart class */

VescUart UART;

float inputVal = 0.0;

void setup() {

Serial.begin(9600);

/** Setup UART port (Serial1 on Atmega32u4) */

Serial1.begin(115200);

while (!Serial1) {;}

/** Define which ports to use as UART */

UART.setSerialPort(&Serial1);

}

void loop() {

if (Serial.available()>1.0)

{

inputVal = Serial.parseFloat();

Serial.print("Input value is: ");

Serial.println(inputVal);

UART.setRPM(inputVal);

}

else Serial.println("nothing to read");

delay(100);

}

What happens is that I introduce -let's say- 200.0- and then it works, it tells me "Input value is: 200.0" and actually i can see that the motor is moving, but just for a loop.

After that, the serial monitor puts again "nothing to read" and the motor stops (since i guess, the value returns to being 0)

So I guess what I need is the last value to remain until I put the next value

I hope someone can help me :slight_smile:

Thanks in advance

it could be that the end of lines characters() are being read as 0
after parsing the float try reading and throwing away any characters in the input buffer

inputVal = Serial.parseFloat();
while(Serial.available())Serial.read();

Did you expect some fraction of a character to be available?

Maybe not the best code, but that was working for my last script

instead of being offended, it is better to use the comments of others for learning.
See what is the meaning of the operation :

and what needs to be written here instead of 1.0

I have try

if (Serial.available()>1)

but doesn't work as well.
However, I just try the following and it works:

#include <VescUart.h>

/** Initiate VescUart class */

VescUart UART;

float inputVal = 0.0;

void setup() {

Serial.begin(9600);

/** Setup UART port (Serial1 on Atmega32u4) */

Serial1.begin(115200);

while (!Serial1) {;}

/** Define which ports to use as UART */

UART.setSerialPort(&Serial1);

}

void loop() {

if (Serial.available()>1.0)

{

inputVal = Serial.parseFloat();

Serial.print("Input value is: ");

Serial.println(inputVal);



}

else Serial.println("nothing to read");

delay(100);
UART.setRPM(inputVal);
}

Why would you do this if you did not read a new value?

Side note: I would suggest to study Serial Input Basics to understand how to handle the Serial communication

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