Advertisement
TolentinoCotesta

luca

Jun 9th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. const int NUMBER_OF_FIELDS = 3;
  5. int fieldIndex = 0;
  6. int values[NUMBER_OF_FIELDS];
  7. String serialInput ;  
  8.  
  9. void setup() {
  10.   Serial.begin(9600);
  11.   Serial.println("Start");  
  12. }
  13.  
  14.  
  15. void loop(){
  16.  
  17.   while (Serial.available()) {        
  18.     char inChar = (char)Serial.read();
  19.     serialInput += inChar;    
  20.     // quando ricevo il carattere di fine linea, significa che ho ricevuto tutto
  21.     if (inChar == '\n') {      
  22.       Serial.println(serialInput);
  23.       char * pch;
  24.       unsigned int cont = 0;
  25.  
  26.       // troviamo il primo "token" poi cerchiamo i successivi con un ciclo while      
  27.       pch = strtok ((char*)serialInput.c_str(), " ,");      
  28.       while (pch != NULL)  {
  29.         // converto la stringa in intero e la salvo nell'array numeri
  30.         // Se la conversione non è possibile il risultato sarà 0
  31.         int n = atoi(pch);
  32.         values[cont] = n;    
  33.         Serial.print("Valore n° ");
  34.         Serial.print(cont);
  35.         Serial.print(": ");
  36.         Serial.println(values[cont]);
  37.         // troviamo i token successivi
  38.         pch = strtok (NULL, " ,");
  39.         cont++;
  40.       }      
  41.       serialInput = "";
  42.     }
  43.   }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement