Low memory available, stability problems may occur. Have i over complicated

Hi, i am using this programme i wrote to run 40 leds at specific time settings on a mega board
I am getting the message "Low memory available, stability problems may occur."
I am using 40 pins in total, my question is have i over complicated my programme or is there a more
efficient way to write this programme...Thanks for any advice

poole_test.ino (2.44 KB)

whoo! why did you make 'periods' a unsigned long!

uint16_t or unsigned int, should be more then enough.

Are you driving the LEDs with the Mega pins directly?

Did you know there are current limits for each pin, each group of pins on a port and the total?

I only see less than a 1000 bytes in arrays there. Mega has 8K. Odd that it complains.
Looks like states[] could be byte type also.

How does this line work?

if (millis() - startTimes[led] >= periods[led][states[led]])

comparing an unsigned long time result to what? I can't follow what the right hand side does.
Same here

if (periods[led][states[led]] == 0)

What's the left side do?

On array lines such as this

{6310, 490, 500, 490, 500, 490, 500, 490, 500, 490, 500, 490, 500, 490, 500, 490, 500, 490, }, //pin 9

How does the program know to go deep into the line? Just trying to understand how that works.

a1gezzer:
I am using 40 pins in total, my question is have i over complicated my programme or is there a more
efficient way to write this programme...

You want to drive 40 pins and have declared

unsigned long periods[NUM_LEDS][41]

In your code with NUM_LEDS==41 that is 41*41 = 1681 'long' variables, each of them taking 4 bytes = 1681 * 4 bytes = 6724 bytes

Can you please describe what you want to do with those 40 pins?

Most likely you will NOT have to declare that many data to do what you want.

Hi, thanks for the input, i am good on current limits with the leds, and i did show 41 pins when i am only using 40 so thanks for pointing that out, using unsigned int instead of unsigned long has made a huge difference in memory usage.
Sorry for my ignorance i wrote this about 4 months ago and have just been changing the time sequences and only the time sequences to great effect but there are obviously some flaws ... but as the saying goes use it or lose it, and i have lost some of it !.
I very rarely use more than 25 pins and this was my first problem when i got up to 40 pins which was for a one of trial . I think i might have a fresh work through the programme and hopefully fire up some grey matter that has since stopped working

Rule - Always use the smallest type that will do the job never use an int if a byte will do

Rule - Use the F() macro if you can (wont help the op but)

Have you selected the mega in the IDE and not the default Uno?

Mark