Using the Switch() Statement as Sequencing Control

Written by: admin@makezilla

In this Instructable, we'll be going over the "switch()" statement and it's use in programming. The switch statement is a powerful tool for organizing your program, easily allow you to move through complex flowcharts.

In our switch statement, we'll be going over some different input options to create a sequence of events. We'll start easy, with some timing delays to move actuators, then move to processing some inputs to trigger the next stage of motion. Finally, we'll put everything we learned together to make a simulated factory line!

For this project, we will need:

Ardunio Board

Actuator

Actuator with Potentiometer

- 2x MegaMoto motor shield

- Breadboard

- Push buttons for inputs (you can use a wire connected to ground to simulate buttons)

- Potentiometer (10k preferred, any value works)

- LEDs (can use the built in LED on pin 13)

Let's get started!

Step 1: The switch() Statement

Before we dive into the coding, we need to understand what we are working with. The basics of the switch() statement is that you watch a variable, and depending what the variable's value is, a different case in the switch() is activated. 

This property makes it very easy to make a program pointer that will move through your code, applying all the appropriate inputs and outputs at the correct time. There is also a default case, so that you can put a section of the program to happen when the variable you are watching does not match one of the programmed cases.

For the first example, we'll have a passive code. By watching the serial monitor, you can see the program count to 5 over 5 seconds. The switch statement sits in each case until 1 second has passed, then moves to the next. See the attached code, read the comments, and upload it to see how it works before moving on.

Since these are just test programs, you can leave the board plugged into the computer so the Arduino can receive power.

/*  This code is to show the basic workings of a switch statement.  It counts  to 10 in the serial monitor using a delay.</p><p>  Written by Progressive Automations  Sept 21, 2015  This code is in the public domain */int programCount = 0;//variable to move through the programvoid setup() {  Serial.begin(9600);// initialize serial communication:  programCount = 0;//start at the beginning}//end setupvoid loop() {  switch (programCount) {    case 0:      delay(1000);//wait 1 second      Serial.println("0");//print the number      programCount = 1;      break;    case 1:      delay(1000);//wait 1 second      Serial.println("1");//print the number      programCount = 2;      break;    case 2:      delay(1000);//wait 1 second      Serial.println("2");//print the number      programCount = 3;      break;    case 3:      delay(1000);//wait 1 second      Serial.println("3");//print the number      programCount = 4;      break;    case 4:      delay(1000);//wait 1 second      Serial.println("4");//print the number      programCount = 5;      break;    case 5:      delay(1000);//wait 1 second      Serial.println("5");//print the number      programCount = 6;      break;    default:      Serial.print("Counting complete");      while(1); //freeze the program here  }//end switch}//end loop

Leave a Reply