[SOLVED] Intel Edison pinMode Constructor not working

First post alert....I'm attempting to write a library that can be used on an Uno or an Intel Edison. For the purposes of this post I have simplified it but basically the class constructor is not behaving. When uploaded to an Uno the main sketch runs fine (led blinks), on an edison it uploads but the led is constantly on (nothing happens). If anyone can shed some light on why it isn't working it would be greatly appreciated.

Main

#include "blinkClass.h"

blinkClass bClass(13);

void setup()
{
}

void loop()
{
  bClass.on(); 
}

blinkClass.cpp

#include "Arduino.h"
#include "blinkClass.h"

blinkClass::blinkClass(int pin)
{
  pinMode(pin, OUTPUT);
  _pin = pin;
}

void blinkClass::on()
{
  digitalWrite(_pin, HIGH);
  delay(250);
  digitalWrite(_pin, LOW);
  delay(250);
}

blinkClass.h

#ifndef blinkClass_h
#define blinkClass_h

#include "Arduino.h"

class blinkClass
{
  public:
    blinkClass(int pin);
    void on();
  private:
    int _pin;
};

#endif

Your class constructor can run before the init() function that sets up the hardware does. You shouldn't have hardware related stuff in your constructor for that reason. That's why classes like HardwareSerial or Wire or many others have a begin() method. You don't have to use that name, but you do need a function that can be called from setup to handle the hardware stuff. All the constructor should have is variable initializations and stuff like that.

Can you suggest a .begin() method that would for any number of objects created? So if I do this:

blinkClass bClass1(13);
blinkClass bClass2(12);
blinkClass bClass3(11);

All thats needed is single .begin() instead of

void setup{
   bClass1.begin();
   bClass2.begin();
   bClass3.begin();
}

You should really just call it on each object.

But if you really really want to you could have a array of pointers to your object type as a static member of your class and have the constructor store a pointer to each object being constructed as it is constructed. Then you have a static member function (begin()) that rolls through that array calling the begin method for each one. Or if all you have to do is the pinMode and nothing else I guess you could make it a static member array of pin numbers and have the static begin method call pinMode on each one. Of course you would also need some way to track how many there are so that's another static member and another line in the constructor to add one to it. It's hacky and IMHO code bloat in this case but it can be done.

However, if you didn't really understand everything I just said and see pretty much right off how to do it then you should probably just call begin on each one in setup. This kind of stuff can get complicated quick.

The better method would be to make these an array. Anytime you catch yourself putting sequential numbers on variable names then what you really want is an array. Make those numbers useful.

blinkClass bClass1(13);
blinkClass bClass2(12);
blinkClass bClass3(11);

To:

blinkClass bClass[3] = {blinkClass(13), blinkClass(12), blinkClass(11)};

Then in setup you can roll through them in a for loop:

for (int i = 0; i < 3; i++){
    bClass[i].begin();
}

Delta_G:

blinkClass bClass[3] = {blinkClass(13), blinkClass(12), blinkClass(11)};

Then in setup you can roll through them in a for loop:

for (int i = 0; i < 3; i++){

bClass[i].begin();
}

Since Arduino now uses the C++11 standard, there’s a nicer way to do that:

for( auto element : bClass )
{
  element.begin();
}

Didn't know we got that. Cool to know. What version did it start?

Don’t know. At least about 1.7-ish.

C++11 has a couple of cool things to help with variable typing, like auto which automatically selects the type based on the initialization statement, or decltype() which can evaluate the type of a variable, function return, or expression.

Don’t want to remember what kind of variable you need for millis()?

auto t_now = millis();

Or

decltype(millis()) t_now;

It’s like magic.

I guess they'll just eventually merge all the C# stuff into C++.

C++11 has been the default since IDE 1.6.6 (AVR 1.6.9, SAM 1.6.5).