2016-06-23 14 views
0

私はArduino Unoにはとても新しいので、いくつかのアドバイスが必要なので....ここで私たちは行く。arduino unoもし文字列が単語に一致するならば

私は私のArduinoを使用したい:
1.私のシリアルデータは、データの行の中にプレーンテキストとして受信した特定の単語のための
2.外観を--received読ん
3.のみ/印刷送信それは特定の「言葉」

が含まれている場合、完全な文字列は、私は、このスケッチを発見し、それは私が私はStringクラスのメソッドを使用する方が良いと思いCHAR

// Example 3 - Receive with start- and end-markers 

const byte numChars = 32; 
char receivedChars[numChars]; 

boolean newData = false; 

void setup() { 
    Serial.begin(9600); 
    Serial.println("<Arduino is ready>"); 
} 

void loop() { 
    recvWithStartEndMarkers(); 
    showNewData(); 
} 

void recvWithStartEndMarkers() { 
    static boolean recvInProgress = false; 
    static byte ndx = 0; 
    char startMarker = '<'; 
    char endMarker = '>'; 
    char rc; 

    while (Serial.available() > 0 && newData == false) { 
     rc = Serial.read(); 

     if (recvInProgress == true) { 
      if (rc != endMarker) { 
       receivedChars[ndx] = rc; 
       ndx++; 
       if (ndx >= numChars) { 
        ndx = numChars - 1; 
       } 
      } 
      else { 
       receivedChars[ndx] = '\0'; // terminate the string 
       recvInProgress = false; 
       ndx = 0; 
       newData = true; 
      } 
     } 

     else if (rc == startMarker) { 
      recvInProgress = true; 
     } 
    } 
} 

void showNewData() { 
    if (newData == true) { 
     Serial.print("This just in ... "); 
     Serial.println(receivedChars); 
     newData = false; 
    } 
} 

答えて

関連する問題