Input Decimal points on Serial Monitor

I am using Serial.read function to put decimal points to run code.
I can enter integers 3,4,and 5 without any problem, but I want to input 3.445, 4.123. or 5.490 to run a code.

Does anyone know what kind of function I need to use?

I know I need to use float for decimal displays. But I am not trying to display decimal points; instead, I want to enter decimal points initially.

Thank you

Serial.read works for me for reading '.' characters.

1 Like

Your code needs to properly interpret the number. You should find the Serial Input Basics tutorial on this forum to be helpful.

1 Like

Serial Input Basics

2 Likes

Hi @max7637 unfortunately your explanation of what you want to do is somewhat confusing. Can you give one or two examples? Please explain what other components you are connecting to the Arduino, like perhaps a 16x2 LCD or 4 digit 7 segment display? Please post links to those.

I wonder if maybe the answer to your question could be Serial.parseFloat()?

#include <SPI.h>
#include <Controllino.h>

const int t7 = CONTROLLINO_D1; //set t7 (DISABLE IN) to D1 pin of Controllino (this can be changed if wiring on PLC is different); 5V = start spinning 0V= stop spinning
const int t8 = CONTROLLINO_D4; //set t8 (DIR IN) to D4 pin of Controllino; 5V = CW movement
const int t9 = CONTROLLINO_D2; //set t9 (STEP IN) to D2 pin of Controllino; 5V = CCW movement 

float in; //input to decide what the motor should do


 // SPEED IS SET ON THE DRIVE
 

void setup() {
  // put your setup code here, to run once:
  pinMode(t7, OUTPUT); //set pin as an output
  pinMode(t8, OUTPUT); //set pin as an output
  pinMode(t9, OUTPUT); //set pin as an output
  Serial.begin(9600); //start serial communication
  Serial.println("Input the average time: "); 
}

void loop() {
  
 // put your main code here, to run repeatedly:
  in = Serial.read(); //take input through Serial Monitor

 // if(isAlpha(in)) //check if there is an input and input is a char
 // {
  //  Serial.println(in); //feedback on what you entered
   // Serial.println("START OF CODE"); //feedback on this is start of code
  //}

  if(in == '2') //check input. And why it cannot take more than one digit?
  {
    Serial.println("Running CW for ... seconds"); //feedback; shown on Serial Monitor
    digitalWrite(t9, LOW); //Set pin to ~0V; reason for setting this is to be double sure the pin is not set to HIGH
    digitalWrite(t7, HIGH); //Set pin to ~5V
    digitalWrite(t8, HIGH); //Set pin to ~5V

    delay(5000); //run the motor for this amount of time in milliseconds

    digitalWrite(t7, LOW);
    digitalWrite(t8, LOW);
    digitalWrite(t9, LOW);
  }







  //This part is to make the motor rotate in CCW
  if(in == '3') //check if input is b
  {
    Serial.println("Running CCW for ... seconds"); //feedback; shown on Serial Monitor
    digitalWrite(t8, LOW); //Set pin to ~0V
    digitalWrite(t7, HIGH); //Set pin to ~5V
    digitalWrite(t9, HIGH); //Set pin to ~5V

    delay(5000);

    digitalWrite(t7, LOW);
    digitalWrite(t8, LOW);
    digitalWrite(t9, LOW);
    
  }
  
  

}

When Serial Monitor pops up, I am trying to type 1.22. But whatever reason, I only can put whole number.

Did you see reply #4?

When you see

if(in == '2') // I want to change this to 2.1223

when I type 2 it shows as blue, but soon as I change that to decimal points inside ' ', it does not recognize.

What do you mean?

The single quotes ' indicate a single character only, it represents a C type "char". So

in == '2.34'

is illegal, you have to use

"2.34"

but you can't compare those kind of strings with ==, you need to use strcmp().

The colour changed because the IDE is smart enough to recognize that it's not a valid char.

1 Like

Serial.parseFloat()

https://www.arduino.cc/reference/en/language/functions/communication/stream/streamparsefloat/

... but once you've parsed it to a float, you have the problem of testing for equality which is problematic for floating point variables. I say this because it seems to be your intention:
e.g.

I want to input 3.445, 4.123. or 5.490 to run a code.

and things like

  if(in == '2') //check input. And why it cannot take more than one digit?

Actually, the use case for that kind of input is sort of a mystery. Do users have a "chart" next to the keypad to remember those particular values? Or do you have some plan to process the values somehow, instead of using them for a selection process.

The kind of numeric selection in your code is normally integral, e.g. 1,2,3 etc. Heraldo Rivera does not give you a choice of whether you would like to open door number 5.322 :slight_smile: Maybe I got the wrong game show, but...

Anyway, the problem is, when a float is stored, you might see 3.445 but it's often actually a truncated 3.44499999999999999 to give a crude example, so under some circumstances you could compare that to 3.445 with == and it would fail.

On the other hand, if you compare strings "3.445" and "3.445" it can never fail.

when I type 2.1 instead of 2, it does not recognize 2.1

You mean serial input basics?

Yes.

If you had looked at that you would know not to call Serial.read() unless a character is available (by calling Serial.available() to check).

1 Like

Have you tried using the solution in reply #11 yet?

I am trying now.

1 Like

when I use this function, how can I use if-statement?
I tried...

if (input == "3.12")
.
.
.
if(input  =  " 3.12")

both lines giving me the error, so I do not know what should I put.

Thank you for your hlep

Serial.parseFloat function does work, but I was not able to figure out how to use if-statements with this function.

if (input == "3.12")
.
.
.
if(input  =  " 3.12")

this 2 lines does not work