Chapter 3. Simple Programming Concepts Using the Arduino IDE

Now that you have downloaded, installed, and initiated the Arduino IDE, in this chapter, you'll learn some basic programming concepts. If you are already comfortable with programming, especially the C programming language, you can skip this chapter. If you're not, or need a quick review, this chapter discusses some simple programming examples on how to program Arduino. At the end of the chapter, I'll cover additional important programming constructs.

To get started, open your Arduino IDE and make sure your Arduino is connected to your development environment via its USB cable. You may want to open and run the Blink example from Chapter 2, Getting Started with the Arduino IDE. When you have uploaded the file successfully, the IDE should tell you by displaying Done uploading in the lower-left corner of the IDE.

The program should also be running on Arduino and the orange LED blinking. If all of this seems clear and natural, you may consider skipping this chapter. If, however, this all feels new to you, welcome to Chapter 3, Simple Programming Concepts Using the Arduino IDE! In this chapter, you'll learn about creating, editing, and running programs on Arduino.

Creating, editing, and saving files on Arduino

You know how to start the IDE, but you've not been introduced to all the available functionalities. So let's take a quick tour of the IDE. Again, here is what the IDE should look like when you first start it up:

Creating, editing, and saving files on Arduino

Note that at the top of the IDE, there are five topic menus: File, Edit, Sketch, Tools, and Help. Each of these tabs holds a set of functionalities that you'll need. If you select the File tab, you should see the following screenshot:

Creating, editing, and saving files on Arduino

These selections let you create, open, upload, and print files in the Arduino IDE. Once you click on File, you will get the following options:

  • New: This is straightforward; you use this if you want to create a new file.
  • Open…: This is equally clear; select this if you want to open a file that you created earlier.
  • Sketchbook: This is probably a new term, one with which you are not familiar. Arduino programs are called sketches, and Sketchbook keeps track of sketches you have created. You'll also find sketches placed there if you have downloaded libraries associated with additional HW shields you may have purchased.
  • Examples: This selection has a treasure trove of example programs created by the Arduino community. The IDE comes with a number of basic examples, but as an additional functionality associated with HW, others can place examples there as well. Chapter 4, Accessing the GPIO Pins, will cover this in more detail.
  • Close: This is self-explanatory; it will close the open sketch.
  • Save and Save As…: These allow you to save a sketch to a file. Generally, these will be appended with the .ino extension to differentiate them from other files.
  • Upload: You've already used the Upload menu; it has the same function as clicking the Upload button directly on the IDE. This will compile and send your code to your Arduino.
  • Upload Using Programmer: This is used only when you want an external programmer to upload code to Arduino. This is sometimes used when you have access to an external programmer and don't want to use the Arduino bootloader. To know more about this case, visit http://Arduino.cc/en/Hacking/Programmer#.Uyhr_IUXdNp.
  • Page Setup and Print: These allow you to set up and print your sketch.
  • Preferences: This brings up a set of selections to set defaults, including where to store your sketches, the default language, and other settings that you normally don't need to change.

If you click on the Edit tab, you will see the following options:

Creating, editing, and saving files on Arduino

I'll not cover each of these options in detail; they are standard edit-type commands. If you click on the Sketch tab, you should see the following screenshot:

Creating, editing, and saving files on Arduino

Under the Sketch tab, you will get the following options:

  • Verify / Compile: This allows you to compile and verify your code. This checks whether your code can actually be turned into a program that can be run on Arduino before you try and upload it to the hardware.
  • Show Sketch Folder: This will open a window that shows all of the sketches in the default directory.
  • Add File…: This allows you to add a file to your sketch. Now that may seem odd because you may have assumed that a sketch is a file. But a sketch can contain code that is not just in one file but in several files. We will cover this feature later in the chapter.
  • Import Library…: This is an important option as it allows you to bring in code capabilities that someone else developed; often this is associated with additional hardware that you want to access. Arduino already comes with a large number of functions that you have access to. You don't have to write these sets of code because they are available for you to simply call. You can also add to this set of functionalities by importing additional libraries. Additional HW normally comes with code libraries that provide access to its features. Instead of having to cut and paste this code into your code window or add the files to your sketch, importing these files as libraries allows you to access the functionalities as if they were in files already associated with your sketch.

The next menu is the Tools tab. When you click on it, you should see the following screenshot:

Creating, editing, and saving files on Arduino

You've already used the Board and Serial Port selections. The other choices under the Tools tab are as follows:

  • Auto Format: This automatically formats the code in the current sketch, placing indents where it thinks they should be.
  • Archive Sketch: This takes the sketch and file associated with it and places them in a *.zip file in the same directory.
  • Fix Encoding & Reload: This will sometimes clean up files that are encoded with characters that can't be displayed correctly.
  • Serial Monitor: This opens a serial connection between you and Arduino. You can use this to communicate with the board via a USB connection that acts like a serial port. You'll need to have the serial communication commands in your code.
  • Programmer and Burn Bootloader: These allow you to access an Arduino system without the bootloader. However, since you won't be requiring these, we'll not cover them here.

The last menu, Help, provides help selections. It's simple and self-explanatory, so you can explore the help system on your own. Now that you know your way around the IDE, you can start programming your Arduino.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.196.146