PCA9655E I/O expander configuration in global name space?

In the following two sketches, a PCA9655E I/O expander drives a LED.
PCA9655E_LED1_bb.png

The first sketch works as intended. The LED is on for .9 seconds:

#include <Wire.h>

class LED
{
    public:
        LED()
        {
            Wire.begin();
        }

        void flash()
        {
            Wire.beginTransmission(0x20);
            Wire.write(6);              //configure as output
            Wire.write(0);              //all the pins
            Wire.endTransmission();

            Wire.beginTransmission(0x20);
            Wire.write(2);              //Output Port 0
            Wire.write(~0);             //set pins high
            Wire.endTransmission();

            delay(900);                 //LED is on for .9 seconds

            Wire.beginTransmission(0x20);
            Wire.write(2);              //Output Port 0
            Wire.write(0);              //set pins low
            Wire.endTransmission();
        }
};

LED led;                                //global

void setup() { }

void loop()
{
    led.flash();                        //config in loop

    while(true) { } 
}

The second sketch is similar, but with configuration occurring in global name space. The LED never comes on. Why is that?

#include <Wire.h>

class LED
{
    public:
        LED()
        {
            Wire.begin();

            //why does configuration in global space break the sketch?
            Wire.beginTransmission(0x20);
            Wire.write(6);              //configure as output
            Wire.write(0);              //all the pins
            Wire.endTransmission();
        }

        void flash()
        {
            Wire.beginTransmission(0x20);
            Wire.write(6);              //configure as output
            Wire.write(0);              //all the pins
            Wire.endTransmission();

            Wire.beginTransmission(0x20);
            Wire.write(2);              //Output Port 0
            Wire.write(~0);             //set pins high
            Wire.endTransmission();

            delay(900);                 //LED never comes on

            Wire.beginTransmission(0x20);
            Wire.write(2);              //Output Port 0
            Wire.write(0);              //set pins low
            Wire.endTransmission();
        }
};

LED led;                                //config in global space

void setup() { }

void loop()
{
    //LED led;                          //LED on for .9 seconds if object is instantiated in loop
    led.flash();                        //loop

    while(true) { } 
}

Why does running configuration in global name space break the sketch?

I am using Arudino 1.0.6 and Teensyduino 1.20.
The ICs are Teensy 2.0 and PCA9655E I/O expander.
PCA9655E data sheet is on http://www.onsemi.com/pub_link/Collateral/PCA9655E-D.PDF

Thank you.

It is because that constructor gets called when the object is created, which may be before init is called of before Wire is created.

It is never good to have constructors try to do anything other than set up variables or assign values. Calls to code that use hardware may be being executed before the hardware is ready.

A simple solution would be to have an LED.begin() method that handles the set up and you can call that from the setup function when you know everything is ready.

Thank you for the quick response Delta_G :slight_smile: