how i can read negative number with command Serial.read()? beacouse i with this
char tmpChar = Serial.read(); //read incoming character from serial port
if (isDigit(tmpChar)) { //check if the in character it's a number
setpoint = (setpoint * 10 ) + tmpChar - '0'; // convert from ascii code to it's corresponding number; '0' == DEC 48
}
if (tmpChar == '\n' || tmpChar == '\r') { //if CR or LF was received
if (setpoint > 0) {
tmpChar = "";
Serial.print(setpoint);
Serial.println(" NUMERO STEP IN AVANTI");
}
im using an encoder motor, i want to move for N steps the motor forward (positive number) or backward (negative number) , i insert the number of step in serial input
i can read only positive number. Thanks for help
how suggest Majenko i made a partial solution :
char buf[8];
int valore = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
if (Serial.available() >= 8) {
for (int i = 0; i < 8; i++) {
buf[i] = Serial.read();
}
String str();
if (buf[0] == "-") {
valore = (atoi(buf)) * -1;
Serial.println(valore);
}
else {
valore = atoi(buf);
Serial.println(valore);
}
}
}
but the problem now it's the static array , unpleasant to use
3 Answers 3
Personally I would read the data into a char array then use either atoi()
or strtol()
to convert that char array into a number.
Using those functions is more robust that parsing the data yourself since they identify where the number ends and discards any trailing garbage. It also skips any white space before the number. For instance it will interpret all these as the number -32
: " -32"
, "-32 Degrees"
, " -32+4 "
and "-32-2"
.
strtol()
has the additional advantage that it can work in other bases than 10, so you could interpret hexadecimal data, binary data, sexagesimal data, etc.
atoi()
and strtol()
are standard C functions.
E.g.:
int val = atoi(mycharbuffer);
or
long val = strtol(mycharbuffer, NULL, 10);
-
can make an example pleaseFrancesco Valla– Francesco Valla2017年06月08日 10:57:20 +00:00Commented Jun 8, 2017 at 10:57
-
Of what - the reading into the char buffer, or the converting to integer?Majenko– Majenko2017年06月08日 10:59:37 +00:00Commented Jun 8, 2017 at 10:59
-
to convert readed char in a negative int , thanks for helpFrancesco Valla– Francesco Valla2017年06月08日 11:00:27 +00:00Commented Jun 8, 2017 at 11:00
-
-
im tryng with this without succes, where im wrong?
void loop() { char tmpChar = Serial.read(); long val = strtol(tmpChar, NULL, 10); Serial.println(val); }
Francesco Valla– Francesco Valla2017年06月08日 11:09:30 +00:00Commented Jun 8, 2017 at 11:09
You could do something like this:
static int setpoint; // the partially read setpoint
static bool isNegative; // whether it should be negative
void applySetpoint(int); // apply the setpoint when fully read
char tmpChar = Serial.read();
if (isDigit(tmpChar)) {
setpoint = (setpoint * 10 ) + tmpChar - '0';
} else if (tmpChar == '-') { // minus sign
isNegative = true;
} else if (tmpChar == '\r') { // line terminator
if (isNegative) {
setpoint = -setpoint;
isNegative = false; // reset for next read
}
applySetpoint(setpoint);
}
Note that this is not very robust: it will happily interpret "3-2" as meaning "-32". Note also that I assumed your terminator to be '\r', you may be using another one.
The parseInt
can read negative numbers (I think it does, the reference is very cryptic). The parseInt
has a timeout
so it does not wait forever.
Can you tell what kind of data you are reading ? Is it part of more data ?
-
im using an encoder motor, i want to move for N steps the motor forward (positive number) or backward (negative number) , i want to insert the number of step in serial inputFrancesco Valla– Francesco Valla2017年06月08日 10:52:44 +00:00Commented Jun 8, 2017 at 10:52
-
Just a number then ? When parseInt has a timeout, it returns 0, that means you can't send a 0. If that is okay, then check for Serial.available and do a Serial.parseInt.Jot– Jot2017年06月08日 12:53:42 +00:00Commented Jun 8, 2017 at 12:53