void setup() {
Serial.begin(9600);
// 입력 문자열
String input = "P,180,2,2,2,100,E";
// ','로 문자열 분리
String tokens[10];
int tokenIndex = 0;
while (input.length() > 0) {
int index = input.indexOf(',');
if (index == -1) { // 더 이상 ','가 없는 경우
tokens[tokenIndex++] = input;
break;
} else {
tokens[tokenIndex++] = input.substring(0, index);
input = input.substring(index + 1);
}
}
// 결과 출력
int motorSpeed=0;;// 80~150에서 동작한다.
int servo1Stat=2;//1:상, 2:중 3:하
int servo2Stat=2;//1:상, 2:중 3:하
int servo3Stat=2;//1:상, 2:중 3:하
int camServoStat=0; //0~180도 가능
motorSpeed=tokens[1].toInt();
servo1Stat=tokens[2].toInt();//1:상, 2:중 3:하
servo2Stat=tokens[3].toInt();;//1:상, 2:중 3:하
servo3Stat=tokens[4].toInt();;//1:상, 2:중 3:하
camServoStat=tokens[5].toInt();;
Serial.println("Parsed tokens:");
for (int i = 0; i < tokenIndex; i++) {
Serial.print("Token ");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(tokens[i]);
}
Serial.println(motorSpeed);
Serial.println(servo1Stat);
Serial.println(servo2Stat);
Serial.println(servo3Stat);
Serial.println(camServoStat);
}
void loop() {
// 빈 루프
}