Attempting to create a class (no errors on compile but no response from Arduino)

Hello,

I’ve been struggling for the past couple of days with my Arduino code. I created a class/library to manage columns (which in turn uses the LPD8806.h, SPI.h, and LPD8806.cpp), and it compiles with no errors.

#ifndef Column_h
#define Column_h

#include "Arduino.h"
#include "LPD8806.h"

class Column
{
	public:
		Column(int dataPin, int clockPin, int hoverSensorPin, int dropSensorPin);
	private:
		LPD8806 _strip;
		int _freeSpot, _hoverSensorPin, _dropSensorPin, _led;
		byte _hoverState, _dropState;
		uint32_t _red, _yellow, _green;
		bool _startHover, _hover;
		unsigned long _timeHoverStart;
};

#endif
#include "Arduino.h"
#include "Column.h"

Column::Column(int dataPin, int clockPin, int hoverSensorPin, int dropSensorPin) {
	_strip = LPD8806(6, dataPin, clockPin);
	_strip.begin();
	_strip.show();
	
	_red = _strip.Color(127, 0, 0);
        _yellow = _strip.Color(127, 127, 0);
        _green = _strip.Color(0, 127, 0);
	
	_hoverSensorPin = hoverSensorPin;
        pinMode(hoverSensorPin, INPUT);
	_hoverState = LOW;
    
        _dropSensorPin = dropSensorPin;
        pinMode(dropSensorPin, INPUT);
	_dropState = LOW;
	
	_startHover = false;
	_hover = false;
	_timeHoverStart = 0;
	
	_led = 0;
}

And my simple Arduino code:

#include <Column.h>
#include "SPI.h"

Column column1(22, 23, 52, 53);

void setup() {
  Serial.begin(9600);
  
  while (true) {Serial.write('A'); delay(1000);};
}

void loop() {}

The code compiles perfectly, but I never get to run setup(). Only by commenting out column1 can I get Serial messages. Any ideas on what I’m doing wrong? My libraries/Column contains: Column.cpp, Column.h, LPD8806.cpp, LPD8806.h.

The code compiles perfectly, but I never get to run setup(). Only by commenting out column1 can I get Serial messages. Any ideas on what I'm doing wrong?

Yes. You think you know when your constructors get called. You don't. You are doing far too much in the constructors.

PaulS:

The code compiles perfectly, but I never get to run setup(). Only by commenting out column1 can I get Serial messages. Any ideas on what I'm doing wrong?

Yes. You think you know when your constructors get called. You don't. You are doing far too much in the constructors.

Can you be more specific? I don't understand what I'm doing wrong in the Column constructor.

Can you be more specific? I don't understand what I'm doing wrong the Column constructor.

    _strip = LPD8806(6, dataPin, clockPin);
    _strip.begin();
    _strip.show();

This presumes that the appropriate pins are ready to be used to send data to the strip. Since constructors are called before the init() method that sets up the hardware, that is not a valid assumption.

You need to add a begin method to your class, and call that method in setup() (like Serial.begin()). Move the hardware stuff to the begin method.

Worked like a charm :) Thank you so much.