SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdio.h> | |
2 | #include <string.h> | |
3 | ||
4 | const int NUMBER_OF_FIELDS = 3; | |
5 | int fieldIndex = 0; | |
6 | - | Serial.begin(9600); |
6 | + | |
7 | String serialInput ; | |
8 | ||
9 | - | if (Serial.available()) |
9 | + | |
10 | - | { |
10 | + | Serial.begin(9600); |
11 | - | char ch = Serial.read(); |
11 | + | Serial.println("Start"); |
12 | - | if(ch >= '0' && ch <= '9') |
12 | + | |
13 | - | { |
13 | + | |
14 | - | if(fieldIndex < NUMBER_OF_FIELDS) |
14 | + | |
15 | - | { |
15 | + | |
16 | - | values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0'); |
16 | + | |
17 | - | } |
17 | + | while (Serial.available()) { |
18 | char inChar = (char)Serial.read(); | |
19 | - | else if (ch ==','){ |
19 | + | serialInput += inChar; |
20 | - | fieldIndex++; |
20 | + | // quando ricevo il carattere di fine linea, significa che ho ricevuto tutto |
21 | if (inChar == '\n') { | |
22 | - | else{ |
22 | + | Serial.println(serialInput); |
23 | - | for(int i=0; i < min(NUMBER_OF_FIELDS, fieldIndex+1); i++){ |
23 | + | char * pch; |
24 | - | Serial.println(values[i]); |
24 | + | unsigned int cont = 0; |
25 | - | values[i] =0; |
25 | + | |
26 | // troviamo il primo "token" poi cerchiamo i successivi con un ciclo while | |
27 | - | fieldIndex = 0; |
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 | } |