Arduino wont work properly when dynamic memory goes above 70%

I am doing a light painting led strip application. Uses a SD card to store a BMP image, and print it strip by strip over an array of LEDs.

my project uses a Bluno Nano(same mcu as Uno, but with integrated BLE).

my adafruit neopixel library wont work properly after dynamic memory goes above 70%. 69% will still work..

would be grateful if anyone can assist me with this.

is this normal?

Post your code and maybe you get some advice on possibilities for optimisation.

6v6gt:
Post your code and maybe you get some advice on possibilities for optimisation.

Hi, cant attach code below due to 9k character limit.

you can find my code in here: my code in arduino online editor

Thank you!

You can add a large program by attaching your .ino file. Your link did not work for me.

...R

Robin2:
You can add a large program by attaching your .ino file. Your link did not work for me.

...R

hmm.. you can view my code here: my code here

thank you!

its like a big mystery. uploads to board is successful, but when the dynamic mem usage goes above 70%, the whole adafruit library is turned unusable lol. any calls for the library functions all either end up getting skipped, or stuck inside the library.

What's in the ZIP file? People are a bit wary about downloading ZIP files from unknown sources.

...R

Robin2:
What's in the ZIP file? People are a bit wary about downloading ZIP files from unknown sources.

...R

the zip file contains the NeoPixel_Painter.ino and gamma.h project.

it just makes it easier to download the zip and open the .ino as it all are alr in place.

i understand the concern about zip files hence i uploaded both.

anyway, ive optimised it a little bit. dynamic ram used to be 72%, now its 68%. but i still gonna need to add more features so some optimisation help would be good. thank you!

you can view my code here: my code here

I would upgrade to something with more RAM.

'Optimisation' when your using the SD card libraries has its limits.

Arduino RAM is shared by the heap (globals and static data) and the stack (return addresses, for-next indexes and local data).

During runtime a sketch may allocate more heap, C++ container classes (like String) and other dynamic memory practices that can work well on a PC with lots of RAM are poison in small RAM environments.

Functions that pass a lot of data on the stack will for a time grow stack which may overwrite or be overwriten by heap - crash!
Functions called from functions grow the stack and may call further functions.
Recursive functions grow the stack by calling themselves.

Depending on how the sketch is written it can be a RAM hog.

There are AVR chip options with 4K, 8K, or 16K internal RAM. The Mega2560 has 8K internal, can have external RAM as well.

There are Arduinos with ARM chips with 32K, 64K or 96K RAM that I have seen. Those are 32-bit machines.

Thing is that your sketch may or may not run as expected on an ARM chip.

GoForSmoke:
Arduino RAM is shared by the heap (globals and static data) and the stack (return addresses, for-next indexes and local data).

During runtime a sketch may allocate more heap, C++ container classes (like String) and other dynamic memory practices that can work well on a PC with lots of RAM are poison in small RAM environments.

Functions that pass a lot of data on the stack will for a time grow stack which may overwrite or be overwriten by heap - crash!
Functions called from functions grow the stack and may call further functions.
Recursive functions grow the stack by calling themselves.

Depending on how the sketch is written it can be a RAM hog.

There are AVR chip options with 4K, 8K, or 16K internal RAM. The Mega2560 has 8K internal, can have external RAM as well.

There are Arduinos with ARM chips with 32K, 64K or 96K RAM that I have seen. Those are 32-bit machines.

Thing is that your sketch may or may not run as expected on an ARM chip.

guess my best option would be to get a board upgrade..

size is quite an impt feature for my proj. my current bluno nano(basically a uno + BLE) is like 53x19x12mm(2.09x0.75x0.47").. a mega is kinda a little too big..

would appreciate if someone could recommend me a small form factor board that uses a avr chip with higher SRAM.

I really like the Teensy 3.2. It has a 32-bit ARM Cortex-M4 that runs up to 96MHz, 64KB of RAM and 256KB of flash. I/O is 5V tolerant.
With a PCB size of 36x18mm, it's even smaller than the Arduino Nano.
Arduino compatibility is excellent, never had any problems.

It supports up to 4,000 Neopixels and uses DMA to minimize CPU load when driving them.
https://www.pjrc.com/teensy/td_libs_OctoWS2811.html
The results are pretty impressive: OctoWS2811 LED Library - YouTube

Pieter

PieterP:
I really like the Teensy 3.2. It has a 32-bit ARM Cortex-M4 that runs up to 96MHz, 64KB of RAM and 256KB of flash. I/O is 5V tolerant.
With a PCB size of 36x18mm, it's even smaller than the Arduino Nano.
Arduino compatibility is excellent, never had any problems.

It supports up to 4,000 Neopixels and uses DMA to minimize CPU load when driving them.
OctoWS2811 LED Library, Driving Hundreds to Thousands of WS2811 LEDs with Teensy 3.0
The results are pretty impressive: https://www.youtube.com/watch?v=M5XQLvFPcBM

Pieter

its not compatible with my project at hand. installed all the teensy stuff onto my arduino ide, and its not working. im quite new in these board and mcu stuff so i am not sure how to convert the code.

posted a thread on teensy forum for help. Arduino uno to Teensy help needed | Teensy Forum

First error is pretty simple, the ARM math library contains a function 'gamma', so you can't use it as a variable name, just change the name of your array.
Second one is just a warning,
The third one is a bit trickier. You are writing directly to the timer registers in your sketch. These registers are AVR-specific. You'll have to use the ARM equivalent, or use the Teensy Timer libraries. What do you need it for?
Did you write all that code yourself, or did you copy it?

Pieter

PieterP:
First error is pretty simple, the ARM math library contains a function 'gamma', so you can't use it as a variable name, just change the name of your array.
Second one is just a warning,
The third one is a bit trickier. You are writing directly to the timer registers in your sketch. These registers are AVR-specific. You'll have to use the ARM equivalent, or use the Teensy Timer libraries. What do you need it for?
Did you write all that code yourself, or did you copy it?

Pieter

i would say about half or the main application is copied from adafruit's neopixel_painter. im quite clueless about what those timers are.

i refer to : A Simple and Interactive Explanation of the Teensy's 16-bit timer (Timer1)

the timers that teensy has are same as what arduino has? o_O

tzijie:
i refer to : A Simple and Interactive Explanation of the Teensy's 16-bit timer (Timer1)

the timers that teensy has are same as what arduino has? o_O

That article is about the old Teensy++, which is an AVR board, the new Teensy 3.2 is an ARM board.

I think the example sketches OctoWS2811 library are a great place to start.

Pieter

PieterP:
That article is about the old Teensy++, which is an AVR board, the new Teensy 3.2 is an ARM board.

I think the example sketches OctoWS2811 library are a great place to start.

Pieter

guess if i gonna keep using that code i gotta use avd mcus :confused: or mofidy that..

guess i gotta lookup TimerOne & TimerThree instead :confused:

The Neopixel library is compatible with Teensy, however, the sketch you copied is not portable.

I don't see why you would stick with AVR boards just for that reason.
I think that, in general, it's not a good idea to copy code without understanding it. If you find a piece of code that works, try to understand it, line by line, before pasting it into your own sketch. It's a great opportunity to learn. And if you don't understand what you're doing, you'll end up with terrible Frankenstein-code, patched together, and with lots of parts that aren't really necessary, but you didn't want to remove, because "If it ain't broken, don't fix it". (Speaking from my own experience here :slight_smile: )

I'm recommending Teensy, because the OctoWS2811 library seems to be exactly what you're after. It even comes with an example that can read videos from an SD card and display them on the Neopixel "display". It can't be too hard to change that code for using bitmaps instead.

Pieter

Im sorry for bothering you again.

i refer to Teensy USB Development Board,
I see that teensy++ 2.0 uses an AVR chip. I've changed the board on my arduino IDE to teensy 2.0 and tried uploading, and i receiving the same error. any idea y?

about what you mentioned, allow me to explain my situation.
currently, my main objective is the phone application associated to this arduino application.
deliverables include but not limited to, sending images, making ur own text, etc.

i will certainly need to understand the printing part and how it works sooner or later, but for now, those are not the priority. i just kinda need a board that can reliably make the application work. so yeah, you are not wrong about the 'if its not broken, dont fix it' rule.

i will certainly need to understand the arduino code sometime in the future, and hopefully, if time permits, ill be able to change and optimise it. but at this juncture, the phone app comes first.. so yeah..

edit: holy cow, i change to teensy 2.0, and tried to verify, my code works, and the teensy ui popped up.
i closed it, and change back to teensy++ 2.0, and it works now too. the typical, "my code doesnt work, and i dont know why. my code works, cant i also dont know why.." -___-'''''

PieterP:
The Neopixel library is compatible with Teensy, however, the sketch you copied is not portable.

I don't see why you would stick with AVR boards just for that reason.
I think that, in general, it's not a good idea to copy code without understanding it. If you find a piece of code that works, try to understand it, line by line, before pasting it into your own sketch. It's a great opportunity to learn. And if you don't understand what you're doing, you'll end up with terrible Frankenstein-code, patched together, and with lots of parts that aren't really necessary, but you didn't want to remove, because "If it ain't broken, don't fix it". (Speaking from my own experience here :slight_smile: )

I'm recommending Teensy, because the OctoWS2811 library seems to be exactly what you're after. It even comes with an example that can read videos from an SD card and display them on the Neopixel "display". It can't be too hard to change that code for using bitmaps instead.

Pieter

Teensy 2.0 is the same chip as Leonardo with 2.5K RAM. Teensy++ 2.0 is bigger chip and board, 8K RAM.

There is also a small 1284 breakout by Crossroads (member here):

1284Mini.

ATMega1284P-AU with 32 IO, Dual Hardware UART, 128K FLASH, 16K SRAM. Schematic, Layout, PL
Bare Boards $3. Kits, $15. Assembled boards with straight male headers $20. Add USPS shipping & paypal fee.

Found on this page: Cross Roads Electronics

Look around! Shop! Shipping can be the difference depending on where you are in the whole wide world!

The ARM boards have more bang for the buck. See if where you got that code has an ARM version. If they don't then figure it should take time and work to DIY.

OMG!! if i were to buy teensy++2.0, i am going to need to buy another bluetooth and sd card breakout.
perhaps i could get 1 custom made. u patronised them before? how long do they take to get things done? i kinda only got 8weeks left lol

GoForSmoke:
Teensy 2.0 is the same chip as Leonardo with 2.5K RAM. Teensy++ 2.0 is bigger chip and board, 8K RAM.

There is also a small 1284 breakout by Crossroads (member here):

Found on this page: Cross Roads Electronics

Look around! Shop! Shipping can be the difference depending on where you are in the whole wide world!

The ARM boards have more bang for the buck. See if where you got that code has an ARM version. If they don't then figure it should take time and work to DIY.