title title title title

IDE Coding Tips

Use of Libraries


Arduino.h

For the IDE interface to make sense, once you've been told that this development aid is in C++ syntax, it's useful to realize that the Arduino.h does a lot of heavy lifting in the background , and what you see (blank) on the IDE screen when you first turn it on is really C++. All C++ programs start with a main() function. Arduino.h has hidden that from view along with providing all the syntax for the statements that manipulate the I/O pins on the Arduino microcontroller boards, i.e., pinMode, digitalWrite, analogRead, etc. Wrapped up inside Arduino.h is the Series class not to mention all the HTML code to create a monitor screen and the interface to it, to display and provide a human interface to get data into and out of your program. The secret is revealed when you see the dot after Series, as in Series.begin(9600). That's real C++. It says "use the begin() method of the Series class and provide it a property value of 9600.

Along with the Series class there is the Wire class, the SPI class, the SD class and a few others - why are they fundamental to the Arduino boards? This is the main point of this discussion. Those classes provide the explanation of the multiplexing of the board's pins. Now it should make sense that this is how the I2Cand SPI busses are activated for certain particular pins. These class libraries adjust the hardware to provide this multiplex capability. So libraries are the secret. All the third-party libraries follow suit. They provide all this power to this little board and all its cousins.

To grasp a feeling of what's going on behind the scenes, just think of all the manipulation going on with menu bar under Tools -> board. All those different controller board types have to be dealt with in the Arcuino.h class to properly set up the IDE so the code is compiled correctly. The compiler itself is a wonderful thing and nicely done. If you had to set up all this code for each board type and pin configuration according to the multiplexed functionality of the pins, you would never be able to do it as a hobbyist, but realize that is the typical job of the professional C++ programmer. They have to account for every bit and register and clock function of the hardware and pull it all together.

So where do we go from here? I say all of this to get you to realize that the power is in the classes. Check out some of the third-party libraries by opening up their .h and .cpp files and look at them. I would recommend NotePad++ app so you can retain the color coding of the statements for easy reading. Some are easy, some are complex, but pull them apart. Print them out and get a stack of colored pencils and draw arrows connecting the parts. Figure out how they did it. Then get ready to write your own class structures. Here's the good news. You are an individual, not a major engineering firm, so you don't have to include all the formality that the professional programmer has to endure. Cut to the chase and write classes that do what you want. You don't have to create vast hierarchal classes that are mostly a massive pile of bureaucracy. To demonstrate what I mean, I will look at a couple of classes written for programming the Arduino Uno to beep-out Morse Code. The first class is simple and the second more complex, but well within your grasp as a beginner. Once you see what can be done, you will never look at project code the same way again, because the classes organize and provide a lot of functionality to your sketches.

So how do you start this process? Once you have NotePad++ on your computer (or Wrangler for MACs), go to MyComputer -> C-drive -> Program Files -> Arduino -> Libraries and look for the library you want to sneak a peek at. The folder will probably contain a folder of example sketches, the .h, the .cpp, and a small file called Keywords. That little file tells the IDE the color to make the keywords from the class so they look good on the screen. Next, just "open with" right click and choose NotePad++. Once the code is there, go up to Language and find C++ under the "C" listing. Print it out and then close it so you don't accidently modify anything. That's it. You didn't disturb your library at all. You can close out of MyComputer. If you want to get into it deeper, make a copy using "save as." Once you understand what the code is doing, many of these classes are set up to handle older IDE versions, boards you will never use, or have features that don't apply to what you're doing. So you can pair down the code and get it a try. It's best to do one thing at a time and test before trying anything else - like remove the stuff that doesn't apply to you. Once the code is easier to see, you might even add a feature that wasn't originally there. That's the beauty of open source code, but remember to credit the source and change the name so you won't currupt the original class.

Morse.h

This class was released to the public by David A. Mellis back in 2007. Thank you David. Here's what you'll find:

    #ifndef Morse_h
    #define Morse_h

    #include "Arduino.h"

    class Morse
    {
    public:
      Morse(int pin);
      void dot();
      void dash();
    private:
      int _pin;
    };

    #endif
    

The first two lines and the last line are some of that bureaucracy from C++, but needs to be there. It basically says, "If this code has already been opened, then don't do it again. If not, open it." Notice the Arduino.h shows up for the first time, which has to be there in any new class you add to the system. That ties in all the good stuff. The class its self has a constructor, two methods, and one variable property. I will assume you have some knowledge of C++. If not, go read a primer on the subject while we walk through this. The constructor is there so we can create an instance of Morse in our sketch. Classes are like rubber stamps. In our sketch, we just stamp out a new copy for our use in the sketch. The class is a blueprint of what the new stamped out copy does. We pass it the pin number of the output pin we want to use to beep a speaker or light an LED. Then there is a method for making a dot or a dash. Lets looks at a typical sketch using this class:

    #include <Morse>

    Morse morse(13);

    void setup()
    {
    }

    void loop()
    {
    morse.dot(); morse.dot(); morse.dot();
    morse.dash(); morse.dash(); morse.dash();
    morse.dot(); morse.dot(); morse.dot();
    delay(3000);
    }
    

Notice we don't even need a setup(). The first line tells the compiler to grab the Morse Class and bring it to the party. The next line uses the constructor to create an instance of Morse in our sketch. The loop() uses the Morse methods of dot and dash to send out SOS and wait 3 seconds and do it again.

Now let's look into the .cpp file and see how the class did this:

    #include "Arduino.h"
    #include "Morse.h"

    Morse::Morse(int pin)// standard C++ to create a class
    {
     pinMode(pin, OUTPUT);// notice our statements from 
                          // the Arduino.h class
    _pin = pin;
    }

    void Morse::dot()
   {
    digitalWrite(_pin, HIGH);
    delay(250);
    digitalWrite(_pin, LOW);
    delay(250);  
   }

   void Morse::dash()
  {
    digitalWrite(_pin, HIGH);
    delay(1000);
    digitalWrite(_pin, LOW);
    delay(250);
  }
    

If you know C++, this will all look right to you. The class type of Morse is created with a parameter that needs to be supplied. Notice how the Arduino class is used to set the pin as an OUTPUT. Then the argument is assigned to the property _pin. I'm not fond of this underscore convention for a property, but it does offset it as special. Then notice the two additional methods used by the Arduino class to turn on and off the output pin for specific amounts of time. A dot is 250ms, spacing is 250ms and a dash is four times as long.

Here's a place we could manipulate this class. We could vary the timing, speed up the dots and dashes, etc. There's not much else we could do here, but you get the idea you could modify this class.

Telegraph.h

This library has a checkered past if you try to find it on the Internet, but I used it and probably modified it to run correctly and now forget what I did. Since there is much to say about how to dissect it, I will refer to a set of slides that go through the whole class and a sketch that uses it, which can be found here.


Return to: Electronics Page

bottom strip