Using Object-Oriented Programming in Arduino
Personally, I’m not very fond of Arduino because my programming experience with it hasn’t been particularly clean. But recently, I had another opportunity to use Arduino.
I wasn’t thrilled, but rather than just killing time, I figured I might as well learn something new, so I challenged myself to use object-oriented design. Arduino operates on C++, which looked somewhat different from Python or C#, making it a bit unfamiliar, but seeing it work properly was still satisfying. I hope this can serve as a reference if I ever need to use C++ later.
Example Code
#include "Arduino.h"
class MainFunctions
{
public:
AddFiveHundreadWon(int Money);
AddOneHundreadWon(int Money);
AddFiftyWon(int Money);
};
#include "Arduino.h"
#include "MainFunctions.h"
int MainFunctions::AddFiftyWon(int Money)
{
Money += 50;
return Money;
}
int MainFunctions::AddOneHundreadWon(int Money)
{
Money += 100;
return Money;
}
int MainFunctions::AddFiveHundreadWon(int Money)
{
Money += 500;
return Money;
}
void setup()
{
/* ... */
}
void loop()
{
/* ... */
Money = MainFunctions.AddFiveHundreadWon(Money);
Money = MainFunctions.AddOneHundreadWon(Money);
Money = MainFunctions.AddFiftyWon(Money);
/* ... */
}
This is an example of splitting simple money-adding functionality into individual files. The class declaration file MainFunctions.h declares methods, MainFunctions.cpp defines the declared methods, and Arduino_OOP.ino calls the defined methods.
It works well. Each time the functions are called, AddFiveHundreadWon() adds 500 won, AddOneHundreadWon() adds 100 won, and AddFiftyWon() adds 50 won.