Array animation

I have an Arduino and am trying to find the most efficient way to animate pixels in an array. The array is represented by 3 arrays of 30 uint8_t s. (30 pixels * R + G + B levels 0-255).

What is the best way to move pixels across an array independently? This is limited memory memory (2 KB of RAM), and the array occupies 720 bytes. At first I tried to use an array for each pixel and ran out of memory. The second implementation is used if assertions, and it works well, but it is very tedious to create new templates. I am looking for a way to solve a problem that may be better than I am solving it now.

Here is an example of a template that I would like to animate.

 X moves forward 1 each cycle. Y moves forward 3 places then back 2. Z moves backwards 3 places each cycle. |_|X|_|_|_|_|_|_|_|_|_|_|_|_|Y|_|_|_|_|_|_|_|_|_|_|_|_|_|_|Z| |_|_|X|_|_|_|_|_|_|_|_|_|_|_|_|_|_|Y|_|_|_|_|_|_|_|_|Z|_|_|_| |_|_|_|X|_|_|_|_|_|_|_|_|_|_|_|Y|_|_|_|_|_|_|_|Z|_|_|_|_|_|_| 

And here is the code I would use.

 void animateScene1() { for(int i = 0; i <= numPixels; i++) { setColor(i, X); if(i < 15) { if(i % 2 == 0) setColor((i+15)+3), Y); else setColor((i+15)-2), Y); } if(i < 10) setColor(numPixels-3*i, Z); } } 

Any ideas on better ways?

+4
source share
1 answer

How about such a feature?

 int pixelAt(int index, int min, int max, int a, int b, int b_2) { int pixel = -1; if (min <= index && index <= max) { pixel = a * index + b; if (i % 2) { pixel += b_2; } } return pixel; } 

Then you will have:

 for (int i = 0; i < numPixels; i++) { // This assumes setColor(-1, x) does nothing setColor(pixelAt(i, 0, numPixels, 1, 0, 0), X); setColor(pixelAt(i, 0, 15, 1, 18, -5), Y); setColor(pixelAt(i, 0, 10, -3, numPixels, 0), Z); } 

Of course, the pixelAt function will be much more complicated if you add new requirements, for example, change the multiplication factor by even numbers, process it several times by 3, etc. However, you can use functions like this to reduce the number of functions you have to write. One for simple patterns, one for even / odd patterns, one for multiples of three, etc. Depends on how different the templates are and how much memory space there is for the code.

+2
source

Source: https://habr.com/ru/post/1336706/


All Articles