I wasn’t able to figure out the intricacies of defining a class on my own, so I enlisted the assistance of Martin Mlostek at work. I showed him what I’d coded thus far, and he immediately identified all the errors and also gave me some tips on writing better code. He taught me Hungarian Notation for my variables since the Arduino programming environment doesn’t feature many of the data-tracking features in full-blown IDEs.
After discussing the changes on Friday night, I headed home and started implementing his suggestions. BOOM! Within about 30 minutes, I had a working Knob class! My class reads the knobs, scales their values, and automatically displays their names and values on the attached LCD. With this class in place, all I have to do is write a simple line of code like this:
volume = knob0.getNewValue();
and the software will place the knob’s value in the “volume” variable so I can process volume changes. However, what happens behind the scenes is that, if the knob has changed value, the LCD is updated. I don’t have to write anything in my main routine to update the LCD–that’s totally hidden away now so I can just focus on the important data that I’m trying to wrangle. I can make as many Knobs as I have analog inputs on this Arduino simply reusing the same class. That’s super-dope.
The other benefit of the Knob class is that I can extend its behavior to other types of controls. For example, I should have a Button class that does the same thing as the Knob, but for a 2-state button. The cool thing is that I don’t have to write an entire Button class–I can simply base the Button class on the Knob class and merely detail the differences between the two. For example, the routine for writing the Button name and value to the LCD will be exactly the same as the Knob, but the scanning method for the Button will be different. So all I have to do is write the scanning method! This is all starting to make sense…
The ultimate trick, though, will be converting my class into a library. Right now, my class is defined at the beginning of my code so the code looks rather long. It would be nice to just move that into an external library so I can just include it at the start of my code, but splitting it into separate .h and .cpp files has proven problematic. Time to talk to Martin again!
Leave Your Response