Tag Archives: Arduino

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:




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 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.

Comparing the Arduino, Raspberry Pi Model 2, and Beaglebone Black

In this video we do a head to head comparison of the Arduino, Raspberry Pi Model 2, and the Beaglebone black. We compare the pros and cons of each platform and discuss how to decide which platform to learn on and which is best for different types of projects.

You can pick up the gear discussed in this video below:

Arduino: This is a great place to start, and the device is very affordable.

Sparkfun Inventor Kit: Everything you need to learn microcontroller programming and circuits. This is the kit we use in our Arduino Lessons, and even includes the Arduino.

Raspberry Pi Kit: This kit has everything you need to follow along on our Raspberry Pi Lessons.

Raspberry Pi: If you already have the cords and cables, you can buy just the Raspberry Pi.

Beaglebone Black: We are not working on a series of lessons showing you how to use the Beaglebone Black. Now would be a good time to go ahead and order your Beagle.

I hope you enjoyed this video lesson, and hope you will jump in and take our lessons on using the Arduino, Raspberry Pi, and the Beaglebone Black

Python with Arduino LESSON 17: Sending and Receiving Data Over Ethernet

Arduino Ethernet
This circuit contains an Arduino Nano and Pressure Sensor Communicating over Ethernet

In LESSON 16 we showed a simple Client Server model that allows us to send strings between Python running on a PC and the arduino over Ethernet. That lesson simply passed strings back and forth to show a very basic Server on Arduino, and Python acting as the Client. In this lesson we show a more practical example, with the Arduino connected to an Adafruit BMP180 Pressure Sensor. In order to complete this lesson, you will need an Arduino, an Ethernet Shield, and the Pressure Sensor. If you do not have this particular pressure sensor, you can probably follow along in the lesson using whatever sensor you have that is of interest. The video will take you through the tutorial step-by-step, and then the code we developed is shown below.

The key issue in getting this project to work is to get your mac address and IP address from your router or network. If you are at school, simply speak to your network administrator, and he will help you get an IP address for your arduino. If you are at home, you will need to connect to your router from a browser, and configure it to assign an IP address and agree on a mac address for your arduino. Some arduino Ethernet shields have a sticker with a mac address. If your Ethernet shield has a sticker with mac address, use that one. If it does not, you will need to come up with a unique mac address. There are thousands of possible routers and networks out there, so I can not help you with that part. But if you look in the router documentation, you should be able to get the IP address and mac address worked out. The arduino itself does not have a hard wired mac address, but you set the mac address in the arduino software, and the IP address as well. The key thing is that the mac address is unique on your network, and the router and arduino agree on the IP address and mac address. If you have a clearer way to explain this, please leave a comment below.

This is the server side software to run on the arduino. Again, you should use a suitable IP address and mac address for your network. Do not think you can just copy the ones I use in the code below.

Once you have this on your arduino, and the arduino connected to the internet via an Ethernet cable, you can test by opening a command line in Windows. Then ping the address you have assigned to the Arduino. If it pings correctly and you get a reply, you are ready to develop the Python code. The Python will be the client. It will send the requests to the Arduino, and the Arduino will respond with data. Since our circuit can measure pressure or temperature, you can request either of those. When the arduino receives a request for temperature, it will go out, make the temperature measurement and then return the data to Python. Similarly, if you request Pressure the arduino will read the request, will make the Pressure measurement, and then return pressure reading to the client (Python).

 This python code will request Temperature, will then read the response, and then will print the data. It then requests Pressure, reads the response, and then prints it. If you look at our earlier lessons you can see graphical techniques to visually present the data. The hard part is getting the data passed back and forth, which we show how to do in this lesson.