/* Protocollo comunicazione su seriale A100 --> grandezza 1 con relativo valore --> salvo in cella memoria 1 (nella 0 metto 'A') B40 --> grandezza 2 con realtivo valore --> salvo in cella memoria 3 (nella 2 metto 'B') C --> richiesta SetPoint grandezza 1 D --> richiesta SetPoint grandezza 2 */ String inString = ""; // string to hold input int Comando; int valoreSP1; // valore di set point grandezza da controllare int valoreSP2; // valore di set point grandezza 2 da controllare float controllo1 = 20; // valore grandezza da controllare float controllo2 = 40; // valore grandezza 2 da controllare void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); } void loop() { // Read serial input: while (Serial.available() > 0) { int inChar = Serial.read(); //Serial.println((char)inChar); if (isDigit(inChar)) { // convert the incoming byte to a char and add it to the string: inString += (char)inChar; } if (isDigit(inChar) == false ) { // controllo se è il comando richiesta valore SP --> NB sono integer --> 'A' singoli apici!! if (inChar == 'A' || inChar == 'B' ) { Comando = inChar; //Serial.print("Comando="); //Serial.println((char)Comando); } if (inChar == 'C') { inString = ""; TornaValoreSP1(); //delay(500); } if (inChar == 'D') { inString = ""; TornaValoreSP2(); //delay(500); } if (inChar == 'E') { inString = ""; TornaValore1(); //delay(500); } if (inChar == 'F') { inString = ""; TornaValore2(); //delay(500); } } // if you get a newline, print the string, then the string's value: if (inChar == '\n') { if (inString != "") { //Serial.print("Valore="); //Serial.println(inString); if (Comando == 'A') { // converto string in numero intero valoreSP1 = inString.toInt(); Serial.print("A="); Serial.println(valoreSP1); //delay(500); } if (Comando == 'B') { // converto string in numero intero valoreSP2 = inString.toInt(); Serial.print("B="); Serial.println(valoreSP2); //delay(500); } // clear the string for new input: inString = ""; Comando= '0'; } } } controllo1= controllo1 + 0.5; if (controllo1>30) { controllo1= 20; } controllo2= controllo2 + 0.5; if (controllo2>60) { controllo2= 40; } //DEBUG Serial.println(controllo1); delay(1000); } void TornaValore1() { Serial.print("E="); Serial.println(controllo1); } void TornaValore2() { Serial.print("F="); Serial.println(controllo2); } void TornaValoreSP1() { Serial.print("C="); Serial.println(valoreSP1); } void TornaValoreSP2() { Serial.print("D="); Serial.println(valoreSP2); }