Posts

Showing posts from June, 2021

Organize your Arduino code with header and class files

Image
Download Example Code Here Ok, so you made your awesome Arduino widget, but now you're looking at the code trying to find the spot where you made it do that cool thing, and you can't find it! There's just too much going on in the sketch file! Well my friend, it might be time for you to start organizing your code using classes and class files.  What are C++ classes? A class is basically a description of the variables and actions that something in your application might be interested in. For instance, if you were making a application involving vehicles, you might make a class that holds the speed the vehicle is currently traveling, and might have actions like drive and stop.   Something like this: class vehicle{     private:     int CurrentSpeed;     public:     void drive(int speed){          //increase the speed          CurrentSpeed = speed;      }     void stop(){          // decrease the speed          while(CurrentSpeed>0){          CurrentSpeed--;      } }; The clas

Using Abstract Classes with Arduino

Image
Download Code Example Here:   What is an 'abstract class'? An abstract class is an 'abstraction', or more simply put - it's a way to tell the computer what you want to do without telling it how you're going to do it. The first time I heard that, it made no sense to me. So, the way I think about it is that an abstract class is like a game cartridge. The game system knows what to do with the game cartridge when you put it the system, even though it has no idea what game you're playing. How does the game play without the underlying code running it knowing anything about the game code? Well, one way to do that is by using abstract classes in the system code. Why would I use one? Simply put, you might not ever use one. You can do a lot of really useful things with an Arduino and never use anything but the single file that you get when you start a new sketch. However, when things start getting complicated you might find the need to start organizing your code into