[SOLVED] help with atoi/atol

Hi, I'm doing a small project and can not find the solution to the following problem: when I read the serial numbers 1,0,2,4 on the go to convert 1024 turns to 24, but when the numbers are 1,1,2,4, then it works correctly and converts to 1124.
Someone we could help me please?

#include <LiquidCrystal.h>
#include <stdio.h>    
#include <stdlib.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

char buffer2[5];

char a = ' ';
int state = 0;
int contador = 0;
int contador_errors = 0;
char buffer[500] = {' '};
int h = 0;
void setup()
{
  Serial.begin(19200);
  Serial3.begin(19200);
  lcd.begin(16, 2);
}

void loop()
{
  
  while (state == 0)
  {
    if(contador_errors > 5)
    {
      Serial.print("no signal");
      Serial.print("State = 1");
      contador = 0;
      delay(1000);
    }
    if((Serial3.available() > 0))
    {

      a = Serial3.read();
      buffer[contador] = a;
      Serial.print(buffer[contador]);
      ///Serial.print(a);

      contador++;
      contador_errors = 0;
      if(a == 's')
      {
        state = 1;
      }
     if(a == 'f')
      {
        contador = 0;
        h++;
        if(h >= 40)
        {
          state = 1;
        }
      }
    }
  }
  
  
  while (state == 1)
  {
    long int  n;
    n = atol(buffer2);
    Serial.println(n);
    delay(3000);
    contador = 0;
    h = 0;
     state = 0;
  }
}

I may have missed something, but you're putting the serial data in one buffer ("buffer"), not terminating it, but anyway using a different buffer ("buffer2") to do the conversion.

char buffer[500] = {' '};

500? Why?

    n = atol(buffer2);

You've never written anything to buffer2.

I don't understand this:

  while (state == 1)
  {
    long int  n;
    n = atol(buffer2);
    Serial.println(n);
    delay(3000);
    contador = 0;
    h = 0;
     state = 0;
  }

So if state equals 1, we drop into the while loop, convert buffer2 to a long and display it, wait three seconds, and then set state equal to 0. Why are you using a while loop when an if statement would work?

Make sure that you are sending the character for the number zero , and not the letter between 'N' and 'P'

This program is used to read the next character, then stored in a String, and finally converted to int using atoi / atoll, but do not boil convert all characters and keep only four picks in another Strring

This is the data to convert:

f0,0,0,0,0,0,0,0,0,0,0,0,0513,0525,0494,0512,0497,0495,0499,0500,0516,0504,0508,0499,0521,0529,0,0,0,0,0,0,0

I only convert the red numbers.

the problem is when it becomes them to me and I only have a zero interleaving two figures shows that the four would have become
1024 --> 24
1124 --> 1124

This program is used to read the next character, then stored in a String,

The one you posted does no such thing.

Please re-read replies 1 and 2.

this is the code, missing some things

#include <LiquidCrystal.h>
#include <stdio.h>    
#include <stdlib.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

char buffer2[5];

char a = ' ';
int state = 0;
int contador = 0;
int contador_errors = 0;
char buffer[120] = {' '};
int h = 0;
void setup()
{
  Serial.begin(19200);
  Serial3.begin(19200);
  lcd.begin(16, 2);
}

void loop()
{
  
  while (state == 0)
  {
    if(contador_errors > 5)
    {
      Serial.print("no signal");
      Serial.print("State = 1");
      contador = 0;
      delay(1000);
    }
    if((Serial3.available() > 0))
    {

      a = Serial3.read();
      buffer[contador] = a;
      Serial.print(buffer[contador]);
      ///Serial.print(a);

      contador++;
      contador_errors = 0;
      if(a == 's')
      {
        state = 1;
      }
     if(a == 'f')
      {
        contador = 0;
        h++;
        if(h >= 40)
        {
          state = 1;
        }
      }
    }
  }
  
  
  while (state == 1)
  {
 buffer2[0] =  '0';
    buffer2[1] =  buffer[25];
    buffer2[2] =  buffer[26];
    buffer2[3] =  buffer[27];
    buffer2[4] =  buffer[28];
    long int  n;
    n = atol(buffer2);
    Serial.println(n);
    delay(3000);
    contador = 0;
    h = 0;
     state = 0;
  }
}

the problem is when it becomes them to me and I only have a zero interleaving two figures shows that the four would have become
1024 --> 24
1124 --> 11

This seems extremely implausible.

Your program seems to be extremely overcomplicated.

this is the code, missing some things

{ taps fingers impatiently }

then stored in a String

I see no Strings.

This is how I would implement your program

#define BSIZE (20)
char buf[BSIZE] ;
int p=0 l;

void setup()
{
}

void loop()
{
    if ( Serial.available >= 1 )
    {
        char ch = Serial.read() ;
        if ( ch >= '0' && ch <= '9' && p<BSIZE )
        {
            buf[p]=ch ;
            p++ ;
        }
        else if ( ch == ',' || ch == '\n' )
        {
             buf[p]='\0' ;
             int n = atoi(buf);
             Serial.print("Got ");
             Serial.println(n);
             p=0 ;
        }
        else
        {
              Serial.print(" Problem:  Got ch = ");
              Serial.print( ch );
              Serial.print("    p is ");
              Serial.println(p) ;
              p=0 ;   //   start again 
        }
    }
}

Thank you very much!!
The program are very good!!