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.
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.
... 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 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.