C++ in arduino : Classes and objects

Hello in this post I want to share how we can implement OOP concepts in Arduino
OOP  means object oriented programming

In OOP Class is the blueprint of an object, in UML class is represented by a rectangle



Object : 
  • The object is an instance of a Class 
  • Both data and functions that operate on data are bundled as a unit & it is called an object 
  • You can create one or more instances of a Class 
OOP designing help solve the problem in a modular fashion and the code becomes very structured, readable & maintainable 

Making a class in Arduino

.cpp & .h are the 2 file  formats of a c++ class and you only need these 2 files to make a class  

.h contains : 
  • any import statement that the class needs to make
  • name of the class & anything that the class extends 
  • Declaration of variables
  • Declaration of methods 
.cpp contains : 
  • The definition of the methods
Let us create a class Ball in Arduino using the following steps 
  1. Open Arduino IDE 
  2. Save the sketch somewhere 
  3. Click the rightmost arrow and press new tab 
  4. Type Ball.h and press enter
  5. Ball.h header file is created 

Now lets think what does a ball can do ?  ............
well it can bounce :) so lets declare a method named bounce() in the .h file


#ifndef _BALL_H      //tells compiler to compile this class only once 
#define _BALL_H

#include <Arduino.h>

class Ball
{
   public:
          Ball(byte _pin, unsigned long int _bounceTime);
          void bounce();
   private:
          unsigned long int bounceTime;
          byte pin;
};
#endif

here the public function Ball() is the constructor . note you can pass argument to the constructor also

but what can we put in the bounce() ? ...
lets Blink the LED at pin 13 so that it would look like bounce :)

now lets make the implementation file to do this like the header file press newtab and create the Ball.cpp  file

#include "ball.h"

Ball::Ball(byte _pin, unsigned long int _bounceTime)
{
  bounceTime=_bounceTime;
  pin = _pin;
  pinMode(pin,OUTPUT);
}

void Ball::bounce()
{
  digitalWrite(pin,HIGH);
  delay(bounceTime);
  digitalWrite(pin,LOW);
  delay(bounceTime);
}

now your blueprint i.e. Class is ready now you can create an instance of it and play with it . now select the sketch from the tabs & copy paste the following code
/*
 * this example shows how to make Class and create object 
 * author : Hassin Ayaz 
 */

#include "ball.h"

Ball b(13,1000);

void setup() 
{
  Serial.begin(9600);
  Serial.println("Ball object created");
}

void loop() 
{
 Serial.println("Ball will bounce i.e Led will Blink");
 b.bounce();
}

so here an object of Ball type is created (note: object b is a global variable)

verify it and upload it
you will see the LED at pin 13 will blink :)

All the sources are available in this link https://github.com/hassin23ayz/ArduinoCpp/tree/master/T13_obj_classes 

I hope you have enjoyed this post
Welcome to the world of OOP :)

2 comments:

  1. All can be on the sketch without adding tabs. Copying and pasting your code for the sketch and both header files I receive the error on the Ball.cpp tab: 'Ball' does not name a type

    ReplyDelete
    Replies
    1. Hello Sorry for the late reply .. here is the code link https://github.com/hassin23ayz/ArduinoCpp/tree/master/T13_obj_classes

      Delete

Categories

Pages

Firmware Engineer

My photo
Works on Firmware, Embedded Linux, Smart Metering, RTOS, IoT backend

Contact Form

Name

Email *

Message *

Copyrighted by Hassin. Powered by Blogger.