Difficulty manipulating char/int.

If i write 2100a the program is supposed to move servo 2 to 100 degrees, so the variable Serveposint" is supposed to be 2, and the variable “pos” is supposed to be 100.

Why is the servo number or servo pin number stored in a variable names servoPOSint? The value is NOT the position of the servo.

Using names that make sense will make your code far easier to write.

Sending data in a parse-able format would make more sense. Sending “<2, 100>” makes it very easy to determine where a packets starts (’<’), where it ends (’>’), and where the data in the packet is delimited. strtok() and atoi() can then handle the parsing and conversion.

michinyon: What do you expect this line to do ?

When i read it out of the array, the digit is a character(?), I expect the line to make it to an integer, so i can use it to use the right servo-output.

PaulS:
Why is the servo number or servo pin number stored in a variable names servoPOSint? The value is NOT the position of the servo.

Using names that make sense will make your code far easier to write.

Sending data in a parse-able format would make more sense. Sending “<2, 100>” makes it very easy to determine where a packets starts (’<’), where it ends (’>’), and where the data in the packet is delimited. strtok() and atoi() can then handle the parsing and conversion.

The variable has both the position and the servo number, therefore servo(for servo number)pos(for position)int(for integer)

I am sure its better ways to do it, how would the code be with the <2,100> format? I am not familiar with the strtok() command.

The variable has both the position and the servo number, therefore servo(for servo number)pos(for position)int(for integer)

No, it does not. Quit persisting in this delusion.

I am sure its better ways to do it,

As several of us have said.

how would the code be with the <2,100> format?

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where it says “Process the packet” is where you’d put code to parse the data (“2,100”) in inData, using strtok() and atoi() and to use the data.

I am not familiar with the strtok() command.

Google is.

PaulS:
No, it does not. Quit persisting in this delusion.
As several of us have said.

#define SOP '<'

#define EOP ‘>’

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
  Serial.begin(57600);
  // Other stuff…
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
      index = 0;
      inData[index] = ‘\0’;
      started = true;
      ended = false;
    }
    else if(inChar == EOP)
    {
      ended = true;
      break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = ‘\0’;
      }
    }
  }

// We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

// Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = ‘\0’;
  }
}




Where it says "Process the packet" is where you'd put code to parse the data ("2,100") in inData, using strtok() and atoi() and to use the data.
Google is.

Yeah, including me.

The processing of the input data is where I am struggling. I appreciate your help, but I am probably not going to rewrite the whole processing part from a scratch just for it to be “nicer code”, the only thing in the code that is causing trouble at the moment seem to be the conversion of the char to an integer. Rewriting the processing of the packet is probably easy for someone who is skilled in programming. I am not. So for the moment it seems like a lot of work for little gain for me.

the only thing in the code that is causing trouble at the moment seem to be the conversion of the char to an integer.

Converting ONE character to an integer is trivial.

char someChar = '4';
int theVal = someChar - '0';

// theVal will be 4

Great, that solved the whole problem, now the program works. Thanks :) Why do you have to deduct zero instead of just writing "int theVal = someChar"?

If you insist on doing it your way have a look at this short program and pick out any ideas that might be useful.

char input[] = {'2', '1', '0', '0', 'a'};

void setup()
{
  Serial.begin(115200);
  char servoPosAsChar = input[0];
  int servoPosAsInt = input[0] - '0';
  Serial.println(servoPosAsChar);
  Serial.println(servoPosAsChar * 10);
  Serial.println(servoPosAsInt);
  Serial.println(servoPosAsInt * 10);
}

void loop()
{
}

ATWindsor: Great, that solved the whole problem, now the program works. Thanks :) Why do you have to deduct zero instead of just writing "int theVal = someChar"?

You're not deducting zero, you're deducting the value of the character zero. Not the same thing at all.

You're not deducting zero, you're deducting the value of the character zero.

Precisely. Look at an ASCII table to understand why.