



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to the c programming language as it is used in arduino sketches. It covers fundamental concepts such as variables, functions, control structures (if, for), and basic arithmetic operations. Practical examples of arduino sketches that demonstrate how to control leds and use the serial monitor for debugging. It is designed to help beginners understand the basics of c programming within the arduino environment, offering a step-by-step guide to writing and understanding simple sketches. The document also explains the compilation process and the role of the setup and loop functions in arduino programming. It emphasizes hands-on experimentation and provides clear explanations of key concepts, making it a valuable resource for anyone starting with arduino programming. The document also covers the use of variables, arithmetic operations, and control structures like if statements and for loops, providing a solid foundation for more advanced programming concepts.
Typology: Study Guides, Projects, Research
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















C. In this chapter, you get to understand the basics of the C language. You will use what you learn here in every sketch you develop as an Arduino programmer. To get the most out of Arduino, you need to understand these fundamentals.
programming
It is not uncommon for people to speak more than one language. In fact, the more you learn, the easier it seems to learn spoken languages as you start to find common patterns of grammar and vocabulary. The same is true of programming languages. So, if you have used any other programming language, you will quickly pick up C.
The good news is that the vocabulary of a programming language is far smaller than that of a spoken language, and because you write it rather than say it, the dictionary can always be at hand whenever you need to look things up. Also, the grammar and syntax of a programming language are extremely regular, and once you come to grips with a few simple concepts, learning more quickly becomes second nature. It is best to think of a program—or a sketch, as programs are called in Arduino—as a list of instructions to be carried out in the order that they are written down. For example, suppose you were to write the following:
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
These three lines would each do something. The first line would set the output of pin 13 to HIGH. This is the pin with an LED built in to the Arduino board, so at this point the LED would light. The second line would simply wait for 500 milliseconds (half a second) and then the third line would turn the LED back off again. So these three lines would achieve the goal of making the LED blink once. You have already seen a bewildering array of punctuation used in strange ways and words that don’t have spaces between them. A frustration of many new programmers is, “I know what I want to do, I just don’t know what I need to write!” Fear not, all will be explained. First of all, let’s deal with the punctuation and the way the words are formed. These are both part of what is termed the syntax of the language. Most languages require you to be extremely precise about syntax, and one of the main rules is that names for things have to be a single word. That is, they cannot include spaces. So, digitalWrite is the name for something. It’s the name of a built-in function (you’ll learn more about functions later) that will do the job of setting an output pin on the Arduino board. Not only do you have to avoid spaces in names, but also names are case sensitive. So you must write digitalWrite , not DigitalWrite or Digitalwrite. The function digitalWrite needs to know which pin to set and whether to set that pin HIGH or LOW. These two pieces of information are called arguments , which are said to be passed to a function when it is called. The parameters for a function must be enclosed in parentheses and separated by commas. The convention is to place the opening parenthesis immediately after the last letter of the function’s name and to put a space after the comma before the next parameter. However, you can sprinkle space characters within the parentheses if you want. If the function only has one argument, then there is no need for a comma. Notice how each line ends with a semicolon. It would be more logical if they were periods, because the semicolon marks the end of one command, a bit like the end of a sentence. In the next section, you will find out a bit more about what happens when you press the Upload button on the Arduino integrated development environment (IDE). Then you will be able to start trying out a few examples.
Figure 3.2 Arduinos don’t speak Italian.
The Arduino has tried to compile the words “Ciao Bella,” and despite its Italian heritage, it has no idea what you are talking about. This text is not C. So, the result is that at the bottom of the screen we have that cryptic message “Ciao does not name a type.” What this actually means is that there is a lot wrong with what you have written. Let’s try another example. This time we will try compiling a sketch with no code at all in it (see Figure 3-3 ).
Figure 3.3 No setup or loop.
This time, the compiler is telling you that your sketch does not have setup or
loop functions. As you know from the Blink example that you ran in Chapter 2 , you have to have some “boilerplate” code, as it is called, before you can add your own code into a sketch. In Arduino programming the “boilerplate” code takes the form of the “setup” and “loop” functions that must always be present in a sketch. You will learn much more about functions later in the book, but for now, let’s accept that you need this boilerplate code and just adapt your sketch so it will compile (see Figure 3-4 ).
Figure 3.4 A sketch that will compile.
The Arduino IDE has looked at your efforts at writing code and found them to be acceptable. It tells you this by saying “Done Compiling” and reporting the size of the sketch to you: 450 bytes. The IDE is also telling you that the maximum size is 32,256 bytes, so you still have lots of room to make your sketch bigger. Let’s examine this boilerplate code that will form the starting point for every sketch that you ever write. There are some new things here. For example, there is the word void and some curly braces. Let’s deal with void first. The line void setup() means that you are defining a function called setup. In Arduino, some functions are already defined for you, such as digitalWrite and delay , whereas you must or can define others for yourself. setup and loop are two functions that you must define for yourself in every sketch that you write. The important thing to understand is that here you are not calling setup or loop like you would call digitalWrite , but you are actually creating these
Blink-again
The reason that Arduino has the two functions setup and loop is to separate the things that only need to be done once, when the Arduino starts running its sketch, from the things that have to keep happening continuously. The function setup will just be run once when the sketch starts. Let’s add some code to it that will blink the LED built onto the board. Add the lines to your sketch so that it appears as follows and then upload them to your board:
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
void loop()
{
}
The setup function itself calls two built-in functions, pinMode and digitalWrite. You already know about digitalWrite , but pinMode is new. The function pinMode sets a particular pin to be either an input or an output. So, turning the LED on is actually a two-stage process. First, you have to set pin 13 to be an output, and second, you need to set that output to be high (5V). When you run this sketch, on your board you will see that the L LED comes on and stays on. This is not very exciting, so let’s at least try to make it flash by turning it on and off in the loop function rather than in the setup function. You can leave the pinMode call in the setup function because you only need
to call it once. The project would still work if you moved it into the loop, but there is no need and it is a good programming habit to do things only once if you only need to do them once. So modify your sketch so that it looks like this:
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
}
Run this sketch and see what happens. It may not be quite what you were expecting. The LED is basically on all the time. Hmm, why should this be? Try stepping through the sketch a line at a time in your head:
You may have noticed the comment at the top of the listing saying “sketch 3- 01.” To save you some typing, we have uploaded to this book’s website all the sketches with such a comment at the top. You can download them from www.arduinobook.com.
Valiables
In this Blink example, you use pin 13 and have to refer to it in three places. If you decided to use a different pin, then you would have to change the code in three places. Similarly, if you wanted to change the rate of blinking, controlled by the argument to delay, you would have to change 500 to some other number in two places. Variables can be thought of as giving a name to a number. Actually, they can be a lot more powerful than this, but for now, you will use them for this purpose. When defining a variable in C, you have to specify the type of the variable. We want our variables to be whole numbers, which in C are called int s. So to define a variable called ledPin with a value of 13, you need to write the following:
int ledPin = 13;
Notice that because ledPin is a name, the same rules apply as those of function names. So, there cannot be any spaces. The convention is to start variables with a lowercase letter and begin each new word with an uppercase letter. Programmers will often call this “bumpy case” or “camel case.” Let’s fit this into your Blink sketch as follows:
// sketch 3-
int ledPin = 13;
int delayPeriod = 500;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(delayPeriod);
digitalWrite(ledPin, LOW);
delay(delayPeriod);
}
We have also sneaked in another variable called delayPeriod. Everywhere in the sketch where you used to refer to 13, you now refer to ledPin , and everywhere you used to refer to 500, you now refer to delayPeriod . If you want to make the sketch blink faster, you can just change the value of delayPeriod in one place. Try changing it to 100 and running the sketch on your Arduino board. There are other cunning things that you can do with variables. Let’s modify your sketch so that the blinking starts really fast and gradually gets slower and slower, as if the Arduino is getting tired. To do this, all you need to do is to add something to the delayPeriod variable each time that you do a blink. Modify the sketch by adding the single line at the end of the loop function so that it appears, as in the following listing, and then run the sketch on the Arduino board. Press the Reset button and see it start from a fast rate of flashing again.
// sketch 3-
delayPeriod. We will come back to arithmetic shortly, but first you need a better way than a flashing LED to see what the Arduino is up to.
Experiment in C
You need a way to test your experiments in C. One way is to put the C that you want to test out into the setup function, evaluate them on the Arduino, and then have the Arduino display any output back to something called the Serial Monitor, as shown in Figures 3-5 and 3-.
Figure 3.5 Writing C in setup.
Figure 3.6 The Serial Monitor.
The Serial Monitor is part of the Arduino IDE. You access it by clicking on the rightmost icon in the toolbar (it looks like a magnifying glass). Its purpose is to act as a communication channel between your computer and the Arduino. You can type a message in the text entry area at the top of the Serial Monitor and when you press Return or click Send, it will send that message to the Arduino. Also if the Arduino has anything to say, this message will appear in the Serial Monitor. In both cases, the information is sent through the USB link. As you would expect, there is a built-in function that you can use in your sketches to send a message back to the Serial Monitor. It is called Serial.println and it expects a single argument, which consists of the information that you want to send. This information is usually a variable. You will use this mechanism to test out a few things that you can do with variables and arithmetic in C; frankly, it’s the only way you can see the results of your experiments in C.
Numeric Variables and Arithmetic
The last thing you did was add the following line to your blinking sketch to increase the blinking period steadily:
delayPeriod = delayPeriod + 100;
Looking closely at this line, it consists of a variable name, then an equals sign, then what is called an expression ( delayPeriod + 100 ). The equals sign does something called assignment. That is, it assigns a new value to a variable, and the value it is given is determined by what comes after the equals sign and before the semicolon. In this case, the new value to be given to the delayPeriod variable is the old value of delayPeriod plus 100. Let’s test out this new mechanism to see what the Arduino is up to by entering the following sketch, running it, and opening the Serial Monitor:
// sketch 3-
void setup()
{
{
Serial.begin(9600);
int degC = 20;
int degF;
degF = degC * 9 / 5 + 32;
Serial.println(degF);
}
void loop()
{}
There are a few things to notice here. First, note the following line:
int degC = 20;
When we write such a line, we are actually doing two things: We are declaring an int variable called degC , and we are saying that its initial value will be 20. Alternatively, you could separate these two things and write the following:
int degC;
degC = 20;
You must declare any variable just once, essentially telling the compiler what type of variable it is—in this case, int. However, you can assign the variable a value as many times as you want:
int degC;
degC = 20;
degC = 30;
So, in the Centigrade to Fahrenheit example, you are defining the variable degC and giving it an initial value of 20, but when you define degF , it does not get an initial value. Its value gets assigned on the next line, according to the conversion formula, before being sent to the Serial Monitor for you to see. Looking at the expression, you can see that you use the asterisk ( ***** ) for multiplication and the slash ( / ) for division. The arithmetic operators **+, –, *** , and / have an order of precedence—that is, multiplications are done first, then divisions, then additions and subtractions. This is in accordance with the usual use of arithmetic. However, sometimes it makes it clearer to use parentheses in the expressions. So, for example, you could write the following:
degF = ((degC * 9) / 5) + 32;
The expressions that you write can be as long and complex as you need them to be, and in addition to the usual arithmetic operators, there are other less commonly used operators and a big collection of various mathematical functions that are available to you. You will learn about these later.
Commands
The C language has a number of built-in commands. In this section, we explore some of these and see how they can be of use in your sketches.
if
In our sketches so far, we have assumed that your lines of programming will be 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? Let’s return to our gradually slowing-down Blinking LED example. At the moment, it will gradually get slower and slower until each blink is lasting hours. Let’s look at how we can change it so that once it has slowed down to a certain point, it goes back to its fast starting speed. To do this, you must use an if command; the modified sketch is as follows.
delayPeriod = 100;
}
}
The if command looks a little like a function definition, but this resemblance is only superficial. The word in the parenthesis is not an argument; it is what is called a condition. So in this case, the condition is that the variable delayPeriod has a value that is greater than 3,000. If this is true, then the commands inside the curly braces will be executed. In this case, the code sets the value of delayPeriod back to 100. If the condition is not true, then the Arduino will just continue on with the next thing. In this case, there is nothing after the “if”, so the Arduino will run the loop function again. Running through the sequence of events in your head will help you understand what is going on. So, here is what happens:
Operator Meaning Examples Result < Less than 9 < 10 10 < 10
true false
> Greater than 10 > 10 10 > 9
false true <= Less than or equal to 9 <= 10 10 <= 10
true true >= Greater than or equal to
true true == Equal to 9 == 9 true != Not equal to 9 != 9 false
To compare two numbers, you use the == command. This double equals sign is easily confused with the character = , which is used to assign values to variables. There is another form of if that allows you to do one thing if the condition is true and another if it is false. We will use this in some practical examples later in the book.
for
In addition to executing different commands under different circumstances, you also often will want to run a series of commands a number of times in a program. You already know one way of doing this, using the loop function. As soon as all the commands in the loop function have been run, it will start again automatically. However, sometimes you need more control than that. So, for example, let’s say that you want to write a sketch that blinks 20 times, then pauses for 3 seconds, and then starts again. You could do that by just repeating the same code over and over again in your loop function, like this:
// sketch 3-
int ledPin = 13;
int delayPeriod = 100;
void setup()