Thank Nick Gammon, PaulS and Robin2
the link that Nick gave is very helpful, I will use this the method, but for a moment I am using this code an working but not to well.
the different just moving the
delay(5) to the place before while (Serial.available()) {
Here is the code:
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
int result;
void setup() {
Serial.begin(9600);
myservo1.attach(8); //the pin for the servo control
myservo2.attach(7);
Serial.println("test"); // so I can keep track of what is loaded
}
void abc () {
while (Serial.available()) {
delay(5); //delay to allow buffer to fill
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
servo1 = readString.substring(0, 3); //get the first four characters
servo2 = readString.substring(3, 6); //get the next four characters
int n1 = servo1.toInt();
int n2 = servo2.toInt();
Serial.println("the numbers are :");
Serial.println(n1); //print to serial monitor to see number results
Serial.println(n2);
myservo1.write(n1);
myservo2.write(n2);
readString="";
servo1="";
servo2="";
}
}
void loop() {
if(Serial.available()){
result = Serial.read();
if (result== 'g'){
Serial.println("xxx");
delay(30);
abc();
}
}
}
but I will try the State Machine methods anyway.