Arduino Uno Example Sketch: Switch Case

In this example the user will learn to use a switch case statement. A switch case statement is used in place of multiple if statements. When using a switch case, the program will take a variable, in the example below it is “range”, and compare it to several cases. It will then select the matching case, and run the code listed under it before returning to the top and looping through the code again. In the example below, we have mapped “range” into numbers 0-3. When the program gets to the switch case, it will compare the variable “range” to the 4 cases and choose which code to follow. Once the code has ran, it will hit the break command and begin to loop through the program again. If the break is not placed in the code, it will continue to run through the next case, so it is very important to include the break.

Starting Example Code


// sensor minimum, discovered through experiment
const int sensorMin = 320; 

// sensor maximum, discovered through experiment
const int sensorMax = 870;

void setup() {
	// Begin serial communication
	Serial.begin(9600);
}

void loop() {

	// read the photocell and store it in the variable
	int sensorReading = analogRead(A0); 

	/* I added the following line to the example code to calibrate the photocell in the following section*/

	//Print the reading from the photoresistor to the serial monitor. This will be used to set the
	// Serial.println(sensorReading);  constants sensorMin and sensorMax.

	// map the sensor range to a range of four options
	int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

	switch (range) { // this Switch case is used to choose which option to follow. Above we mapped it into 4 selections, 0-4

		case 0: // your hand is on the sensor

			// write dark on the serial monitor
			Serial.println("dark"); 

			break; //used to break away from the code and recheck the variable

		case 1: // your hand is close to the sensor

			// write dim on the serial monitor
			Serial.println("dim");

			break;

		case 2: // your hand is a few inches from the sensor

			// write medium on the serial monitor
			Serial.println("medium");

			break;

		case 3: // your hand is nowhere near the sensor

			// write bright on the serial monitor
			Serial.println("bright");

			break;
	}
	delay(1); // this will help the stability of the readings
}

Calibrating the Constants for the Photocell

To calibrate the constants for the photocell set up your code like the following example code.

// sensor minimum, discovered through experiment
const int sensorMin = 320;

// sensor maximum, discovered through experiment
const int sensorMax = 870;

void setup() {
	// Begin serial communication
	Serial.begin(9600);
}

void loop() {

	// read the photocell and store it in the variable
	int sensorReading = analogRead(A0); 

	//Print the reading from the photoresistor to the serial monitor. This will be used to set the constants sensorMin and sensorMax.
	Serial.println(sensorReading); 

	// map the sensor range to a range of four options
	//int range = map(sensorReading, sensorMin, sensorMax, 0, 3); 

	// switch (range) { // this Switch case is used to choose which option to follow. Above we mapped it into 4 selections, 0-4

		// case 0: // your hand is on the sensor

			// write dark on the serial monitor
			// Serial.println("dark");

			// break; //used to break away from the code and recheck the variable

		// case 1: // your hand is close to the sensor

			// write dim on the serial monitor
			// Serial.println("dim");

			// break;

		// case 2: // your hand is a few inches from the sensor

			// write medium on the serial monitor
			// Serial.println("medium");

			// break;

		// case 3: // your hand is nowhere near the sensor


	// }
	delay(1); // this will help the stability of the readings
}

Once your code is set up like the above code, load it into the board and open the Serial Monitor and record the value of the photocell with full light entering. Next cover the photocell with you finger, so no light is getting in. Record this value. With these values recorded we are ready to move on to the example code and to get it reading the values correctly.

Change your code back to the original example code. Next change the sensorMin constant to the lowest number you got when the sensor was covered in the calibration test above. Then change the sensorMax constant to the highest number that you got with the sensor uncovered. Now reload the program into the Arduino board.

Now when you move your finger closer to the sensor, you should notice that when the sensor is uncovered it should read “bright” on the serial monitor. As you move closer to the sensor with your finger it will switch to “medium”, then “dim” and when you are finally covering the sensor it will read “dark”. If you run into a problem where the serial monitor doesn’t every show either “bright” or “dark”, you can go through the calibration again and see if the numbers have changed.

Initial Code Wiring Diagram

Expanded Code

In the expanded code we will add a couple of LEDs to the circuit to present a visualization of what is happening with the photocell. We will attach LEDs to pins 11, 12, and 13 along with a 330 ohm resistor. What the program will do is when the photocell senses that it is full brightness all LEDs will turn on. As it gets darker, the LEDs will turn off one by one until they are all off when it senses dark.

// sensor minimum, discovered through experiment
const int sensorMin = 320;

// sensor maximum, discovered through experiment
const int sensorMax = 870;

void setup() {
	// Begin serial communication
	Serial.begin(9600);

	//Set pin 13 as an ouput
	pinMode(13, OUTPUT);

	// Set pin 12 as an output
	pinMode(12, OUTPUT);

	// Set pin 11 as an output
	pinMode(11, OUTPUT);
}

void loop() {

	// read the photocell and store it in the variable
	int sensorReading = analogRead(A0);

	// map the sensor range to a range of four options
	int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

	switch (range) {
		case 0: // your hand is on the sensor

			// write dark on the serial monitor
			Serial.println("dark");

			// turn off all LEDs
			digitalWrite(11, LOW);
			digitalWrite(12, LOW);
			digitalWrite(13, LOW);

			break;

		case 1: // your hand is close to the sensor

			// write dim on the serial monitor
			Serial.println("dim");

			// turn on just the LED connected to pin 11
			digitalWrite(11, HIGH);
			digitalWrite(12, LOW);
			digitalWrite(13, LOW);

			break;

		case 2: // your hand is a few inches from the sensor

			// write medium on the serial monitor
			Serial.println("medium");

			// turn on the LEDs connected to pins 11 and 12
			digitalWrite(11, HIGH);
			digitalWrite(12, HIGH);
			digitalWrite(13, LOW);

			break;

		case 3: // your hand is nowhere near the sensor

			// write bright on the serial monitor
			Serial.println("bright");

			// turn on all LEDs
			digitalWrite(11, HIGH);
			digitalWrite(12, HIGH);
			digitalWrite(13, HIGH);

			break;
		}
	delay(1); // this will help the stability of the readings
}

Expanded Code Wiring Diagram

Bill of Materials

Link to Cart: https://www.digikey.com/short/pq5b8c

1 Like