|
我練習了一下識別特定的格式的字串,再把識別正確的字串轉變數,可是不知到為什麼回傳的變數(val)都是回傳0,程式碼再下面,請告訴我哪裡出問題了(拜託),想了好久都想不到。
int valL;
int valR;
String string;
char Cend = ',';
char Cbegin = 'A';
char currentC;
void setup(){
Serial.begin(9600);
}
void loop(){
while(Serial.available()){
currentC = Serial.read();
if(currentC == Cbegin){
string = "";
while(currentC != Cend){
currentC = Serial.read();
if(currentC != Cend){
string += currentC;
delay(2);
}
}
if(currentC == Cend){
Serial.println(string);
handle(string);
string = "";
}
}
}
}
void Left(String string){
if(string.substring(0,1) == "L"){
char temp[20];
string.substring(1).toCharArray(temp,19);
int val = atoi(temp);
valL = val;
}
}
void Right(String string){
if(string.substring(0,1) == "R"){
char temp[20];
string.substring(1).toCharArray(temp,19);
int val = atoi(temp);
valR = val;
}
}
void handle(String string){
Left(string);
Right(string);
Serial.print("Left: ");
Serial.print("");
Serial.print(valL);
Serial.print(" ");
Serial.print("Right: ");
Serial.print("");
Serial.print(valR);
Serial.println();
}
順便把我執行的結果給大家看
�123
Left: 0 Right: 0
�23213
Left: 0 Right: 0
�213
Left: 0 Right: 0
傳多少都是零,拜託幫忙除錯,兼教學。 |
|