Reader small image

You're reading from  C Programming for Arduino

Product typeBook
Published inMay 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781849517584
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Julien Bayle
Julien Bayle
author image
Julien Bayle

Julien Bayle owns his Master Degree of biology & computer sciences in 2000. After several years in pure IT System Design, he founded Design the Media early 2010 in order to provide his own courses, training & tools for art fields. As a digital artist, he designed some huge new media art installations, like the permanent exhibition of La Maison des Cinématographies de la Méditerranée (Château de la Buzine) in Marseille, France, in 2011. He also works as a new media technology consultant for some private & public entities. As an A/V live performer, he plays his cold electronic music from New York to Marseille, where he actually lives. Arduino framework is one of his first electronic hardware studies early 2005 and he designed the famous protodeck controller with some opensource framework too. As an art & technology teacher, also certified by Ableton in 2010, he teaches a lot of courses about the digital audio workstation named Ableton Live, about the real-time graphical programming framework Max6 and also about Processing and Arduino. As a minimalist digital artist, he works at the crossroads of sound, visual and data. He explores relationships between sounds and visuals through his immersive A/V installations, his live performances and his released music. His work, often described as “complex, intrigating and relevant”, tries to break classical codes to bring audience a new vision of our world through his pure digital and real-time generated stimuli. He's deeply involved in the open source community and loves to share and provide workshops and masterclasses online and on-site too. His personal website is http://julienbayle.net.
Read more about Julien Bayle

Right arrow

Chapter 4. Improve Programming with Functions, Math, and Timing

As a digital artist, I need special conditions to be able to work. We all need our own environment and ambience to be productive. Even if each one of us has his/her own way, there are many things in common.

In this chapter, I want to give you elements that will make you more comfortable to write source code that is easily readable, reusable and, as much as possible, beautiful. Like Yin and Yang, for me there has always been a Zen-like quality to the artistic and coding sides of me. Here is where I can deliver some programming pearls of wisdom to bring peace of mind to your creative side.

We are going to learn something we have already used a bit before: functions. They contribute to improve both readability and efficiency at the same time. As we do that, we'll touch on some mathematics and trigonometry often used in many projects. We'll also talk about some approaches to calculation optimization, and we'll finish this chapter...

Introducing functions


A function is a piece of code defined by a name and that can be reused/executed from many different points in a C program. The name of a function has to be unique in a C program. It is also global, which means, as you already read for variables, it can be used everywhere in the C program containing the function declaration/definition in its scope (see the The scope concept section in Chapter 3, C Basics – Making You Stronger).

A function can require special elements to be passed to it; these are called arguments. A function can also produce and return results.

Structure of a function

A function is a block of code that has a header and a body. In standard C, a function's declaration and definition are made separately. The declaration of the function is specifically called the declaration of the prototype of the function and has to be done in the header file (see Chapter 2, First Contact with C).

Creating function prototypes using the Arduino IDE

The Arduino IDE makes our...

C standard mathematical functions and Arduino


As we have already seen, almost all standard C and C++ entities supported by the compiler avr-g++ should work with Arduino. This is also true for C mathematical functions.

This group of functions is a part of the (famous) C standard library. A lot of functions of this group are inherited in C++. There are some differences between C and C++ in the use of complex numbers. C++ doesn't provide complex numbers handling from that library but from its own C++ standard library by using the class template std::complex.

Almost all these functions are designed to work with and manipulate floating-point numbers. In standard C, this library is known as math.h (a filename), which we mention in the header of a C program, so that we can use its functions.

Trigonometric C functions in the Arduino core

We often need to make some trigonometric calculations, from determining distances an object has moved, to angular speed, and many other real-world properties. Sometimes...

Approaching calculation optimization


This section is an approach. It means it doesn't contain all the advanced tips and tricks for programming optimizations, but contains the optimizations on pure calculation.

Generally, we design an idea, code a program, and then optimize it. It works fine for huge programs. For smaller ones, we can optimize while coding.

Note

Normally, our firmware is small and so I'd suggest that you consider this as a new rule: Write each statement keeping optimization in mind.

I could add something else right now: Don't kill the readability of your code with too many cryptic optimization solutions; I thought of pointers while writing that. I'll add a few lines about them in order to make you familiar with, at least, the concept.

The power of the bit shift operation

If I consider an array to store things, I almost always choose the size as a power of two. Why? Because the compiler, instead of performing the array indexing by using a CPU-intensive multiply operation, can use...

Time measure


Time is always something interesting to measure and to deal with, especially in embedded software that is, obviously, our main purpose here. The Arduino core includes several time functions that I'm going to talk about right now.

There is also a nice library that is smartly named SimpleTimer Library and designed as a GNU LGPL 2.1 + library by Marcello Romani. This is a good library based on the millis() core function which means the maximum resolution is 1 ms. This will be more than enough for 99 percent of your future projects. Marcello even made a special version of the library for this book, based on micros().

The Arduino core library now also includes a native function that is able to have a resolution of 8 microseconds, which means you can measure time delta of 1/8,000,000 of a second; quite precise, isn't it?

I'll also describe a higher resolution library FlexiTimer2 in the last chapter of the book. It will provide a high-resolution, customizable timer.

Does the Arduino...

Summary


This completes the first part of this book. I hope you have been able to absorb and enjoy these first (huge) steps. If not, you may want to take the time to review something you may not have clarity on; it's always worth it to better understand what you're doing.

We know a bit more about C and C++ programming, at least enough to lead us safely through the next two parts. We can now understand the basic tasks of Arduino, we can upload our firmware, and we can test them with the basic wiring.

Now, we'll move a step further into a territory where things are more practical, and less theoretical. Prepare yourself to explore new physical worlds, where you can make things talk, and communicate with each other, where your computer will be able to respond to how you feel and react, and without wires sometimes! Again, you may want to take a little time to review something you might still be a little hazy on; knowledge is power.

The future is now!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C Programming for Arduino
Published in: May 2013Publisher: PacktISBN-13: 9781849517584
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Julien Bayle

Julien Bayle owns his Master Degree of biology & computer sciences in 2000. After several years in pure IT System Design, he founded Design the Media early 2010 in order to provide his own courses, training & tools for art fields. As a digital artist, he designed some huge new media art installations, like the permanent exhibition of La Maison des Cinématographies de la Méditerranée (Château de la Buzine) in Marseille, France, in 2011. He also works as a new media technology consultant for some private & public entities. As an A/V live performer, he plays his cold electronic music from New York to Marseille, where he actually lives. Arduino framework is one of his first electronic hardware studies early 2005 and he designed the famous protodeck controller with some opensource framework too. As an art & technology teacher, also certified by Ableton in 2010, he teaches a lot of courses about the digital audio workstation named Ableton Live, about the real-time graphical programming framework Max6 and also about Processing and Arduino. As a minimalist digital artist, he works at the crossroads of sound, visual and data. He explores relationships between sounds and visuals through his immersive A/V installations, his live performances and his released music. His work, often described as “complex, intrigating and relevant”, tries to break classical codes to bring audience a new vision of our world through his pure digital and real-time generated stimuli. He's deeply involved in the open source community and loves to share and provide workshops and masterclasses online and on-site too. His personal website is http://julienbayle.net.
Read more about Julien Bayle