Problem with Scope of Variables in Libraries

I’ve had some good luck starting out playing with this, but my luck has seemed to run out. I have been creating libraries for some projects I’m working on and it has gone well so far. Now I want to pass some arrays. I had some problems with them and you guys/girls gave me a hand.

Now I think my problem lies in scope but I’m just not sure. I declare an LEDControl object I call ledControl before the startup function. It has an array which I pass a pointer to my constructor and try to put those intergers into a private array. When I print out those in my method called ledUpdate I get other variables for the first two spots in the array, then the rest are just fine. So I’m sure I just don’t quite understand what the heck I’m pointing for some reason.

Here is the “Sketch”

#include <LEDControl.h>

  int ledCount = 6;
  int ledPins[] = 
  {
  11, 12, 13, 14, 15, 16  //Pins that LED's are connected
  };

  unsigned long fastFlash = 250; // fast flashing blinks every quarter second
  unsigned long slowFlash = 1000; // slow flashing blinks every second.

  LEDControl ledControl(ledCount,ledPins,fastFlash,slowFlash);  //Create an LEDControl
  
void setup() 
{
  Serial.begin(9600);  //Start Serial   
  Serial.println("<Arduino is ready>");

}

void loop() 
{
  Serial.print("ledPins[0] is: ");
  Serial.println(ledPins[0]);
  ledControl.ledUpdate();
}

The .cpp file

#include "Arduino.h"
#include "LEDControl.h"

LEDControl::LEDControl(int ledCount, int *ledPins, int fastFlash, int slowFlash)
{
	_ledCount = ledCount;  //The number of led's to control
		
	for (int thisLed = 0; thisLed < ledCount; thisLed++) //Loop over the _ledPin Array and set them all to OUTPUT
	{
		_ledPins[thisLed] = *ledPins;  //The pins the led's are connected to.
		ledPins++;
	}
	_fastFlash = fastFlash; //It is the time the led uses for fast flashing blinks
	_slowFlash = slowFlash; // It is the time the led uses for slow flashing blinks
}

void LEDControl::ledUpdate()
{
	Serial.print("|");
	for(int i=0; i<_ledCount; i++)
	{
		Serial.print(i);
		Serial.print(",");
		Serial.print(_ledPins[i]);
		Serial.print(",");
	}
	Serial.println("|");
}

My .h file

#ifndef LEDControl_h
#define LEDControl_h

#include "Arduino.h"

class LEDControl
{
	public:
		LEDControl(int ledCount, int *ledPins, int fastFlash, int slowFlash);
		void ledUpdate();
	private:
		int _ledCount;  //The number of led's to control
		int _ledPins[];  //The pins the led's are connected to.
		int _fastFlash;  // fast flashing blinks every quarter second
		int _slowFlash;  // slow flashing blinks every second.
};

#endif

and here is the crazy stuff that comes back through the Serial:

ledPins[0] is: 11 |0,250,1,1000,2,13,3,14,4,15,5,16,|

ledPins from the loop is correct.
But _ledPins 0 and 1 are the values for the variables fastFlash and slowFlash then 2 through 5 are correct. What the heck gives??? (I figure it’s just happen stance that they are correct)

Thanks for your previous help and thanks for the help I’m hoping to get!!!

int _ledPins[];  //The pins the led's are connected to.

You have to give this array a size if you’re going to fill it in that for loop. Or just keep the pointer and work with the array that is already there in the .ino file. This creates an array with a size of zero, basically just a pointer.

When you do this.

for (int thisLed = 0; thisLed < ledCount; thisLed++) //Loop over the _ledPin Array and set them all to OUTPUT
	{
		_ledPins[thisLed] = *ledPins;  //The pins the led's are connected to.
		ledPins++;
	}

You’re writing over memory that you don’t own.

After reading Delta-G's response I understood what I was doing wrong (thank you Delta-G!), but was still in the dark as to how to create an array in a library, keep it private, and make it the size I wanted when creating the object. I found the post Pass array size to class constructor I hope that will solve my problem.

I just thought I'd pass that info on to anyone having the same problem/conundrum.