const int pinoLED1 = 13;
const int pinoLED2 = 12;
const int pinoInterruptor = 7;
const int pinoBotao1 = 9;
const int pinoBotao2 = 8;
void setup() {
// Abre a conexão serial com taxa de 9600 bits/s.
Serial.begin(9600);
pinMode(pinoLED1, OUTPUT);
pinMode(pinoLED2, OUTPUT);
pinMode(pinoInterruptor, INPUT);
}
void loop() {
if (digitalRead(pinoBotao1) == HIGH) {
digitalWrite(pinoLED1, not digitalRead(pinoLED1));
delay(500);
}
if (digitalRead(pinoBotao2) == HIGH) {
bool sinalLed2 = digitalRead(pinoLED2);
digitalWrite(pinoLED2, not sinalLed2);
delay(500);
}
if ( Serial.available() > 0 ) {
// Lê o conteúdo da serial e armazena na variável cmd.
String cmd = Serial.readString();
// Se controle remoto ligado ...
if (digitalRead(pinoInterruptor) == HIGH) {
// Se o comando recebido é "L"...
if (cmd == "l1" or cmd == "L1" or cmd == "L1\n" or cmd == "l1\n") {
digitalWrite(pinoLED1, HIGH); // Liga o LED
}
else if (cmd == "l2" or cmd == "L2" or cmd == "L2\n" or cmd == "l2\n") {
digitalWrite(pinoLED2, 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(pinoLED1, LOW); // Desliga o LED
}
else if (cmd == "d2" or cmd == "D2" or cmd == "d2\n" or cmd == "D2\n") {
digitalWrite(pinoLED2, LOW); // Desliga o LED
}
// Se o comando é "I"
else if (cmd == "i1" or cmd == "I1" or cmd == "i1\n" or cmd == "I1\n") {
digitalWrite(pinoLED1, not digitalRead(pinoLED1)); // Inverte o LED
}
else if (cmd == "i2" or cmd == "I2" or cmd == "i2\n" or cmd == "I2\n") {
digitalWrite(pinoLED2, not digitalRead(pinoLED2)); // Inverte o LED
}
// Se o comando recebido não é nenhum dos anteriores...
else {
Serial.println("ERRO: comando inválido (" + cmd + ").");
}
}
// Se controle remotor desligado...
else {
Serial.println("ERRO: controle remoto desabilitado.");
}
}
}