Led matrix deifne "setColumn" names to reduce code

hi all,

i am using LedControl.h library to control 4 8x8 led matrix displays, those are on a single PCB. Now, i made a mask to cover the displays and then light up 3 columns of leds at a time to glow through the plastic mask and light up a day of week.

My code looks like this:

if (WeekDay == 1) {
    MaximMATRIX.setColumn(MonTueDisplay,0,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,1,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,2,B11111111); 
  } else {
    MaximMATRIX.setColumn(MonTueDisplay,0,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,1,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,2,B00000000);
  }

  if (WeekDay == 2) {
    MaximMATRIX.setColumn(MonTueDisplay,4,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,5,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,6,B11111111); 
  } else {
    MaximMATRIX.setColumn(MonTueDisplay,4,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,5,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,6,B00000000);
  }

and so on...

This works but there is a lot of the code for a simple task. My questions is, is there a way to name or define those 3 columns of leds for each display so i can get shorter and more simple code?

For example something like:

column MonTueDisplay from 0 to 2 = monday

many thanks for any hints,
Alek

Yes, it's called a function. Those things with parameters...

Ok, to explain it better.
I get WeekDay from RTC and based on the values from 1 to 7, i want to light up 3 columns of leds on matrix display for each value. So, i have a "sticker" on each of those 3 columns with cutout words for days (MON, TUE, WED...) so when the week day is '1' i have first 3 columns on 1st matrix display (MonTueDisplay) lit, and the word MON lights up.

as i said, this works great but too much code and i would like to optimize and make it shorter, if possible.

Many thanks,
Alek

aarg:
Yes, it's called a function. Those things with parameters...

Yes, i am aware of that and how it's called, just not sure how to do it :slight_smile:

thanks,
Alek

Actually you could think of it as a formulaic problem, and just code it in line. Because, there is a simple and fixed geometrical relationship between the day of the week and the columns. If you can index the columns independently (which should be your first job!), Then for the day of the week 'n', columns 3*(n-1), 3*(n-1)+1, 3*(n-1)+2 will be lit, the others no. So changing 'n' automatically selects the 3 columns you want.

Please don't ask for code, that is your job.

Thanks aarg,

I understand the what you are saying but still not sure how to do it, and since you kindly asked not to ask for code, i will need to find help elsewhere :wink:

Thanks,
Alek

Come on, I practically gave it to you on a silver platter.

    MaximMATRIX.setColumn(Display,3*(weekDay-1),B11111111);
    MaximMATRIX.setColumn(Display,3*(weekDay-1)+1,B11111111);
    MaximMATRIX.setColumn(Display,3*(weekDay-1)+2,B11111111);

Notice that I removed the, 'MonTue' from the Display name. I suspect (you didn't say) that you are handling each 8x8 separately, you should combine them as I said... it is also just some simple math. I haven't used that library in a while, but IIRC you can specify a unified matrix (combined multiple display drivers) in the library itself.

Yes, i am using each 8x8 separately, each 8x8 module is named by the days it displays, so MonTueDisplay is the 1st 8x8, WedThuDisplay is the 2nd 8x8 and so on… my entire code looks like this:

if (WeekDay == 1) {
    MaximMATRIX.setColumn(MonTueDisplay,0,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,1,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,2,B11111111); 
  } else {
    MaximMATRIX.setColumn(MonTueDisplay,0,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,1,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,2,B00000000);
  }

  if (WeekDay == 2) {
    MaximMATRIX.setColumn(MonTueDisplay,4,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,5,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,6,B11111111); 
  } else {
    MaximMATRIX.setColumn(MonTueDisplay,4,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,5,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,6,B00000000);
  }

  if (WeekDay == 3) {
    MaximMATRIX.setColumn(WedThuDisplay,0,B11111111);
    MaximMATRIX.setColumn(WedThuDisplay,1,B11111111);
    MaximMATRIX.setColumn(WedThuDisplay,2,B11111111); 
  } else {
    MaximMATRIX.setColumn(WedThuDisplay,0,B00000000);
    MaximMATRIX.setColumn(WedThuDisplay,1,B00000000);
    MaximMATRIX.setColumn(WedThuDisplay,2,B00000000);
  }

  if (WeekDay == 4) {
    MaximMATRIX.setColumn(WedThuDisplay,4,B11111111);
    MaximMATRIX.setColumn(WedThuDisplay,5,B11111111);
    MaximMATRIX.setColumn(WedThuDisplay,6,B11111111); 
  } else {
    MaximMATRIX.setColumn(WedThuDisplay,4,B00000000);
    MaximMATRIX.setColumn(WedThuDisplay,5,B00000000);
    MaximMATRIX.setColumn(WedThuDisplay,6,B00000000);
  }

  if (WeekDay == 5) {
    MaximMATRIX.setColumn(FriSatDisplay,0,B11111111);
    MaximMATRIX.setColumn(FriSatDisplay,1,B11111111);
    MaximMATRIX.setColumn(FriSatDisplay,2,B11111111); 
  } else {
    MaximMATRIX.setColumn(FriSatDisplay,0,B00000000);
    MaximMATRIX.setColumn(FriSatDisplay,1,B00000000);
    MaximMATRIX.setColumn(FriSatDisplay,2,B00000000);
  }

  if (WeekDay == 6) {
    MaximMATRIX.setColumn(FriSatDisplay,4,B11111111);
    MaximMATRIX.setColumn(FriSatDisplay,5,B11111111);
    MaximMATRIX.setColumn(FriSatDisplay,6,B11111111); 
  } else {
    MaximMATRIX.setColumn(FriSatDisplay,4,B00000000);
    MaximMATRIX.setColumn(FriSatDisplay,5,B00000000);
    MaximMATRIX.setColumn(FriSatDisplay,6,B00000000);
  }

  if (WeekDay == 7) {
    MaximMATRIX.setColumn(SunDisplay,0,B11111111);
    MaximMATRIX.setColumn(SunDisplay,1,B11111111);
    MaximMATRIX.setColumn(SunDisplay,2,B11111111); 
  } else {
    MaximMATRIX.setColumn(SunDisplay,0,B00000000);
    MaximMATRIX.setColumn(SunDisplay,1,B00000000);
    MaximMATRIX.setColumn(SunDisplay,2,B00000000);
  }

Thanks for the hint, my coding knowledge is bit more than basic but i will see what i can do :slight_smile:

That is not your entire code. Please do post it. What I'm talking about, somewhere you have defined 'MonTueDisplay' as some integer that addresses a certain display. That is what is forcing you to use this canonical code. Don't do that. Instead, use the integer as an index to address the display that you want. If you don't make that change, there is very little that you can do to improve the code you posted.

Mathematically, the 'Display' index is just the Day of the Week minus one, divided by two (because there are two days on each display). Do you see how simple this is?

I'm telling you how to set columns, not how to clear them. But you should be able to do that easily once you get the general method worked out.

Ah, no, i said this is the entire code for the display control, and yes i did not post the display definition, it goes like this :

#define SunDisplay 0
#define FriSatDisplay 1
#define WedThuDisplay 2
#define MonTueDisplay 3

and then the displays are initialized like this:

//---------------------------------------------------
  // Initialize Maxim 72xx MATRIX
  //---------------------------------------------------
  
  for (int i = 0; i < 4; i++)
  {
    // display wake up
    MaximMATRIX.shutdown(i, false);
    // clear display
    MaximMATRIX.clearDisplay(i);
  }

Many thanks,
Alek

elcrni:
Ah, no, i said this is the entire code for the display control

In fact that is not true. You did not qualify that statement. Go back and read your post.

aarg:
Mathematically, the 'Display' index is just the Day of the Week minus one, divided by two (because there are two days on each display). Do you see how simple this is?

Hah! It's starting to make sense, not completely clear but makes sense for a start! :slight_smile:

In fact that is not true. You did not qualify that statement. Go back and read your post.

not sure what i have missed...?

many thanks!

Thanks for posting the entire sketch. I think you will be able to piece it together from what I suggested. There are still lots of helpers around, why not throw another sketch together, and post it if you have problems?

hey man, my sketch has 3000+ lines of code, simply impossible to post it here, sorry.

thanks,
Alek

simply impossible to post it here,

So attach the file

Something like…

DISPLAY_TYPE* Display[] = { MonTueDisplay, WedThuDisplay, FriSatDisplay, SunDisplay };

uint8_t start = (WeekDay - 1) * 3;
uint8_t end = start + 3;
for (uint8_t x = 0; x < 21; x++)
{
  uint8_t d = x / 6;
  uint8_t c = x % 6; 
  MaximMATRIX.setColumn(Display[d], c, x >= start && x < end ? B11111111 : B00000000);
}

…but as you don’t show your complete code I had to guess at the Display array.

elcrni:
hey man, my sketch has 3000+ lines of code, simply impossible to post it here, sorry.

thanks,
Alek

I was typing a long reply and the electricity failed. :frowning: It's standard procedure to add long sketches as an attachment.

Many thanks PCBBC.

This is, literally, all the code i have for the matrix displays, there is nothing else that relates to matrix in my sketch:

LedControl MaximMATRIX=LedControl(MAXIMMATRIXDATA, MAXIMMATRIXCLK, MAXIMMATRIXLD, 4); // Define pins for Maxim 72xx and how many 72xx we use

#define SunDisplay 0
#define FriSatDisplay 1
#define WedThuDisplay 2
#define MonTueDisplay 3

under SETUP loop:

//---------------------------------------------------
  // Initialize Maxim 72xx MATRIX
  //---------------------------------------------------

  for (int i = 0; i < 4; i++)
  {
    // display wake up
    MaximMATRIX.shutdown(i, false);
    // clear display
    MaximMATRIX.clearDisplay(i);
  }  

and then loop to light up columns of leds on matrix:

if (dcfWeekDay == 1) {
    MaximMATRIX.setColumn(MonTueDisplay,0,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,1,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,2,B11111111); 
  } else {
    MaximMATRIX.setColumn(MonTueDisplay,0,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,1,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,2,B00000000);
  }

  if (dcfWeekDay == 2) {
    MaximMATRIX.setColumn(MonTueDisplay,4,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,5,B11111111);
    MaximMATRIX.setColumn(MonTueDisplay,6,B11111111); 
  } else {
    MaximMATRIX.setColumn(MonTueDisplay,4,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,5,B00000000);
    MaximMATRIX.setColumn(MonTueDisplay,6,B00000000);
  }

  if (dcfWeekDay == 3) {
    MaximMATRIX.setColumn(WedThuDisplay,0,B11111111);
    MaximMATRIX.setColumn(WedThuDisplay,1,B11111111);
    MaximMATRIX.setColumn(WedThuDisplay,2,B11111111); 
  } else {
    MaximMATRIX.setColumn(WedThuDisplay,0,B00000000);
    MaximMATRIX.setColumn(WedThuDisplay,1,B00000000);
    MaximMATRIX.setColumn(WedThuDisplay,2,B00000000);
  }

  if (dcfWeekDay == 4) {
    MaximMATRIX.setColumn(WedThuDisplay,4,B11111111);
    MaximMATRIX.setColumn(WedThuDisplay,5,B11111111);
    MaximMATRIX.setColumn(WedThuDisplay,6,B11111111); 
  } else {
    MaximMATRIX.setColumn(WedThuDisplay,4,B00000000);
    MaximMATRIX.setColumn(WedThuDisplay,5,B00000000);
    MaximMATRIX.setColumn(WedThuDisplay,6,B00000000);
  }

  if (dcfWeekDay == 5) {
    MaximMATRIX.setColumn(FriSatDisplay,0,B11111111);
    MaximMATRIX.setColumn(FriSatDisplay,1,B11111111);
    MaximMATRIX.setColumn(FriSatDisplay,2,B11111111); 
  } else {
    MaximMATRIX.setColumn(FriSatDisplay,0,B00000000);
    MaximMATRIX.setColumn(FriSatDisplay,1,B00000000);
    MaximMATRIX.setColumn(FriSatDisplay,2,B00000000);
  }

  if (dcfWeekDay == 6) {
    MaximMATRIX.setColumn(FriSatDisplay,4,B11111111);
    MaximMATRIX.setColumn(FriSatDisplay,5,B11111111);
    MaximMATRIX.setColumn(FriSatDisplay,6,B11111111); 
  } else {
    MaximMATRIX.setColumn(FriSatDisplay,4,B00000000);
    MaximMATRIX.setColumn(FriSatDisplay,5,B00000000);
    MaximMATRIX.setColumn(FriSatDisplay,6,B00000000);
  }

  if (dcfWeekDay == 7) {
    MaximMATRIX.setColumn(SunDisplay,0,B11111111);
    MaximMATRIX.setColumn(SunDisplay,1,B11111111);
    MaximMATRIX.setColumn(SunDisplay,2,B11111111); 
  } else {
    MaximMATRIX.setColumn(SunDisplay,0,B00000000);
    MaximMATRIX.setColumn(SunDisplay,1,B00000000);
    MaximMATRIX.setColumn(SunDisplay,2,B00000000);
  }

thats all i have that relates to matrix.

Once again, many thanks for any help.

Alek

Well, right at the top I see the #defines that I mentioned before, that are making your life difficult. You should be able to get some traction with pcbbc's code in reply #6. Notice that pcbbc does use your definitions in a table, which is good for flexibility, but isn't absolutely necessary, since the elements are already in order 0,1,2,3.

I was typing a long reply and the electricity failed. :frowning: It's standard procedure to add long sketches as an attachment.

no worries! :wink:

...but as you don't show your complete code I had to guess at the Display array.

The code is awesome, certainly would be out of my league to come up with this! :slight_smile:

Now i am getting compile error saying "expected primary-expression before ']' token"

on this line:

DISPLAY_TYPE* Display[] = { MonTueDisplay, WedThuDisplay, FriSatDisplay, SunDisplay };

Many thanks,
Alek