我对我的项目有疑问。我仍然是这个领域的初学者。我想制作一个可以过滤第一个和第二个字符的程序。我已经尝试了很多,但结果仍然不符合我的期望。这是我所做的代码:
int i; // counter i
int inByte =0;
int numByte =0;
int value = 0;
char*arrayData= "0,1,2";
String stringA = "";
float reading = 0;
void setup() { // initialize serial communications at 9600 bps:
Serial.begin(9600);
analogReference (INTERNAL);
pinMode(13, OUTPUT);
clearBuff();
}
void loop() {
if (Serial.available()) {
numByte = Serial.available();
for (int i =0; i < numByte; i++){
inByte = Serial.read();
}
if (inByte == 13){ //inByte is collected until carriage return;
if (stringA.substring(0,1)== "F"){ //if the first character of stringA is "F", this condition is true and ledShort will be operate;
ledShort();
int value = atoi(arrayData);
if(value = stringA.substring(0,2).toInt()){ //if the second character of stringA is 0,1,2 (according arrayData) this condition is true. Else, it is false;
switch(value){
case 1:
value = (arrayData[1]); // value = 0
break;
case 2:
value = (arrayData[2]); // value = 1
break;
case 3:
value = (arrayData[3]); // value = 2
break;
default:
clearBuff;
Serial.print("");
break;
}
}
}
Serial.println(stringA.substring(0,1));
Serial.println(stringA.substring(0,2).toInt());
Serial.println(value);
Serial.println(stringA);
stringA = "";
clearBuff();
}
else{
stringA += char(inByte);
if (stringA.length() > 15){
clearBuff();
stringA = "";
}
}
}
}
void ledShort() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
}
void clearBuff(){ // flush chars in serial buffer
while (Serial.available() > 0 ){
inByte = Serial.read();
}
}
第一个字符("F"
)没有问题,但此程序无法识别数字0,1,2
。
在串行监视器上键入F0
时,会显示F,0,0,F1
。如果F1
也相同F,0,0,F1
。我想要F1 = F,1,1,F2
。