Two Dimensional Sets and Referencing sets in sets

Hello,

I am very new to this so please have a bit of patience with me. I had a bit of money burning a hole in my pocket when I walked through a Microcenter and here I am now.

I have purchased an Arduino starter kit and one of the projects it gives you is a 8 by 8 led array. The instructions only tell you to make it light up the number 0, but, I wanted to go a bit further and have it count from 0 to 9 in a loop. I have done this, however, it felt like the code I made was unnecessarily long and complicated and I wanted to try to achieve the same result by doing it two ways.

A: Using a two dimensional set
B: Using sets in sets.

Example A: The original code - everything is super long here.


void clear(void){
for(int i=0;i<8;i++){
digitalWrite(i+2,LOW);
digitalWrite(i+10,HIGH);
}
}
void drawPoint(unsigned char x, unsigned char y){
clear();
digitalWrite(x+2,HIGH);
digitalWrite(y+10,LOW);
delay(1);
}

unsigned char Zero[]={0x00,0x1c,0x22,0x22,0x22,0x22,0x22,0x1c};
unsigned char One[]={0x10,0x30,0x10,0x10,0x10,0x10,0x10,0x7c};
unsigned char Two[]={0x38,0x44,0x04,0x04,0x38,0x40,0x40,0x7c};
unsigned char Three[]={0x38,0x44,0x04,0x04,0x38,0x04,0x44,0x38};
unsigned char Four[]={0x48,0x48,0x48,0x78,0x08,0x08,0x08,0x08};
unsigned char Five[]={0x3c,0x20,0x20,0x20,0x1c,0x04,0x04,0x3c};
unsigned char Six[]={0x38,0x44,0x40,0x40,0x7c,0x44,0x44,0x38};
unsigned char Seven[]={0x7c,0x04,0x04,0x08,0x08,0x08,0x08,0x08};
unsigned char Eight[]={0x38,0x44,0x44,0x38,0x44,0x44,0x44,0x38};
unsigned char Nine[]={0x38,0x44,0x44,0x7c,0x04,0x04,0x44,0x38};

void Show_Zero(void){
unsigned char i,j,data;
for(i=0;i<8;i++){
data=Zero[i];
for(j=0;j<8;j++){
if(data & 0x01)drawPoint(j,i);
data>>=1;
}
}
}

void Show_One(void){
unsigned char i,j,data;
for(i=0;i<8;i++){
data=One[i];
for(j=0;j<8;j++){
if(data & 0x01)drawPoint(j,i);
data>>=1;
}
}
}

void Show_Two(void){
unsigned char i,j,data;
for(i=0;i<8;i++){
data=Two[i];
for(j=0;j<8;j++){
if(data & 0x01)drawPoint(j,i);
data>>=1;
}
}
}

void Show_Three(void){
unsigned char i,j,data;
for(i=0;i<8;i++){
data=Three[i];
for(j=0;j<8;j++){
if(data & 0x01)drawPoint(j,i);
data>>=1;
}
}
}

void Show_Four(void){
unsigned char i,j,data;
for(i=0;i<8;i++){
data=Four[i];
for(j=0;j<8;j++){
if(data & 0x01)drawPoint(j,i);
data>>=1;
}
}
}

void Show_Five(void){
unsigned char i,j,data;
for(i=0;i<8;i++){
data=Five[i];
for(j=0;j<8;j++){
if(data & 0x01)drawPoint(j,i);
data>>=1;
}
}
}

void Show_Six(void){
unsigned char i,j,data;
for(i=0;i<8;i++){
data=Six[i];
for(j=0;j<8;j++){
if(data & 0x01)drawPoint(j,i);
data>>=1;
}
}
}

void Show_Seven(void){
unsigned char i,j,data;
for(i=0;i<8;i++){
data=Seven[i];
for(j=0;j<8;j++){
if(data & 0x01)drawPoint(j,i);
data>>=1;
}
}
}

void Show_Eight(void){
unsigned char i,j,data;
for(i=0;i<8;i++){
data=Eight[i];
for(j=0;j<8;j++){
if(data & 0x01)drawPoint(j,i);
data>>=1;
}
}
}

void Show_Nine(void){
unsigned char i,j,data;
for(i=0;i<8;i++){
data=Nine[i];
for(j=0;j<8;j++){
if(data & 0x01)drawPoint(j,i);
data>>=1;
}
}
}
void setup() {
int i=0;
for(i=2;i<18;i++){
pinMode(i,OUTPUT);
}
clear();
}

void loop() {
unsigned long q;
for(q=0;q<100;q++){
Show_Zero();
}
unsigned long s;
for(s=0;s<100;s++){
Show_One();
}
unsigned long t;
for(t=0;t<100;t++){
Show_Two();
}
unsigned long u;
for(u=0;u<100;u++){
Show_Three();
}
unsigned long v;
for(v=0;v<100;v++){
Show_Four();
}
unsigned long w;
for(w=0;w<100;w++){
Show_Five();
}
unsigned long x;
for(x=0;x<100;x++){
Show_Six();
}
unsigned long y;
for(y=0;y<100;y++){
Show_Seven();
}
unsigned long z;
for(z=0;z<100;z++){
Show_Eight();
}
unsigned long a;
for(a=0;a<100;a++){
Show_Nine();
}
}


Example B - the two dimensional set
unsigned long Sets[][8] =
{
{0x00,0x1c,0x22,0x22,0x22,0x22,0x22,0x1c},
{0x10,0x30,0x10,0x10,0x10,0x10,0x10,0x7c},
{0x38,0x44,0x04,0x04,0x38,0x40,0x40,0x7c},
{0x38,0x44,0x04,0x04,0x38,0x04,0x44,0x38},
{0x3c,0x20,0x20,0x20,0x1c,0x04,0x04,0x3c},
{0x38,0x44,0x40,0x40,0x7c,0x44,0x44,0x38},
{0x7c,0x04,0x04,0x08,0x08,0x08,0x08,0x08},
{0x38,0x44,0x44,0x38,0x44,0x44,0x44,0x38},
{0x38,0x44,0x44,0x7c,0x04,0x04,0x44,0x38}
};

How would I get it repeat the first line continuously before moving onto the next line?

example C - sets in a set- how would someone even get started in referencing this.

int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int j;
unsigned long Set[]={Zero[a],One[b],Two[c],Three[d],Four[e],Five[f],Six[g],Seven[h],Eight[i],Nine[j]};

Welcome to the forum

I am not sure why you want it to repeat, but to access the values in row 0 of the array you could do this

for (int x = 0; x < 8; x++)
{
  byte value = Sets[0][x];
}

As you can see, the row number is used as the first index and the column number is used as the second index

Your example C makes no sense because the integers have not had a value defined for them and the arrays have not even been declared

1 Like

When you see a data structure that follows a pattern there is an opportunity to use an array (or set, as you call it). In this case all the data for the 10 digits can be managed in a single bidimensional array.

When you see a code structure that follows a pattern there is an opportunity to use a function. In this case all the repetitive code you’ve written for displaying each of the different digits can be condensed in a single function, called 10 times with their respective digit data.

There are of course other uses for both arrays and functions.

1 Like

Post code so it looks like code

void setup() {

}

void loop() {

}

The easiest way to do this is to use the IDE Copy for Forum tool, then paste it here.

You might want to apply the IDE Auto Format tool to the sketch first.

You've got ahead of yourself, nothing wrong with that, but things like this will be much easier to code when you have learned not all that much more about how to.

For this reason, following a, or even several at once, organized tutorials or videos or whatever will mean you might skip ever writing something marvelous, by which I mean keep this sketch handy and in a few weeks or months or years drag it out and marvel.

I look forward to reading your code once it is formatted and posted.

Do I think you are creating characters by rapidly calling drawPoint()? That's reasonably advanced, so good on you if.

a7

1 Like

Yes,

drawPoint() is rapidly being called to create characters. Although, I can't say that I came up with that solution myself. Each set is rapidly cycled through to produce a certain number. The example only showed Zero, after a bunch of fumbling around I figured out the sets for one through nine.

The thing that really bothered me about example A is that I used 9 additional show functions for digits 1-9. Initially, I wanted to have a array of arrays to shorten the code, but, after looking at:

I found you could create a two dimensional array. Since I am able to count from zero to nine with my current code I wanted to do it at least with a two dimensional array and a single function like Show_One() but instead Show_Count() or something similar.

My other goal for my understanding would be to use an array inside of an array to count, but, I don't even know if that is possible

Yes - that is my exact goal to condense those ten functions into a single function.

I FIGURED IT OUT!!!

void clear(void){
  for(int i=0;i<8;i++){
    digitalWrite(i+2,LOW);
    digitalWrite(i+10,HIGH);
  }
}
void drawPoint(unsigned char x, unsigned char y){
  clear();
  digitalWrite(x+2,HIGH);
  digitalWrite(y+10,LOW);
  delay(1);
}
unsigned long Sets[][8] =
{
{0x00,0x1c,0x22,0x22,0x22,0x22,0x22,0x1c},
{0x10,0x30,0x10,0x10,0x10,0x10,0x10,0x7c},
{0x38,0x44,0x04,0x04,0x38,0x40,0x40,0x7c},
{0x38,0x44,0x04,0x04,0x38,0x04,0x44,0x38},
{0x3c,0x20,0x20,0x20,0x1c,0x04,0x04,0x3c},
{0x38,0x44,0x40,0x40,0x7c,0x44,0x44,0x38},
{0x7c,0x04,0x04,0x08,0x08,0x08,0x08,0x08},
{0x38,0x44,0x44,0x38,0x44,0x44,0x44,0x38},
{0x38,0x44,0x44,0x7c,0x04,0x04,0x44,0x38}
};

void show_Set(void){
  unsigned char g,h,i,j,data;
  for(h=0;h<8;h++){
      for(g=0;g<100;g++){  //to repeat long enough to be visible
        for(i=0;i<8;i++){
          data = Sets[h][i];
          for(j=0;j<8;j++){
          if(data & 0x01)drawPoint(j,i);
          data>>=1;
        }}}}}
      
void setup() {
  int i=0;
  for(i=2;i<18;i++){
    pinMode(i,OUTPUT);
  }
  clear();
}

void loop() {
  show_Set();
}

Nice.

Until formatted code naturally springs from your fingers, use the IDE Auto Format tool, it puts your code into one of the standard ways to format code and makes it easier for others to read.

And I know you could do this now:

Write a function that displays the digit you pass as an argument, long enough to be visible.

So this sequence in the loop would show 3, 1, 4 over and over:

void loop() {
  showDigit(3);
  showDigit(1);
  showDigit(4);

  delay(500);
}

Another thing you might already be doing is to read on these fora. Pick topics that look interesting and are neither too simple or complex and follow along. I learn something every day here.

a7