Book Image

Arduino Robotic Projects

By : Richard Grimmett
Book Image

Arduino Robotic Projects

By: Richard Grimmett

Overview of this book

Table of Contents (21 chapters)
Arduino Robotic Projects
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Basic programming constructs on Arduino


Now that you know how to enter and run a simple C program on Arduino, let's look at some additional programming constructs. Specifically, you'll see what to do when you want to decide between two instructions to execute and how to execute a set of instructions a number of times.

The if statement

As you have seen, your programs normally start with the first line of code and then continue executing the next line until your program runs out of code. This is fine, but what if you want to decide between two different courses of action? We can do this in C using an if statement. The following screenshot shows some example code:

You'll need to make several changes this time. The first is to add another global variable, int whichLED = 1; at the top of your program. Then, you'll need to add several statements to your loop() function. The line-by-line details are as follows:

  • if (whichLED == 1): This is the if statement. The if statement evaluates the expression...