// Aurduino use C/C++ Programming
/*
Switch statement with serial input
Demonstrates the use of a switch statement. The switch statement allows you
to choose from among a set of discrete values of a variable. It's like a
series of if statements.
To see this sketch in action, open the Serial monitor and send any character.
The characters a, b, c, d, and e, will turn on LEDs. Any other character will
turn the LEDs off.
The circuit:
- five LEDs attached to digital pins 2 through 6 through 220 ohm resistors
created 1 Jul 2009
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/SwitchCase2
*/
void setup() {
// iniciamos el monitor serie
Serial.begin(9600);
// con un for inicializamos todos los pins como salida -output-
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// declaramos una variable char userinput para que el usuario introduzca un caracter
char userInput;
// Pedimos un caracter al usuario
Serial.println("Enter a letter -a a a la e para encender led-:");
while (!Serial.available()) {
// Esperar a que el usuario introduzca caracter
}
userInput = Serial.read();
// if (Serial.available() > 0) {
// int inByte = Serial.read();
// do something different depending on the character received.
// The switch statement expects single number values for each case; in this
// example, though, you're using single quotes to tell the controller to get
// the ASCII value for the character. For example 'a' = 97, 'b' = 98,
// Utilizamos la estructura switch case
// para encender algún pin en función de la letra que introduzca el usuario
// por ejemplo la b encendería el pin 3
switch (userInput) {
case 'a':
digitalWrite(2, HIGH);
delay(5000);
break;
case 'b':
digitalWrite(3, HIGH);
delay(5000);
break;
case 'c':
digitalWrite(4, HIGH);
delay(5000);
break;
case 'd':
digitalWrite(5, HIGH);
delay(5000);
break;
case 'e':
digitalWrite(6, HIGH);
delay(5000);
break;
default:
// turn all the LEDs off:
for (int thisPin = 2; thisPin < 7; thisPin++) {
digitalWrite(thisPin, LOW);
}
}
}