Serial has a function for that: “Serial.parseFloat()”. It skips over any characters that don’t look like a ‘float’ and then gathers the characters that DO look like a ‘float’ until it hits a non-float character or no character has arrived for the timeout period (default: one second). Then it returns the value of the float.
const byte NumberOfMotors = 6;
// At least one character is available to read
for (byte motor = 0; motor < NumberOfMotors; motor++)
if (Serial.peek() == ‘\r’)
return; // OUT OF SYNC! Re-start loop().
pos[motor] = Serial.parseFloat();
// Do stuff here to act on the new input
User_InputM1 = pos;
User_InputM2 = pos;
User_InputM3 = pos;
User_InputM4 = pos;
User_InputM5 = pos;
User_InputM6 = pos;
// Do stuff here that doesn’t need new input
I tested it and it works beautifully -- however, there is one minor issue that I cannot determine why.
I gave as input 1.1 to 6.6 to determine the parsing. It parsed them correctly and displayed them on the monitor.
However, without me doing anything, a few seconds later, it displayed 0.00 for all the inputs and then it stopped.
This is the monitor output:
Why would it do a second parse without any input and how do I stop it?
I posted my code in another post.