FAQ up-arrow faq Arduino page
Dual arduinos in control panel hdr

Object oriented Arduino programming

Index:


I got introduced to Arduino programming by Dr. Bunza in 2014. With the price of Arduino Pro-Mini boards being extremely low, I concluded why should I use hardwared electonics for many projects around my train room?

After figuring out that the gnu C++ is the compiler used by the Arduino IDE (there are some custom Arduino extensions), I began experimenting to see whether I could trick the IDE into supporting Object Oriented Programming (OOP) and classes. The answer was yes! I then started creating classes to perform common functions: operating LEDs, measuring time, connecting push buttons, making a speaker beep, and others.

It occurred to me, perhaps some of these objects might be useful to others, so I created this web page to share them and started an Object Oriented Arduino programming thread on the MRH forum.

This site is NOT for absolute Arduino beginners. If you know how to write basic Arduino programs and upload them to your Arduino hardware, you may find these examples and classes useful. Some knowledge of C/C++ programming will also be helpful. Anyway, I hope you'll find this page and my classes useful.

Charlie

Examples


Classes

The following classes are available for download and installation.

  • CLASS: bcsjArcWelder - configures an LED to simulate an arc welder Nov 11, 2016
  • CLASS: bcsjBeep - generates beeps with a speaker attached to an Arduino pin Nov 15, 2016
  • CLASS: bcsjEncoder - detects clockwise or counterclockwise movement of encoder's knob Nov 16, 2016
  • CLASS: bcsjLED - controls an LED including auto-pulsing Nov 15, 2016
  • CLASS: bcsjPushButton - monitors the state of a pushbutton Nov 15, 2016
  • CLASS: bcsjTimer - microsecond precision software timing object Nov 11, 2016
  • CLASS: bcsjVarLED - pulse width modulates the intensity of a LED Nov 11, 2016

NOTE: Naming convention suggestion - I prepend all my externally visible class names with "bcsj" to make it much less likely there would be a name collision with another class. For example: how many classes named "Timer" do you suppose are out there? Even if you don't directly use the foreign "Timer" class, what if another class you are using - perhaps a server or loconet handler class - uses the Timer class, presto, namespace collisions.

Namespace collisions can be hard to find, and a pain to deal with once the problem is found. You don't have to use "bcsj" to prepend your names, but I strongly suggest using something unique to you or your project to avoid namespace conflicts. This applies to all globally named items such as class names or the names of #define macros from a class or external globals (global items declared in a class file). Things like member function names are accessed relative to a class or class instance name and are thus immune to this problem.


Installing class files in your Arduino library

NOTE: I'm using version 1.6.11 of the Arduino IDE. If you are using an earier version, you might need an upgrade.

You'll need to copy the header and C++ code files (.h and .cpp) for each class into your arduino installation directory. Usually this will be

C:\Program Files (x86)\arduino\libraries

You will likely need admin priveledges to write to this directory. If you performed a custom Arduino IDE installation you'll need to find the ...\arduino\libraries folder on your computer.

Each class you install in your Aruduino environment requires its own folder under the ...\arduino\libraries. Each folder must have the SAME root name as the .h and .ccp files for that class, which in turn must have the SAME root name as the class. Names are case sensitive. bcsjTimer is not the same as BcsjTimer.

For example, once installed, the arc welder class should have the following files/folders:

Class name: bcsjArcWelder
C:\Program Files (x86)\arduino\libraries\bcsjArcWelder - class folder in the Arduino installation
C:\Program Files (x86)\arduino\libraries\bcsjArcWelder\bcsjArcWelder.cpp - Class body : C++ source
C:\Program Files (x86)\arduino\libraries\bcsjArcWelder\bcsjArcWelder.h - Class header : C++ header file

So what's with the 'bcsj' in the class, folder, and file names? Two reasons:

  1. The Arduino environment is flat. That is, the classes are on the same level. Prepending the names with 'bcsj' helps avoid name collisions with other classes.
  2. I'm sharing object sourxce code that I've used when programing the Arduino Pro-Mini's on my Bear Creek & South Jackson railroad.

To install a class:

  1. Create a folder with the appropriate name in your ...\arduino\libraries folder
  2. Copy the .h and .cpp (class header and class body files to the folder created in step 1.
  3. Add an #include <library-name.h> directive at the beginning of the Arduino sketches that will use this class.
  4. Repeat for each class to be installed.
  5. Test your installed with a trial compilation

NOTE: Renaming classes is possible but not recommended. Should you choose to do so, you must track down EVERY place your class is referenced, and change that also as well as change the file and folder naming.


Licenses and valid uses of this code

There are NO restrictions. You are free to use, modify, redistribute, etc. any or all of these classes or examples for commercial or non-commercial purposes.

I make NO guaranttees, express or implied, as to the quality, lack of bugs, usefullness, or fitness for any purpose of these classes and examples. If you download and use 'em, you assume all risks. In NO case will I be liable for any consquential damages arising from their use.

If you ask and I have both time and energy, I may, at my sole descretion provide support.


C/C++ hints and tips

A few things you may or may not find helpful as you work to navigate the wilderness of programmming Arduino processors in C or C++.


Arduino programming reference links



control panel with dual arduino pro minis

Backside of the control panel at South Jackson yard for operating Toh Junction. The second Pro Mini is hooked to three optical occupancy detectors on the South Jackson switch lead which extends through the backdrop. When the switch engine is fou cars from the end of track it beeps. Two cars and it beeps twice. One car and it beeps three times. The Arduino drives the speaker directly (not external drivers) through a capacitor to filter out DC. With a price of less than $4 Pro Mini's have popped up all over the place on the BC & SJ.