Hi Please let me know why this function does not work correctly :(

I used parseInt() function to get integer value from Serial port buffer.

But this function can get only positive integer value not negative value. So I made new function parseInt2() and those code is below.

int parseInt2(){

int i=0;
int data=NULL;
int predata =NULL;
int minusSwitch = 0;
int memory[10]={};
int order=0;
int array_length=0;
while(Serial.available()){
predata = Serial.read();
if(predata == 45){
minusSwitch=1;}

if(predata>=48&&predata<=57){
memory*=predata-48;*

  • i++;*

  • array_length++;*

  • }*
    else break;
    }
    for(int j=0;j<array_length;j++){
    data = (data*10)+memory[j];}
    if(minusSwitch==1){
    data=data*(-1);
    }
    minusSwitch=0;
    return data;
    }
    and I used this parseInt2() with arduino Mega 2560 like below .
    void setup(){

  • Serial.begin(9600);*
    }
    void loop(){

  • int number=NULL;*

  • if(Serial.available()){*

  • number = parseInt2();*

  • Serial.println(number);*

  • Serial.println(-1)*

  • delay(1000);*

  • }*

  • else{*

  • Serial.println("There's no data to print");*

  • delay(1000);}*
    *} *
    when I write positive integer value on Serial monitor, it works correctly.
    But when I write negative integer value, Serial monitor shows '0' and 'Absolute integer value' like
    ------------------------------------------------
    There's no data to print
    There's no data to print <---------------------I write -10 on Serial monitor and send
    0
    10
    There's no data to print
    .
    .
    .
    .
    .
    Please let me know why like this. Is there any function in Arduino library to get both positive and negative integer value from Serial buffer????
    Thanks :slight_smile:

But this function can get only positive integer value not negative value.

BZZZZZT!

Is there any function in Arduino library to get both positive and negative integer value from Serial buffer????

Have a close look at parseInt.
Here's its source

long Stream::parseInt(char skipChar)
{
  boolean isNegative = false;
  long value = 0;
  int c;

  c = peekNextDigit();
  // ignore non numeric leading characters
  if(c < 0)
    return 0; // zero returned if timeout

  do{
    if(c == skipChar)
      ; // ignore this charactor
    else if(c == '-')
      isNegative = true;
    else if(c >= '0' && c <= '9')        // is c a digit?
      value = value * 10 + c - '0';
    read();  // consume the character we got with peek
    c = timedPeek();
  }
  while( (c >= '0' && c <= '9') || c == skipChar );

  if(isNegative)
    value = -value;
  return value;
}

please use code tags when posting code.

if(predata == 45){
   minusSwitch=1;}

This mess would look so much more readable as:

if(predata == '-')
{
   minusSwitch=1;
}

This mess:

if(predata>=48&&predata<=57){
    memory[i]=predata-48;

should be:

if(predata >= '0' && predata <= '9')
{
    memory[i] = predata - '0';

But, the real question is why are you trying to re-implement the parseInt() function in the Stream class that HardwareSerial derives from? It already knows how to deal with negative numbers.

Thanks a lot :slight_smile: Can you let me know how to get negative numbers from Serial.buffer?? Actually, I am trying to send String of numbers (such as 100:200:300:400:......) to Serial port and Arduino read this numbers.

PaulS:

if(predata == 45){

minusSwitch=1;}



This mess would look so much more readable as:



if(predata == '-')
{
  minusSwitch=1;
}




This mess:


if(predata>=48&&predata<=57){
    memory[i]=predata-48;



should be:


if(predata >= '0' && predata <= '9')
{
    memory[i] = predata - '0';




But, the real question is why are you trying to re-implement the parseInt() function in the Stream class that HardwareSerial derives from? It already knows how to deal with negative numbers.

Can you let me know how to get negative numbers from Serial.buffer?

   int n = Serial.parseInt();

Haha >_< thank u so much!! I didn't recognized that parseInt() also read negative integers!

PaulS:

   int n = Serial.parseInt();