Reading a String with multpile values

Hey, so basically what i'm trying todo is fairly simple. Personally this is new territory in Arduino programming so i lack the knowledge to be able to get this running like id like.

Basically the plan is to download my code to my Arduino Pro mini (5V/16MHz), open the PuTTY application as my interface and be able to type in a 5 character code and press enter.

for Example:

-the fist character can only contain a g or an s
-the second and third character can on contain integers 0-32

right now i have the code perfectly functioning when you type in a 3 digit code of 3 values (_ _ ) i want to turn my 0-9 (on each of my second and third values) into 00-31 but cant figure out how to do that. ( _ _ _ _) (ignore the spaces, just for clarification)

ive attached my code for any suggestions.

Master.State4.ino (2.18 KB)

char inString[64];
int counter;
int rdInt;
int process_me;
int oneSet;
int twoSet;
int threeSet;
void setup()
{
  Serial.begin(115200);

  Serial.println(">Enter Command:");
  counter = 0;
  process_me = 0;
}

void reply()
{
  Serial.println();
  Serial.print(inString);
  Serial.print(" is ");
  Serial.print(counter);
  Serial.print(" characters long");
  Serial.println();
  Serial.print("... processing ...");
  process_me = 1;
}

void prompt()
{
  Serial.println();
  Serial.print("] ");
}

void getInput()
{
  rdInt = Serial.read();
  echo(char(rdInt));
  if (rdInt == '\x0D') {
    reply();
    counter = 0;
  }
  else if (rdInt == 127 || rdInt == 8)
  {
    if (counter > 0)
      counter = counter - 1;
    inString[counter] = 0;
  }
  else {
    if (counter < 64 - 2) {
      inString[counter] = (char) rdInt;
      counter = counter + 1;
      inString[counter] = '\0';
    }
  }

}

void decipher()
{
  // magic happens here

  oneSet = inString[0];
  twoSet = inString[1] - '0';
  threeSet = inString[2] - '0';

  Serial.println();

  if (oneSet == 'g' || oneSet == 'G')
  {
    Serial.write(10);
    Serial.write(13);
    Serial.print ("Get feedback of Slave ");
  }
  else if (oneSet == 's' || oneSet == 'S')
  {
    Serial.write(10);
    Serial.write(13);
    Serial.print ("Set Slave ");
  }
  else
  {
    Serial.write(10);
    Serial.write(10);
    Serial.write(13);
    Serial.println ("***  Invalid character you idiot, expected g or s as first value  ***");
  }

  if (twoSet >= 0 && twoSet <= 9)
  {
    Serial.print (twoSet);
    Serial.print ("'s ");
  }
  else
  {
    Serial.write(10);
    Serial.write(10);
    Serial.write(13);
    Serial.println ("***  Invalid numeral you idiot, expected integer 0-9 as second value!  ***");
  }
  if (threeSet >= 0 && threeSet <= 9)
  {
    Serial.print (threeSet); 
  }
  else 
  {
    Serial.write(10);
    Serial.write(10);
    Serial.write(13);
    Serial.println ("***  Invalid numeral you idiot, expected integer 0-9 as third value!  ***");
  }

  process_me = 0;
}


void echo(char inChar)
{

  Serial.write(inChar);     //
}

void loop()
{
  if (Serial.available() > 0) { //
    getInput();
  }
  if (process_me > 0)
  {
    decipher();
    prompt();
  }
}

I think the first thing you need is a more robust way to receive the data. Have a look at the second example in Serial Input Basics

That will put the whole message into the array recvdChars[]. Then I think all you need to do is extend your decipher() function to work with more elements.

If you need to be able to work with different length messages then you will need to check the length of the string with strlen() before trying to parse it.

...R

JJP4:
-the fist character can only contain a g or an s
-the second and third character can on contain integers 0-32

I'd suggest like this, but the sender needs to send a new line ('\n') at the end of the message (configurable):

#define MAX_MESSAGE_LENGTH 8

void setup(void) 
{
  Serial.begin(9600);
}

void loop(void) 
{
  char* newMessage = checkIncomingMessage(Serial, '\n');  // here we to use new line terminating char
  if(newMessage)
  {
    Serial.print("Just got new message:\t");
    Serial.println(newMessage);
    char command = tolower(newMessage[0]);
    long int value = strtol(newMessage + 1, NULL, 10);
    switch (command)
    {
      case 'g':
        Serial.print("G:");
        Serial.println(value);
        break;
      case 's':
        Serial.print("S:");
        Serial.println(value);
        break;
      default:
        Serial.println("bad message");
        break;
    }
  }
}

char* checkIncomingMessage(HardwareSerial& stream, const char endMarker)
{
  static char incomingPacket[MAX_MESSAGE_LENGTH];
  static int index = 0;
  if(stream.available())
  {
    char inChar = stream.read();
    if(inChar == endMarker)
    {
      incomingPacket[index] = NULL;
      index = 0;
      return incomingPacket;
    }
    else
    {
      incomingPacket[index++] = inChar;
    }
  }
  return nullptr;
}

needs more error checking...