The switch - case statement is a powerful construct that is often under-used by beginners.
Basically it allows you to perform tests on a value (or range of values) and make decisions - a bit like the IF statement.
This tutorial has three examples that progressively introduce some simple ideas to help you use the switch construct in your programming.
You will need an Arduino Uno (the programs will run on most arduinos) three leds - preferably red, yellow, green - some resistors around 220 ohms - 2k2 - and a potentiometer.
- Example 1 which follows tests a value from the ADC and shows "good" and "alarm" conditions on LEDs
- Example 2 reads values from the keyboard via the serial monitor and identifies them as capitals , numbers etc.
- Example 3 uses the switch construct with an EASY example of millis() timing to build a traffic light "state machine". STILL EASY.
Example 1:
Suppose you've read in a value from a potentiometer using the ADC. We will start by dividing it by 64 to get a value "reading" holding a number in the range 0 -- 15.
Now we will use some LEDs to show the state of the value.
0 - red; 1 - red & amber; 2 to 4 - amber; 5 to 14 - green; and 15 - green & amber.
You can easily do this with nested if statements - but here's how to do it with "switch"
switch (reading) {
//here we are testing for a single value
case 0: { // put the red LED on
}
break;
case 1 : { //red and amber on
}
break;
//you can also test for a range of values
case 2 ... 4 : { // just show amber
}
break;
case 5 ... 14: { // all good - show green
}
break;
case 15: { //bit high - show green and amber
}
break;
}
Note - in specifying a range you must use this exact format
case (low value) space three dots space (high value): as shown above.
OK I hate Fritzing but here is how to connect the potentiometer and leds to your Uno.
And now the full code for the sketch:
/*
This simple sketch is an example showing how the switch - case structure can be used to avoid nested if statements
LEDs are connected to pins 2,3,4 on the uno, with 470 ohm resistors to ground
a potentiometer between 0V and +V provides a variable input to A0
J. Errington 23 October 2020
*/
// assign pin numbers for the leds
const byte led_R = 4;
const byte led_Y = 3;
const byte led_G = 2;
//input on analog pin 0
const byte vIn = A0;
int reading;
void setup() {
Serial.begin(9600);
pinMode(led_R, OUTPUT);
pinMode(led_Y, OUTPUT);
pinMode(led_G, OUTPUT);
reading = analogRead(vIn); // dummy read to settle ADC
}
void loop() {
reading = analogRead(vIn);
Serial.println(reading);
reading = reading / 64; //just to get easy numbers
delay(100);
// voltage warning lights
switch (reading) {
case 0: { //R
digitalWrite(led_R, HIGH);
digitalWrite(led_Y, LOW);
digitalWrite(led_G, LOW);
}
break; // try the effect of commenting this out
case 1 : { // RA
digitalWrite(led_R, HIGH);
digitalWrite(led_Y, HIGH);
digitalWrite(led_G, LOW);
}
break;
case 2 ... 4 : { //A
digitalWrite(led_R, LOW);
digitalWrite(led_Y, HIGH);
digitalWrite(led_G, LOW);
}
break;
case 5 ... 14: { //G
digitalWrite(led_R, LOW);
digitalWrite(led_Y, LOW);
digitalWrite(led_G, HIGH);
}
break;
// if its not any of the above then it must be 15.
default: { //GA
digitalWrite(led_R, LOW);
digitalWrite(led_Y, HIGH);
digitalWrite(led_G, HIGH);
}
}
}//loop
"default" is just a catch-all case for any values that dont match the specified tests.
Example 2: testing characters follows.
switch_warnvolts.ino (1.69 KB)