Arduino C++ Class Example

4 min read Jun 28, 2024
Arduino C++ Class Example

Arduino C++ Class Example

This article will demonstrate how to use classes in Arduino C++ to create organized and reusable code. We'll explore the basic concepts of object-oriented programming (OOP) and apply them to a practical example.

Understanding Classes

Classes are blueprints for creating objects. They define the data (attributes) and behavior (methods) of a specific type of object.

Key Concepts:

  • Class Definition: This defines the structure of the class, including its attributes and methods.
  • Object: An instance of a class. It has its own unique values for the attributes defined in the class.
  • Attributes: Variables that store data about the object.
  • Methods: Functions that define the object's behavior and actions.

Example: A Simple LED Control Class

Let's create a class called LEDController that controls an LED connected to a specific pin on the Arduino.

class LEDController {
public:
  LEDController(int pin) {
    _pin = pin;
    pinMode(_pin, OUTPUT);
  }

  void turnOn() {
    digitalWrite(_pin, HIGH);
  }

  void turnOff() {
    digitalWrite(_pin, LOW);
  }

private:
  int _pin;
};

Explanation:

  1. Class Definition: We define a class called LEDController.
  2. Constructor: The LEDController(int pin) function is the constructor. It's called automatically when a new LEDController object is created, setting the _pin attribute to the specified pin number and initializing it as an output.
  3. Methods: We define two methods: turnOn() and turnOff(), which control the LED state by setting the digital pin HIGH or LOW.
  4. Private Attribute: _pin is declared as private. This means it can only be accessed within the class, making the pin value encapsulated and protected.

Using the Class

Now, let's create an object of the LEDController class and use its methods:

#define LED_PIN 13

void setup() {
  LEDController led(LED_PIN); 
}

void loop() {
  led.turnOn(); 
  delay(500);
  led.turnOff();
  delay(500); 
}

Explanation:

  1. Object Creation: We create an object named led of type LEDController, passing the LED_PIN value to its constructor. This initializes the object and associates it with the specified pin.
  2. Method Calls: We call the turnOn() and turnOff() methods of the led object to control the LED's state.

Benefits of Using Classes

  • Code Organization: Classes encapsulate related data and behavior, making your code more structured and readable.
  • Reusability: You can create multiple objects of the same class, representing different instances of the same type. This promotes code reuse and reduces redundancy.
  • Maintainability: Changes to a class only need to be made in one place, improving maintainability.

Conclusion

This example illustrates the fundamental concept of classes in Arduino C++ and how they can improve your programming style. By using classes, you can write cleaner, more organized, and reusable code, ultimately making your projects more efficient and maintainable.