Introduction: If, For, While, Map, and Switch Cases

This instructable covers the basic programming functions which should allow you to begin programming straight away. Note that these functions apply to all programming languages and are not specific to Arduino.

Step 1: If Statements

The if() statement is one of the most fundamental control structures throughout all programming, not just Arduino. It allows for you to make something happen, or not, depending on whether a given condition is true or not.

The basic structure of an if() statement is as follows:

if(someCondition){
//The code you want to execute if someCondition is true

}

The most common variant of if() is the if-else statement, which allows you to execute a different section of code if the condition you want to check for is not true.
The structure of this is very similar to the original, however a small section is added to the end of the code:

if(someCondition){
//The code you want to execute if someCondition is true

} else {

//The code you want to execute if someCondition is false

}

This can be slightly edited to produce an else if statement, which allows you to check a second condition if the first is false:

if(firstCondition){
//The code you want to execute if firstCondition is true

} else if( secondCondition ){

//The code you want to execute if firstCondition is false, and secondCondition is true

}

Finally, all of these variants can be used in what are called ‘nested’ if statements. In a nested if statement, there are if statements placed within if statements. For example:

if(someCondition){
if(otherCondition){ //The code you want to execute if someCondition and otherCondition are true

}else{ //The code you want to execute if someCondition is true, and otherCondition is false

}

}

Step 2: For Loops

A FOR loop is used to repeat a block of statements. They will usually implement an increment counter, which is used to terminate the loop, once the desired amount of loops has been executed.

There are three parts to the for loop statement:

For (initialization; condition; increment) {
//The code you want to loop

}

This is clarified in the image above.

The initialization happens first and only once. Each time the loop executes, the condition is tested. If it is true, the statement block and increment, is executed, then the condition is tested again. When the condition is tested and returns false, the loop ends.

Example

Int PWMpin = 10; //LED in series with 470 ohm resistor on pin 10

void setup(){

pinMode(PWMpin, OUTPUT);

}

void loop(){

for(int i=0; i<= 255; i ++){

analogWrite(PWMpin, i);

delay(10)

}

}

Step 3: While Loops

A while loop, similarly to a for loop, will run a certain block of code repeatedly. With a while loop, however, the loop will continue infinitely until the expression inside the parenthesis, (), becomes false. Something must change the tested variable or the while loop will never exit.

The syntax of a while loop is as follows:

while(statement){

//The code you want to execute repeatedly, until statement is set to false

}

Example

Var = 0;

while(var < 200){

//The code you want loop until var is no longer than 200

Var++;

}

Step 4: Map

A map function is a pretty simple, but extremely useful function to know when one is manipulating inputs and making sure that you have the desired output. The function takes in a number and spits out a new number which has been manipulated so that it can be used later on.

The syntax of the map function is such:

int newValue = map(input, oldMin, oldMax, newMin, newMax);<br>

This line will take a number, input, between the value of oldMin and oldMax and adjust it to lie between newMin and newMax. The output will maintain its relative position between the upper and lower bounds.

In the below code, an input of 50 (halfway between the min and max input) will return an output of 550 (halfway between the min and max output). Also note that an input of -1 (below the input minimum) will also be processed without any issues, returning a value of 499. The map function does not add constraints. To do this, the constrain function must be used.

int output = map(input, 0, 100, 500, 600);

The function may be useful in applications such as the following: Your robot controller inputs a value for speed between 0 and 100. You want to adjust your robots speed using the analogWrite() function which takes a PWM value between 0 and 255. The code below is an example of how you may manipulate the value as needed to be fed into the analogWrite() function.

int output = map(input, 0, 100, 0, 255);

analogWrite(pin, output);

Step 5: Switch Case Statements

An if statement allows you to choose between two discrete options, TRUE or FALSE. When there are more than two options, you can use multiple if statements or you can use the switch statement, which allows you to choose between several discrete options.

In general a switch statement is laid out as follows:

var is the variable whose value we want to compare to the various classes

label is the value to which we wish to compare the variable to.

switch (var) {
case label:

// statements

break;

case label:

// statements

break;

default:

// statements

break;

}

Example
switch(var){

case 1:

//The code you want to execute when var equals 1

break;

case 2:

//The code you want to execute when var equals 2

break;

default:

//If nothing else matches, this is the code that is executed.

break;

}

NOTE

In order to declare variables within a case, brackets are needed, as shown below:


switch (var) {

case 1:

{

//do something when var equals 1

int a = 0;

.......

.......

}

break;

default:

// if nothing else matches, this is the code that is executed

break;

}

Comments