const int LED1 = 13;
const int LED2 = 12;
void setup() {
// Abre a conexão serial com taxa de 9600 bits/s.
Serial.begin(9600);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop() {
if ( Serial.available() > 0 ) {
// Lê o conteúdo da serial e armazena na variável cmd.
String cmd = Serial.readString();
// Se o comando recebido é "L"...
if (cmd == "l1" or cmd == "L1" or cmd == "L1\n" or cmd == "l1\n") {
digitalWrite(LED1, HIGH); // Liga o LED
}
else if (cmd == "l2" or cmd == "L2" or cmd == "L2\n" or cmd == "l2\n") {
digitalWrite(LED2, HIGH); // Liga o LED
}
// Se o comando recebido é "D"...
else if (cmd == "d1" or cmd == "D1" or cmd == "d1\n" or cmd == "D1\n") {
digitalWrite(LED1, LOW); // Desliga o LED
}
else if (cmd == "d2" or cmd == "D2" or cmd == "d2\n" or cmd == "D2\n") {
digitalWrite(LED2, LOW); // Desliga o LED
}
// Se o comando é "I"
else if (cmd == "i1" or cmd == "I1" or cmd == "i1\n" or cmd == "I1\n") {
digitalWrite(LED1, not digitalRead(LED1)); // Inverte o LED
}
else if (cmd == "i2" or cmd == "I2" or cmd == "i2\n" or cmd == "I2\n") {
digitalWrite(LED2, not digitalRead(LED2)); // Inverte o LED
}
// Se o comando recebido não é nenhum dos anteriores...
else {
Serial.println("ERRO: comando inválido (" + cmd + ").");
}
}
}