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!

Advertisements

3 thoughts on “Object-oriented blinking with Arduino

  1. Thanks for this – finding basic info on Arduino Object Syntax is hard. I’m still learning (which is good).

    Here is a suggested change – problem with your blink class is that it can’t be used for anything else as it waits for the delay statements – add this to your code and you can blink LEDs all day while still doing some other work. just call b->BlinkLed

    In Header
    private:
    unsigned long cycleTime;

    void BlinkLed();

    in Implementation
    void BlinkLed( )
    // LED is now on Analog – ran out of pins – LCD uses a lot – Digital Pins are just digitalWrite(….
    {
    if ( ( millis() – cycleTime ) > _OffInterval ) {
    analogWrite( _blinkingLedId, 255 );
    }
    if ( ( millis() – cycleTime ) > ( _OffInterval + _OnInterval ) ) {
    analogWrite( _blinkingLedId, 0 );
    cycleTime = millis();
    }
    }

  2. Thank you vincepii and Tony (for the improvement).

    Vincepii, please add some more OOP examples for the Arduino, it was really helpful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s