Object-oriented blinking with Arduino

Yesterday I started programming again with Arduino.

First thing I setup the Eclipse environment and to test it I ran the very simple blink program.


void setup() {
 pinMode(13, OUTPUT);
 }

void loop() {
 digitalWrite(13, HIGH);
 delay(100);
 digitalWrite(13, LOW);
 delay(100);
 }

Then, I wanted to go OO, so I wrote a class to represent a blinking object!

Header:

class Blink {
 private:
 unsigned long _onInterval;
 unsigned long _offInterval;
 uint8_t _blinkingLedId;
 bool _ledActivity;

public:

Blink(unsigned long onInterval, unsigned long offInterval, uint8_t blinkingLedId);

void oneBlink();

void setLedActivity(bool ledActivity);
 };

Implementation:

Blink::Blink(unsigned long onInterval, unsigned long offInterval, uint8_t blinkingLedId) :
 _onInterval(onInterval), _offInterval(offInterval), _blinkingLedId(blinkingLedId)
 {
 _ledActivity = true;
 pinMode(_blinkingLedId, OUTPUT);
 }

void Blink::oneBlink()
 {
 // LED activity disabled?
 if (_ledActivity == false)
 return;

// ON
 digitalWrite(_blinkingLedId, HIGH);
 delay(_onInterval);

// OFF
 digitalWrite(_blinkingLedId, LOW);
 delay(_offInterval);
 }

void Blink::setLedActivity(bool ledActivity) {
 _ledActivity = ledActivity;
 }

Main:

Blink *b;

void setup() {
 b = new Blink(2000, 2000, 13);
 }

void loop() {
 b->oneBlink();
 }

Well, okay, OOP doesn’t add anything to the story here, but how about having a blinking object instead of a procedural set of blinking statements :D? Exciting!