An issue with the int() function

Hey all,

I am having another issue with some code i am throwing together for an autopilot

char dataset[27]="";
char incoming=-1;
int counta=0;
int count=0;
String StringData="";
int comma1;
int comma2;
int datastart;
int dataend;
String rollstring;
String pitchstring;
String yawstring;
int rollval;
int pitchval;
int yawval;
char dan1='D';
char checker=-1;
int searchcount=0;

char rollstringchar[8]="";
char pitchstringchar[8]="";
char yawstringchar[8]="";
void setup()
{
  Serial.begin(57600);
  Serial1.begin(57600);
}



void loop()
{
  count=0;
 
     StringData="";
    while (incoming!='X')
    {
      incoming=-1;
      if (Serial1.available())
      {
        incoming=Serial1.read();     // Read a byte of the serial port
        dataset[count]=incoming;
        count++;
      }
      }
    StringData=dataset;
 
   for(int i=0;i<=30;i++)
   {
     dataset[i]=0;
   }
   count=0;
   
   
 
datastart=StringData.indexOf(':');
dataend=StringData.indexOf('X');
comma1=StringData.indexOf(',',datastart+1);
comma2=StringData.indexOf(',',comma1+1);
rollstring=StringData.substring(datastart,comma1);
pitchstring=StringData.substring(comma1+1,comma2);
yawstring=StringData.substring(comma2+1,dataend);


rollstring.toCharArray(rollstringchar,7);
rollval=int(rollstringchar);

yawstring.toCharArray(yawstringchar,7);
yawval=int(yawstringchar);

pitchstring.toCharArray(pitchstringchar,7);
pitchval=int(pitchstringchar);


Serial.print("Yaw: ");
Serial.println(yawval);
Serial.print("Roll: ");
Serial.println(rollval);
Serial.print("Pitch: ");
Serial.println(pitchval);

  for(int i=0;i<=7;i++)
   {
     rollstringchar[i]=0;
       yawstringchar[i]=0;
         pitchstringchar[i]=0;
   }
   count=0;
  
}

right now the issue is with the int() conversion from a character array it gives me garbage numbers in like the 577 -585 range as an output I am not really sure what is going on here. I considered using the atoi function but it just outputs zero’s.

any ideas?

When you post a problem like this, it is best to reduce it to the minimum amount of code that shows the problem.

righto

rollstring.toCharArray(rollstringchar,7);
rollval=int(rollstringchar);

yawstring.toCharArray(yawstringchar,7);
yawval=int(yawstringchar);

pitchstring.toCharArray(pitchstringchar,7);
pitchval=int(pitchstringchar);


Serial.print("Yaw: ");
Serial.println(yawval);
Serial.print("Roll: ");
Serial.println(rollval);
Serial.print("Pitch: ");
Serial.println(pitchval);

  for(int i=0;i<=7;i++)
   {
     rollstringchar[i]=0;
       yawstringchar[i]=0;
         pitchstringchar[i]=0;
   }
   count=0;
  
}

what this code does is take a String object, break it back into a char array so that the int() function or atoi would work on it because it seems that neither of those converters work on a String object. the output that this provides is
Yaw: 585
Roll: 569
Pitch: 577

repeating when it should be something like yaw:95 roll:50 pitch:50

When I said "the minimum amount of code", I meant "the minimum compilable sketch"

rollval=int(rollstringchar);

Am I reading this correctly - you're trying to convert a pointer to a String object called "rollstringchar" to an integer called "rollval".
Is that correct?

Be interesting to print out what's in rollstringchar and its partners.

Here is what int() actually does:

Well, the explanation is not very clear especially when it says a value of any type. I think it meant a value of any primitive type, which excludes strings. So to convert a series of ASCII numbers into an integer, this int() is not the right function. I do sscanf but some will tell you a more String-relevant function to extract number without converting to char array.

thank you - and unfortunately that's what I was banking on, that "any type" statement.

Maybe solution presented here will help:

I really don't play with Strings on a microcontroller.