1. Fundamentally, C language still exists, and I have been using it to write programs limited to 200–500 lines, targeted for low-flash-memory AVR microcontrollers such as the ATmega328P on the UNO R3 board.
2. C++ is approximately equal to "C + few more additions (the classes and others)". There are reasons why Bjarne Stroustrup thought of developing C++, which are beyond discussion in this post.
3. Using C, we write progam in "Procedural Method" in which the executing steps come one-after-another. In C, the variables/data are seperated from the functions which operate on data.
4. C++ allows creating sketches using the so called classes; where, the variables/data and the operating methods/functions remain together offering some kind of protection -- a feature that is not available in C.
5. A beginner always start with C. Once, he becomes proficient in C, he tries to convert his C-based programs/sketches into C++ to see the benefits of C++.
C++ motivates a programmer to treat hardware as real objects and then apply methods to modify their properties and behaviors. For example: you have a fully inflated balloon which will burst if you push a pin into it. Here:
"inflated ballonn" is an object,
"push a pin into it" is a method (C++ vocabulary)
"burst" is a eaction (output)
6. Example of a Procedural Program using C to blink onboard led-L of UNO R3 board.
#define ledL 13
void setup()
{
Serial.begin(9600);
pinMode(ledL, OUTPUT);
}
void loop()
{
digitalWrite(Built_inLed, HIGH);
delay(2000);
digitalWrite(Built_inLed, LOW);
delay(1000);
}
7. Let us convert the sketch of Step-6 using C++ Language constructs.
(1) List the variable(s)
- DPin number with which led-L is conneted
- Diection of DPin line
- On time of led-L
- Off time of led-L
(2) List the methods (functions)
- ioDirection()
- ledOn()
- ledOff()
- timeDelay()
8. Before writing a class based sketch for the Procedural sketch of Step-6, let us get familiar with various fields of a typpical "Class Structure" (Fig-1) that is created using class keyword.
Fgure-1:
9. Now let us see how I have creted below a simple "class structure" using class keyword. I have followed the template (Fig-1) of Step-8. (I always use three spaces for indentation.)
class SimpleLED
{
private:
byte DPin;
byte direction;
unsigned long onTime;
unsigned long offTime;
public:
SimpleLED(byte pin, byte dir, unsigned long onT, unsigned long offT);
void ioDirection();
void ledOn();
void ledOff();
void timeDelay(unsigned long delayTime);
};
SimpleLED ledL(13, OUTPUT, 2000, 1000); //object ledL is created
Note-1: Only member functions are allowed to access (performing read/write operations) on the private variables.
Note-2: Any function outside even the loop() function that is outside of class has no right to access any private variables of that class.
Note-3: Any function outside the class has the right o access the member functions.
Note-4: Member functions can access each other.
Note-5: Relationship among object, method, member operator, and function (Fig-2):

Figure-2:
Q: Note that the number of variables (4) agrees with Step-7 except that number of methods is 5. Why is there one more method (SimpleLed()) that has been added?
A: It is the rule of C++ that it adds one constructor() function with the same name as the "Class name" is. The constructor() function is automatically called upon for execution when an object is created as we will see in Step-10.
10. Now, we have to write defining codes for the methods and then integrate them with the setup() and loop() functions of Arduio Sketch.
(1) Object creation/declaration
SimpleLED ledL(13, OUTPUT, 2000, 1000); //object ledL is created
(2) Defning codes of SimpleLED() method
SimpleLED::SimpleLED(byte pin, byte dir, unsigned long onT, unsigned long offT)
{
DPin = pin;
direction = dir;
onTime = onT;
offTime = offT;
}
Note-1: The name of the construction() function is always be the name of the class name. the constructor function does not return any value and not even void.
Note-2: The value of the first argument (13) of the led1 object is passed to the pin parameter of the SimpleLED() constructor, and from there it is assigned to the private member variable DPin. The value 13 cannot be assigned directly to a private variable; it must be passed through a public method/member function (the constructor). The same applies to the other arguments: OUTPUT, 2000, and 1000 of the object.
(3) Defining codes for ioDirecton() method.
void SimpleLED::ioDirection() //:: says ioDirection() belongs to SimpleLed class
{
pinMode(DPin, direction); //pinMode(13, OUTPUT);
}
Note-1: The double colon (::) is called "Scope Resolution Operator." It syas to the compiler that the method to its right (ioDirection()) belongs to the class that is located at its left (SimpleLED).
(4) Defining codes for ledOn() method.
void SimpleLED::ledOn()
{
digitalWrite(DPin, HIGH);
}
(5) Defining codes for ledOff() method.
void SimpleLED::ledOff()
{
digitalWrite(DPin, LOW);
}
(6) Defining codes for timeDelay() method.
void SimpleLED::timeDelay(unsigned long delayTime)
{
delay(delayTime);
}
11. The Sketch
//-- place code to declare class structure
//-- place code to declare object
void setup()
{
ledL.ioDirection();
}
void loop()
{
ledL.ledOn()
ledL.timeDelay(argOn);//argOn must be evaluated to 2000 (onTime variable)
ledL.ledOff();
ledL.timeDelay(argOff);//argOff must be evaluated to 1000 (offTime variable)
}
//-- place code to define SimpleLED() method
//-- place code to defiine ioDirection() method
//-- place code to define ledOn() method
//-- place code to define ledOff() method
//--place code to define timeDelay() method
Note-1: The argOn argument must be evaluated to onTime (2000 ms). The onTime is a private variable. So, it must be accessed via a member function like unsigned long getOnTime(). This type of function is known as getter function.
Note-2: Similarly to assign the value of the offTime (1000) variable to argOff argument, the member function unsigned long getOffTime() is needed.
Note-3: As a result, the class structure of Step-9 will take the following form:
class SimpleLED
{
private:
byte DPin;
byte direction;
unsigned long onTime;
unsigned long offTime;
public:
SimpleLED(byte pin, byte dir, unsigned long onT, unsigned long offT);
void ioDirection();
void ledOn();
void ledOff();
void timeDelay(unsigned long delayTime);
unsigned long getOnTime(); //getter function
unsigned long getOffTime();
};
Note-4: Defining codes for getOnTime() method
unsigned long SimpleLED::getOnTime()
{
return onTime; //2000 ms goes to argOn variable of calling function
}
Note-5: Defining codes for getOffTime() method
unsigned long SimpleLED::getOffTime()
{
return offTime;
}
12. The final integrated sketch:
class SimpleLED
{
private:
byte DPin;
byte direction;
unsigned long onTime;
unsigned long offTime;
public:
SimpleLED(byte pin, byte dir, unsigned long onT, unsigned long offT);
void ioDirection();
void ledOn();
void ledOff();
void timeDelay(unsigned long delayTime);
unsigned long getOnTime(); //getter declaration
unsigned long getOffTime();
};
SimpleLED ledL(13, OUTPUT, 2000, 1000); // DPin 13, OUTPUT, 2000/1000ms ON/OFF
void setup()
{
ledL.ioDirection();
}
void loop()
{
ledL.ledOn();
unsigned long argOn = ledL.getOnTime();
ledL.timeDelay(argOn);//(ledL.getOnTime());
ledL.ledOff();
unsigned long argOff = ledL.getOffTime();
ledL.timeDelay(argOff);//(ledL.getOffTime());
}
SimpleLED::SimpleLED(byte pin, byte dir, unsigned long onT, unsigned long offT)
{
DPin = pin;
direction = dir;
onTime = onT;
offTime = offT;
}
void SimpleLED::ioDirection()
{
pinMode(DPin, direction);
}
void SimpleLED::ledOn()
{
digitalWrite(DPin, HIGH);
}
void SimpleLED::ledOff()
{
digitalWrite(DPin, LOW);
}
void SimpleLED::timeDelay(unsigned long delayTime)
{
delay(delayTime);
}
unsigned long SimpleLED::getOnTime()
{
return onTime;
}
unsigned long SimpleLED::getOffTime()
{
return offTime;
}
13. Upload Sketch of Step-12 into Arduino UNO R3 and check that the led_L is bicking with 2 sec onTime and 1 sec offTime.
14. Exercise
Rewrite Sketch of Step-12 to get onTIme/offTime form the Serial Monitor and then pass them to the class.