Go Down

Topic: problem with array in library (Read 2 times) previous topic - next topic

robtillaart


Try this:

in MoodLight.h declare:

   int* _dim_curve;     

in MoodLight.cpp declare a global array dc and initialize it (consider making the type uint8_t)

int dc[256] = {
    0,   1,   1,   2,   2,   2,   2,   2,   2,   3,   3,   3,   3,   3,   3,   3,
   ...
    193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255
};

Change the constructor so the member accesses the global array.

MoodLight::MoodLight()
{   
   _dim_curve = dc;
}

should work ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

kasperkamperman.com

Thanks this works. However I still wonder why I couldn't use the global array directly?

After you're example I tried it in my old way, and it works.

Probably this int dc[256] was the issue. I forgot to set the length of the array..

Whats the benefit to make the type in an unsigned int ? : uint8_t

robtillaart

Quote
Whats the benefit to make the type in an unsigned int ? : uint8_t


RANGE:
int = -32768..32767
unsigned int = 0..65535
uint8_t = 0..255   // unsigned in 8 bits

FOOTPRINT
int array[256]; takes 256 x int = 2 bytes = 512 bytes
uint8_t array[256] takes 256 x 1 byte = 256 bytes.

As you only have 2048 bytes of RAM this differs 12.5%

Could you post your working code for all who find this thread interesting?


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

kasperkamperman.com

Clear. In my other examples i've used the byte type for this.

I've put the library online in my blog:
http://www.kasperkamperman.com/blog/arduino-moodlight-library/

If there are any issues please let my know.

PaulS

Quote
Probably this int dc[256] was the issue. I forgot to set the length of the array..

Probably not. If you don't define a length, the compiler will determine the length based on the number of initializers present.

What you were doing was initializing the global array, but not the member variable. You were then referencing the member variable (var test = ml._dim_curve[254];), which was never initialized.

Go Up