LED matrix control using arrays and state machine?

I don't know to whom I may have sold my soul for being able to, but putting things on youtube is very easy.

I point out that


  if (digitalRead(sensorPin1) == LOW) // read the value from the sensor1: //run
    state1 = 1;
  else state1 = 0;

and the other lines like it don't need the else part, the variable is already 0. Minor.

Now you are charging ahead on a bad path (I said so), but it is nice to see things working. I can't, so my efforts have been to get it to run with less memory.

You need to stop making progress and start learning about arrays. Anytime your code has variables that have numbers as part of their names is a chance to see if arrays could be used instead.

Also, you may see that all your state machine functions look very similar, no surprise there, and within them, the cases all look very similar.

Recognizing and exploiting patterns is key to making progress as a programmer. Currently my code to see your patterns has only one animation state machine and it is ready to handle anything that isn't too far out of the patterns you are working with.

Also also, I did manage to cut the data down considerably.

You only use three different pixel colors. A scheme allowing for 16 different colors code could encode each pixel as only 4 bits instead of 32.

But it gets better! You only have 5 unique 16 pixel lines in all the data. This means a 256 pixel pattern can be stored as just a list of 16 numbers, the numbers 0..4 corresponding to which of the five different lines is next in the pattern.

At the expense of some fiddling around during the rendering, for which there is plenty of time, I have reduced all the data to less than 500 bytes. Here is my data section. YES I have ditched a great deal of general flexibility.

/* these are the unique colors from the pattern arrays */

# define aBK	0		// a black color
# define aRD	1		// a red color
# define aWH	2		// a white color

uint32_t myColors[3] = {0x0, 0xff0000, 0xffffff};

/* these are the unique lines from the pattern arrays
	"................",
	"..====....====..",
	"==....====....==",
	"================",
	"XXXXXXXXXXXXXXXX",
*/

unsigned char uniqueLines[5][16] = {
	{aBK, aBK, aBK, aBK, aBK, aBK, aBK, aBK, aBK, aBK, aBK, aBK, aBK, aBK, aBK, aBK, },
	{aBK, aBK, aRD, aRD, aRD, aRD, aBK, aBK, aBK, aBK, aRD, aRD, aRD, aRD, aBK, aBK, },
	{aRD, aRD, aBK, aBK, aBK, aBK, aRD, aRD, aRD, aRD, aBK, aBK, aBK, aBK, aRD, aRD, },
	{aRD, aRD, aRD, aRD, aRD, aRD, aRD, aRD, aRD, aRD, aRD, aRD, aRD, aRD, aRD, aRD, },
	{aWH, aWH, aWH, aWH, aWH, aWH, aWH, aWH, aWH, aWH, aWH, aWH, aWH, aWH, aWH, aWH, },
};

# define NPATTERNS	22

unsigned char frames[NPATTERNS][16] = {
// pattern  0
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },

// pattern  1
	{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, },

// pattern  2
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 3, 3, 3, },

// pattern  3
	{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 3, 3, 3, },

// pattern  4
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 0, },

// pattern  5
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 3, 3, 3, },

// pattern  6
	{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, },

// pattern  7
	{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, },

// pattern  8
	{2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, },

// pattern  9
	{2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },

// pattern 10
	{2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },

// pattern 11
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },

// pattern 12
	{3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 0, 0, 3, 3, 3, },

// pattern 13
	{3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 0, 0, 3, 3, 3, },

// pattern 14
	{3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 0, 0, 3, 3, 3, },

// pattern 15
	{3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 3, 3, 3, },

// pattern 16
	{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 3, 3, 3, },

// pattern 17
	{2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 4, 4, 0, 0, 0, },

// pattern 18
	{2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 4, 4, 0, 0, 0, },

// pattern 19
	{2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 0, },

// pattern 20
	{2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 0, },

// pattern 21
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
};

I hope this inspires you. I understand you may only want whizzy lighting system for your car, but if you do more projects you will def want to know more about arrays, functions and a few other steps up the big ladder.

a7

1 Like