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]};