Category Archives: Tutorial

LESSON 33: Understanding Local and Global Variables in Arduino

In lesson 32 we introduced you to the concept of Arduino Functions. Functions are little blocks of code that allow you to break a complicated task down into small logical chunks of code. All the parts of the program shared the same set of variables.  This is the easiest way to do functions, but is really not a good way of doing it. As programs get more complicated, with more functions, unexpected problems can arise if all the parts of the program are sharing the same variables. One function might inadvertently change a variable in use by another function causing unexpected problems. The best way to write modular code is to use local variables.

In Arduino, if a variable is declared at the top of the program, before the void setup, all parts of the program can use that variable. Hence, it is called a Global variable. On the other hand, if the variable is declared between a set of curly brackets, the variable is only recognized within that scope . . . that is, it will only be recognized and can only be used between that set of curly brackets.

For example, if a variable is declared in the void setup, it will not be recognized and can not be used in the void loop, because the void loop is within its own set of curly brackets.

Similarly, if there are two for loops inside the void loop, each for loop has its own set of curly brackets. If a variable is declared inside the first for loop, it will not be recognized inside the other for loop, and will not be recognized in the other parts of the void loop.

This might sound like a hassle, but using local variables really helps you stay out of trouble. The best way to do functions is to use local variable, and inside each function, the variables are declared that are needed by that function. Watch the video and I will give you clear examples of using local and global variables.

 

LESSON 32: Understanding Arduino Functions

So far we have written programs as a long string of code, pretty much all in the void loop. As we begin to need to develop more complicated code, putting all the programming in the void loop can become unmanageable. It is easy to lose track of what we are doing. For more complicated programs, we want to break the problem up into manageable chunks of code. This is called modular program. We develop small modules that do specific tasks, and then our void loop simply calls these modules. The modules are called “Functions” in arduino.

Lets consider an example. Lets say we want to write an arduino program that prompts the user for the number of grades he has. Then it averages the grades, prints the grades and then prints the average. The following program would do this job, with all the code in the void loop:

You can see that the void loop is getting pretty complicated, and it would be easy to begin to lose track of what is going on. If we think about what we are trying to do, lets try to break it down more logically. These are the logical tasks we need to do:

Input Grades

Average Grades

Print Grades and Average

I think that is the logical way to break the program down. Hence, we need three modules or functions, which we could define as follows:

inputGrades();

avGrades();

printGrades();

We could call these three functions in the void loop. then down below the void loop we would need to define, or teach arduino what each of these functions do. In effect, the code in the example above is put down in three logical blocks, which we call functions. Notice that when we do that, the functions must be defined AFTER the void loop. That means it is done after the closing curly bracket for the void loop. Using functions, we can rewrite the program above as follows:

Notice now that the void loop is very simple to understand, since each function is logically named. Also, if we look down at the function definition, it is clear what each chunk of code does. In this example, we are using global variables, so each function, and the void loop are all working with the same set of variables. In future lessons we will look at the use of local variables, and then how that would affect the structure of our functions.

LESSON 30: Advanced Software Interrupt Techniques for Reading Serial Data with Arduino

In Lesson 28 and Lesson 29 we showed how to use simple software interrupts to do quick and easy tasks like turning an LED on or off. We emphasized the importance of just doing simple tasks, and to get in and out of the interrupt function as quickly as possible. It is sometimes necessary to do more sophisticated functions. For example, the Adafruit Ultimate GPS immediately begins sending serial data on powerup. Making matters worse, it sends big bursts of data, and then waits, and then later sends bursts of data again. The data that is sent needs to not just be read, but you must parse the data to make sense of it. Hence, you end up needing to do sophisticated functions to parse the data, but at the same time you have to make sure you do not miss any of the new incoming data. This requires a more thoughtful and sophisticated interrupt strategy.

First off, we will need a better software interrupt library. You should erase the TimerOne library you installed in Lesson 28, and replace it with the one here:

https://github.com/PaulStoffregen/TimerOne

The video shows how to get the library if you are not familiar with github.

The video takes you through the code below. While this is done for the Adafruit GPS, this method should work for other sensors that spew serial data.

While we do this demo for the Adafruit GPS, these techniques will work for other sensors that send data over the Serial Pins.

LESSON 29: The Dos and Don’ts of Arduino Software Interrupts

This is a follow on to lesson 28, to address some of the questions that come up. It is important to understand that all functions are not well suited for use with software interrupts. You must be mindful of timing. Key to being successful with Arduino Software Interrupts is the function called needs to be small and very fast. When the interrupt calls the function, you need to get in and out of that function as quick as you can. Hence, you should avoid doing printing in the function called by the interrupt. You should try and avoid working with serial data, because things can get lost if you are not careful. Also, you should know that you can not use a delay in the function.

For most beginner programmers, interrupts should just be used to call short functions, with minimal lines of code, that can be run quickly.

ARDUINO LESSON 28: Tutorial For Programming Software Interrupts

In this lesson we will show you how to take your arduino programs to the next level by learning to program software interrupts. The challenge with the Arduino platform is that you can only have one program or “thread” running at a time. Hence, something simple like blinking two LED’s at the same time at different rates can not be done because only one line of code is executed at a time. Much more powerful projects can be achieved if we learn how to get around this by programming interrupts. An interrupt can be thought of like an alarm clock. You set the alarm clock, and then when the alarm goes off, no matter where your program is in the execution code, it stops what it is doing, and then runs and executes the interrupt code. In order to use software interrupts, you must load a new library. The TimerOne interrupt library can be downloaded here:

https://code.google.com/archive/p/arduino-timerone/downloads

Click on the TimerOne-r11.zip link, and it will download a zip folder to your computer. Find your Arduino Library folder (Video above explains how if you do not know how). In your Arduino Library folder create a new folder called TimerOne. Take the CONTENTS of the zip folder you just downloaded and put it in your TimerOne library folder. Remember that when you install a new library, you have to close and reopen the Arduino IDE for it to find the new library.

Now you will want to build a simple circuit to allow control of a yellow and red LED. The current limiting resistors in the circuit should be between 200 and 500 ohms. So, lets build the following circuit:

Make sure to connect the long legs of the LED to the control pins.

Now we are ready to create our first interrupts.  First matter of business is your program should load the interrupt library:

This should be at the top of your code, and will load the library.

To create an interrupt, you need to first now initialize the interrupt, specifying what time frame you want it to “interrupt” on,  and then what you want it to do when the interrupt alarm goes off. You would typically set these two things up in the Void Setup. Consider this sample code:

If you place this code in the void setup, it will do two things. The first is it will initialize an interrupt that will go off every .1 seconds. Understand the units for the Timer1.initialize command are in microseconds, so 100,000 microseconds would create an interrupt every .1 seconds. Now you have to tell arduino what to do when the interrupt goes off. This is done with the Timer1.attachInerrupt command. In the code above, you can see that the command is telling the arduino that every time the interrupt goes off, it should pause what it is doing, go and run the BlinkYellow function, and then afterwords return to whatever it was doing.

So, lets pull all this together to crate a program that will blink a Red LED slowly, 1 second on, followed by one second off, and then at the same time blink a yellow LED quickly using the Timer1 interrupt. The following code would do just that.