The switch statement has the following syntax:
switch (expression1) { // opening brace for switch statement block
case 1:
// statements to execute when expression1 is 1
break;
case 2:
// statements to execute when expression1 is 2
break;
case 3:
// statements to execute when expression1 is 3
break;
// more case statements as needed
default:
// statements to execute if expression1 doesn't have a "case value"
break;
} // close brace for switch statement block
/
/ This is the next statement after the switch
switch (inByte) {
case 'a':
digitalWrite(2, HIGH);
break;
case 'b':
digitalWrite(3, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
โปรแกรมภาษา C บน Arduino 2 17
case 'd':
digitalWrite(5, HIGH);
break;
case 'e':
digitalWrite(6, HIGH);
break;
default:
for (int thisPin = 2; thisPin < 7; thisPin++)
{
digitalWrite(thisPin, LOW);
}
}