Variables

Variables give a name to a number. Actually, they can be a lot more powerful than this, but for now, we’ll use them for this purpose.

When defining a variable in C, you have to specify the type of variable. For example, if you want your variables to be whole numbers, you would use int (short for integer). To define a variable called delayPeriod with a value of 200, you need to write:

int delayPeriod = 200;

Notice that because delayPeriod is a name, there cannot be any spaces between words. The convention is to start variables with a lowercase letter and begin each new word with an uppercase letter. Programmers often call this bumpy case or camel case.

Let’s fit this into the blink sketch, so that instead of “hard-coding” the value 200 for the length of delay, we use a variable instead. While we are at it, we can replace that potentially troublesome LED_BUILTIN with our own variable called just “led,” assigned explicitly to pin 13.

Notice, that in the example below I have moved the opening “{” (curly brace) onto a line of its own. This does not alter the way that the program works at all, but I think it makes the code easier to read. Note that this is very much a matter of personal taste.

int led = 13;
int delayPeriod = 200;

void setup()
{
	pinMode(led, OUTPUT);
}

void loop()
{
	digitalWrite(led, HIGH); delay(delayPeriod); digitalWrite(led, LOW); delay(delayPeriod);
}

At each place in the sketch where we used to refer to 200, we now refer to delayPeriod. Now, if you want to make the sketch blink faster, you can just change the value of delayPeriod in one place.

If

Normally, your lines of program are executed in order one after the other, with no exceptions. But what if you don’t want to do that? What if you only want to execute part of a sketch if some condition is true?
A good example of that might be to only do something when a button, attached to the Arduino, is pressed. The code might look like this:

void setup()
{
	pinMode(5, INPUT_PULLUP);
	pinMode(9, OUTPUT);
}
void loop()
{
	if (digitalRead(5) == LOW)
	{
		digitalWrite(9, HIGH);
	}
}

Loops

As well as conditionally performing some of the actions, you also need your sketch to be able to repeat actions over and over again. You get this for free of course by putting commands into the sketch’s loop function. That is, after all, what happens with the Blink example.

Sometimes, however, you’ll need to be more specific about the number of times that you want to repeat something. You can accomplish this with the for command, which allows you to use a counter variable.

For example, let’s write a sketch that blinks the LED ten times. Later, you’ll see why this approach might be considered less than ideal under some circum- stances, but for now, it will do just fine.

// sketch 01_01_blink_10
int ledPin = 13;
int delayPeriod = 200;

void setup()
{
	pinMode(ledPin, OUTPUT);
}
void loop()
{
	for (int i = 0; i < 10; i++)
	{
		digitalWrite(ledPin, HIGH); delay(delayPeriod); digitalWrite(ledPin, LOW); delay(delayPeriod);
	}
}

NOTE: To install all the sketches into your Arduino environment, unzip the file containing the sketches into your Arduino directory, which you’ll find in your Documents folder. The Arduino IDE automatically creates this folder for you the first time it is run.

The for command defines a variable called i and gives it an initial value of 0. After the ; the text i < 10 appears. This is the condition for staying in the loop. In other words, while i is less than 10, keep doing the things inside the curly brackets.

The last part of the for command is i++. This is C shorthand for “i = i + 1”, which, not surprisingly, adds 1 to the value of i. One is added to the value of i each time around the loop. This is what ensures that you can escape from the loop, because if you keep adding 1 to i, eventually it will be greater than 10.

Functions

Functions are a way to group a set of programming commands into a use- ful chunk. This helps to divide your sketch into manageable chunks, making it easier to use.

For example, let’s write a sketch that makes the Arduino blink rapidly 10 times when it first starts and then blink steadily once each second thereafter.

Read through the following listing, and then I’ll explain what is going on.

// sketch 01_02_blink_fast_slow 
int ledPin = 13;

void setup()
{
	pinMode(ledPin, OUTPUT); flash(10, 100);
}

void loop()
{
	flash(1, 500);
}

void flash(int n, int delayPeriod)
{
	for (int i = 0; i < n; i++)
	{
		digitalWrite(ledPin, HIGH); 
		delay(delayPeriod); 
		digitalWrite(ledPin, LOW); 
		delay(delayPeriod);
	}
}

The setup function now contains a line that says flash(10, 100);. This means flash 10 times with a delayPeriod of 100 milliseconds. The flash command is not a built-in Arduino command; you are going to create this quite useful function yourself.

The definition of the function is at the end of the sketch. The first line of the function definition is

void flash(int n, int delayPeriod)

This tells the Arduino that you are defining your own function called flash, which takes two parameters, both of which are ints. The first is n (the number of times to flash the LED), and the second is delayPeriod (the delay to use between turning the LED on or off).

These two parameter variables can only be used inside the function. So, n is used in the for command to determine how many times to repeat the loop, and delayPeriod is used inside the delay commands.

The sketch’s loop function also uses the previous flash function, but with a longer delayPeriod, and it only makes the LED flash once. Because it is inside loop, it will just keep flashing anyway.

Leave a Reply