how to extract two int from string

hi,

i have to send command via serial port to modify speed of two dc motor wired on a motor shied of my arduino.
so, i attempt to use a string, but i fail to extract two values from this. because i receive command in ascii code.
i use this kind of code :

#include <AFMotor.h>

AF_DCMotor motorG(3);
AF_DCMotor motorD(4);

void setup() {

  Serial.begin(9600);
  //digitalWrite(13,HIGH);
//  analogReference(DEFAULT);
  
  // turn on motor
  motorG.setSpeed(1);
 
  motorG.run(RELEASE);
  // turn on motor
  motorD.setSpeed(1);
 
  motorD.run(RELEASE);
}
void loop() {
  	if (Serial.available() > 0) { // si données disponibles sur le port série
// Serial.println("OK");
  String serialReading;
  
  serialReading = Serial.read();    // ex: G11D454E
  // Serial.print(serialReading);
  
    String SpeedG = serialReading.substring(0,1);
    String SpeedD = serialReading.substring(1,2);
    int SG =0;
    int SD =0;
    Serial.print(SpeedG);
    Serial.print(SpeedD);
if(SpeedG==48) SG=0 ;
if(SpeedG==49) SG=1 ;
if(SpeedG==50) SG=2 ;
if(SpeedG==51) SG=3 ;
if(SpeedG==52) SG=4 ;
if(SpeedG==53) SG=5 ;
if(SpeedG==54) SG=6 ;
if(SpeedG==55) SG=7 ;
if(SpeedG==56) SG=8 ;
if(SpeedG==57) SG=9 ;

if(SpeedD==48) SD=0 ;
if(SpeedD==49) SD=1 ;
if(SpeedD==50) SD=2 ;
if(SpeedD==51) SD=3 ;
if(SpeedD==52) SD=4 ;
if(SpeedD==53) SD=5 ;
if(SpeedD==54) SD=6 ;
if(SpeedD==55) SD=7 ;
if(SpeedD==56) SD=8 ;
if(SpeedD==57) SD=9 ;
//  motorG.setSpeed(SG);  
//  motorD.setSpeed(SD);

this code don't work :frowning: .. i can send what i want via the serial port, and i need to receive a value "SG" into 0 and 250.
Should you have a best way, a working way to do it ?
thanks by advance.

serialReading = Serial.read();    // ex: G11D454E
    String SpeedG = serialReading.substring(0,1);
    String SpeedD = serialReading.substring(1,2);

Serial.read() will only the first byte that is available.

Belowissome simple code I use with servos that converts a string from the serial monitor into numbers for servo control.

// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see results
      Serial.println(servo2);
      
      int n1; //declare as number  
      int n2;
      
      char carray1[6]; //magic needed to convert string to a number 
      servo1.toCharArray(carray1, sizeof(carray1));
      n1 = atoi(carray1); 
      
      char carray2[6];
      servo2.toCharArray(carray2, sizeof(carray2));
      n2 = atoi(carray2); 
      
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}

thanks for the code zoomkat, it's perfect !!

@james : i put this code to avoid prob. :

            int Separator = serialReading.indexOf('D');
if (Separator > 0){
     SpeedG1 = serialReading.substring(0,Separator);
     SpeedD1 = serialReading.substring(Separator+1);