如果輸入"X 3",輸出 3 = 3
如果輸入"X 23",輸出 23 = 20 + 3
如果輸入"X 234",輸出 233 = 199 + 30 + 4
如果輸入"X 2345",輸出 2344 = 1999 + 300 + 40 + 4
這個鬼問題不知道是不是浮點數...
/*8/22寫下的程式碼*/
String readStr = String(100);
String str1 = String(3);
String str2 = String(10);
int pos = 0;
int ind1 = 0;
int ind2 = 0;
//#include <Servo.h>
//Servo myservo; // create servo object to control a servo
void setup()
{
Serial.begin(9600);
}
void loop()
{
//expect a string like wer,qwe rty,123 456,hyre kjhg,
while( Serial.available() )
{
delay(1);
if( Serial.available() > 0 )
{
char c = Serial.read(); //gets one byte from serial buffer
if (c == ';')
{
goto parce;
}
readStr += c;
} //makes the string readString
}
parce:
if ( readStr.length() > 0 )
{
pos = readStr.length(); //capture string length
str1 = readStr.substring(0, readStr.indexOf(' ')); //first part of string
str2 = readStr.substring(readStr.indexOf(' ') + 1, pos);
Serial.println("Recieving command: " + str1 + str2);
command(str1, str2);
readStr = "";
}
}
void command(String str1, String str2)
{
String str = str1 + str2;
if( str == "G01")
{
Serial.print("hello world");
}
else if( str1 == "X")
{
int num = 0;
int n = str2.length();
for( int i = 0; i < n; i++)
{
num += (str2[i] - 48) * pow(10,n-i-1);//在百位數以上的時候,會有少1的bug
//
//Serial.print("num = ");
//Serial.println(num);
}
//Serial.print("n = ");
//Serial.println(n);
if( n > 2)//修正bug用的...
{
num += 1;
}
Serial.println(num);
//motro rotate
//
}
else
{
Serial.println("Incorrect input");
}
}