Semplice buffer seriale

Ciao a tutti,
con questo semplice codice,faccio accendere e spegnere i led inviando un carattere alla seriale.

Avrei la necessità di creare un buffer di lettura della seriale per strutturare meglio il comando,
anzichè inviare "1" avrei la necessità di inviare 3 caratteri ovvero A01

qualcuno mi spiega come costruire il buffer di lettura?

grazie!

char val;
int ledPin0 = 13;    // LED connected to the Wiring I/O board pin0 
int ledPin1 = 12;    // LED connected to the Wiring I/O board pin1 
int ledPin2 = 11;    // LED connected to the Wiring I/O board pin2
void setup() 
{ 
  pinMode(ledPin0, OUTPUT); // sets the digital pin as output
  pinMode(ledPin1, OUTPUT); // sets the digital pin as output
  pinMode(ledPin2, OUTPUT); // sets the digital pin as output
  Serial.begin(9600);       // opens the serial port
}

void off()
{
  // sets all LEDs off
  digitalWrite(ledPin0, LOW);
  digitalWrite(ledPin1, LOW);       
  digitalWrite(ledPin2, LOW);    
  Serial.println("All off");
}

void on()
{
  // sets all LEDs on
  digitalWrite(ledPin0, HIGH);
  digitalWrite(ledPin1, HIGH);       
  digitalWrite(ledPin2, HIGH);    
  Serial.println("All on");
}

void loop() {

  if(Serial.available() > 0) {  

    val = Serial.read();

    if(val == '1'){
      digitalWrite(ledPin0, HIGH);  // sets the LED on
      Serial.println("LED 0 on");   // sends:on
    } 
    else if(val == '2'){
      digitalWrite(ledPin0, LOW);   // sets the LED off
      Serial.println("LED 0 off");  // sends:off
    }
    else if(val == '3'){
      digitalWrite(ledPin1, HIGH);  // sets the LED on
      Serial.println("LED 1 on");   // sends:off
    }
    else if(val == '4'){
      digitalWrite(ledPin1, LOW);   // sets the LED off
      Serial.println("LED 1 off");  // sends:off
    } 
    else if(val == '5'){
      digitalWrite(ledPin2, HIGH); // sets the LED on
      Serial.println("LED 2 on");  // sends:off
    } 
    else if(val == '6'){
      digitalWrite(ledPin2, LOW);  // sets the LED off
      Serial.println("LED 2 off"); // sends:off
    } 
    else if(val == 'o'){
      on();
    }
    else if(val == 'x')
    {
      off();
    }

  }
  delay(100); // wait 100ms for next reading 
}

devi risolverla te via software ma se non sbaglio sulla seriale puoi ricevere un solo carattere (byte) per volta.

ciao Giorgio,
potresti provare a:

dichiarare val come char 3

#char val;
char val[3];

e leggere 3 caratteri invece

#val = Serial.read();
val[0] = Serial.read();
val[1] = Serial.read();
val[2] = Serial.read();

questo è il modo più facile che DOVREBBE funzionare :slight_smile:

Così come gentilmente mi suggerisci mi da alcuni oscuri errori in fase di compilazione.
Devo ancora studiare e apprendere bene la sintassi e la struttura del linguaggio:

#char val;
char val[3];

int ledPin0 = 13;    // LED connected to the Wiring I/O board pin0
int ledPin1 = 12;    // LED connected to the Wiring I/O board pin1
int ledPin2 = 11;    // LED connected to the Wiring I/O board pin2
void setup()
{
  pinMode(ledPin0, OUTPUT); // sets the digital pin as output
  pinMode(ledPin1, OUTPUT); // sets the digital pin as output
  pinMode(ledPin2, OUTPUT); // sets the digital pin as output
  Serial.begin(9600);       // opens the serial port
}

void off()
{
  // sets all LEDs off
  digitalWrite(ledPin0, LOW);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  Serial.println("All off");
}

void on()
{
  // sets all LEDs on
  digitalWrite(ledPin0, HIGH);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  Serial.println("All on");
}

void loop() {

  if(Serial.available() > 0) {

  #val = Serial.read();
val[0] = Serial.read();
val[1] = Serial.read();
val[2] = Serial.read();

    if(val == 'F11'){
      digitalWrite(ledPin0, HIGH);  // sets the LED on
      Serial.println("LED 0 on");   // sends:on
    }
    else if(val == 'F10'){
      digitalWrite(ledPin0, LOW);   // sets the LED off
      Serial.println("LED 0 off");  // sends:off
    }
    else if(val == '3'){
      digitalWrite(ledPin1, HIGH);  // sets the LED on
      Serial.println("LED 1 on");   // sends:off
    }
    else if(val == '4'){
      digitalWrite(ledPin1, LOW);   // sets the LED off
      Serial.println("LED 1 off");  // sends:off
    }
    else if(val == '5'){
      digitalWrite(ledPin2, HIGH); // sets the LED on
      Serial.println("LED 2 on");  // sends:off
    }
    else if(val == '6'){
      digitalWrite(ledPin2, LOW);  // sets the LED off
      Serial.println("LED 2 off"); // sends:off
    }
    else if(val == 'o'){
      on();
    }
    else if(val == 'x')
    {
      off();
    }

  }
  delay(100); // wait 100ms for next reading
}