I am begginer wondering how to shorten my code

My program is supposed to make a 7-segment light from 0-9, and is quite long and repatitive, what can i do to shorten it

int LED0[5] = { 8,9,10,11,12};
int LED1[2] = { 7,10};
int LED2[5] = { 8,9,10,11,13};
int LED3[5] = { 7,8,10,11,13};
int LED4[4] = { 7,10,12,13};
int LED5[5] = { 7,8,11,12,13};
int LED6[6] = { 7,8,9,11,12,13};
int LED7[3] = { 7,10,11};
int LED8[7] = { 7,8,9,10,11,12,13};
int LED9[5] = { 7,10,11,12,13};

void setup(){
for (int zero = 0; zero <= 5; zero++){
pinMode (LED0[zero], OUTPUT);}

for (int one = 0; one <= 2; one++){
pinMode (LED1[one], OUTPUT);}

for (int two = 0; two <= 5; two++){
pinMode (LED2[two], OUTPUT);}

for (int three = 0; three <= 5; three++){
pinMode (LED3[three], OUTPUT);}

for (int four = 0; four <= 4; four++){
pinMode (LED4[four], OUTPUT);}

for (int five = 0; five <= 5; five++){
pinMode (LED5[five], OUTPUT);}

for (int six = 0; six <= 6; six++){
pinMode (LED6[six], OUTPUT);}

for (int seven = 0; seven <= 3; seven++){
pinMode (LED7[seven], OUTPUT);}

for (int eight = 0; eight <= 7; eight++){
pinMode (LED8[eight], OUTPUT);}

for (int nine = 0; nine <= 6; nine++){
pinMode (LED9[nine], OUTPUT);}
}

void loop(){
for(int A = 0; A <= 5; A++){
digitalWrite (LED0[A], HIGH);}
delay(1000);
for(int B=0; B <= 5; B++){
digitalWrite (LED0**,LOW);}**
for(int C = 0; C <= 1; C++){
digitalWrite (LED1
```c
**, HIGH);}
delay(1000);
for(int D=0; D <= 1; D++){
digitalWrite (LED1[D],LOW);}

for(int E = 0; E <= 4; E++){
digitalWrite (LED2[E], HIGH);}
delay(1000);
for(int F=0; F <= 4; F++){
digitalWrite (LED2[F],LOW);}

for(int G = 0; G <= 4; G++){
digitalWrite (LED3[G], HIGH);}
delay(1000);
for(int H=0; H <= 4; H++){
digitalWrite (LED3[H],LOW);}

for(int I = 0; I <= 3; I++){
digitalWrite (LED4[I], HIGH);}
delay(1000);
for(int J=0; J <= 3; J++){
digitalWrite (LED4[J],LOW);}

for(int K = 0; K <= 4; K++){
digitalWrite (LED5[K], HIGH);}
delay(1000);
for(int L=0; L <= 4; L++){
digitalWrite (LED5[L],LOW);}

for(int M = 0; M <= 5; M++){
digitalWrite (LED6[M], HIGH);}
delay(1000);
for(int N=0; N <= 5; N++){
digitalWrite (LED6[N],LOW);}

for(int O = 0; O <= 2; O++){
digitalWrite (LED7[O], HIGH);}
delay(1000);
for(int P=0; P <= 2; P++){
digitalWrite (LED7[P],LOW);}

for(int Q = 0; Q <= 6; Q++){
digitalWrite (LED8[Q], HIGH);}
delay(1000);
for(int R=0; R <= 6; R++){
digitalWrite (LED8[R],LOW);}

for(int S = 0; S <= 5; S++){
digitalWrite (LED9[S], HIGH);}
delay(1000);
for(int T=0; T <= 4; T++){
digitalWrite (LED9[T],LOW);}

}**
```

It would be helpful if you annotate the code. Add some notes describing the basics of what is happening in the code.

Shorten (source)code:

  • Loops
  • Functions
  • Libraries / Classes
  • Macros (handle with care)

sorry i should have commented that was unproffisional, i semi commented because the program is repatative

int LED0[5] = { 8,9,10,11,12}; //make an array with all the pins necessary to display 0 on the 7-segment
int LED1[2] = { 7,10};//make an array with all the pins necessary to display 1 on the 7-segment
int LED2[5] = { 8,9,10,11,13};//make an array with all the pins necessary to display 2 on the 7-segment
int LED3[5] = { 7,8,10,11,13};//make an array with all the pins necessary to display 3 on the 7-segment
int LED4[4] = { 7,10,12,13};//make an array with all the pins necessary to display 4 on the 7-segment
int LED5[5] = { 7,8,11,12,13};//make an array with all the pins necessary to display 5 on the 7-segment
int LED6[6] = { 7,8,9,11,12,13};//make an array with all the pins necessary to display 6 on the 7-segment
int LED7[3] = { 7,10,11};//make an array with all the pins necessary to display 7 on the 7-segment
int LED8[7] = { 7,8,9,10,11,12,13};//make an array with all the pins necessary to display 8 on the 7-segment
int LED9[5] = { 7,10,11,12,13};//make an array with all the pins necessary to display 8 on the 7-segment

void setup(){
//started a for loop for each of the arrays so i can turn all the numbers/pins into a output on the arduino and goes all the way down till the end of the void loop
for (int zero = 0; zero <= 5; zero++){
pinMode (LED0[zero], OUTPUT);}

for (int one = 0; one <= 2; one++){
pinMode (LED1[one], OUTPUT);}

for (int two = 0; two <= 5; two++){
pinMode (LED2[two], OUTPUT);}

for (int three = 0; three <= 5; three++){
pinMode (LED3[three], OUTPUT);}

for (int four = 0; four <= 4; four++){
pinMode (LED4[four], OUTPUT);}

for (int five = 0; five <= 5; five++){
pinMode (LED5[five], OUTPUT);}

for (int six = 0; six <= 6; six++){
pinMode (LED6[six], OUTPUT);}

for (int seven = 0; seven <= 3; seven++){
pinMode (LED7[seven], OUTPUT);}

for (int eight = 0; eight <= 7; eight++){
pinMode (LED8[eight], OUTPUT);}

for (int nine = 0; nine <= 6; nine++){
pinMode (LED9[nine], OUTPUT);}
}

void loop(){
//for loopas are grouped in pair turning on the 7-segment then off it goes through the array and send high output to all the numbers in the
//array, and then send low output to all numbers in the array.
for(int A = 0; A <= 5; A++){
digitalWrite (LED0[A], HIGH);}
delay(1000);
for(int B=0; B <= 5; B++){
digitalWrite (LED0**,LOW);}**
for(int C = 0; C <= 1; C++){
digitalWrite (LED1
```c
**, HIGH);}
delay(1000);
for(int D=0; D <= 1; D++){
digitalWrite (LED1[D],LOW);}

for(int E = 0; E <= 4; E++){
digitalWrite (LED2[E], HIGH);}
delay(1000);
for(int F=0; F <= 4; F++){
digitalWrite (LED2[F],LOW);}

for(int G = 0; G <= 4; G++){
digitalWrite (LED3[G], HIGH);}
delay(1000);
for(int H=0; H <= 4; H++){
digitalWrite (LED3[H],LOW);}

for(int I = 0; I <= 3; I++){
digitalWrite (LED4[I], HIGH);}
delay(1000);
for(int J=0; J <= 3; J++){
digitalWrite (LED4[J],LOW);}

for(int K = 0; K <= 4; K++){
digitalWrite (LED5[K], HIGH);}
delay(1000);
for(int L=0; L <= 4; L++){
digitalWrite (LED5[L],LOW);}

for(int M = 0; M <= 5; M++){
digitalWrite (LED6[M], HIGH);}
delay(1000);
for(int N=0; N <= 5; N++){
digitalWrite (LED6[N],LOW);}

for(int O = 0; O <= 2; O++){
digitalWrite (LED7[O], HIGH);}
delay(1000);
for(int P=0; P <= 2; P++){
digitalWrite (LED7[P],LOW);}

for(int Q = 0; Q <= 6; Q++){
digitalWrite (LED8[Q], HIGH);}
delay(1000);
for(int R=0; R <= 6; R++){
digitalWrite (LED8[R],LOW);}

for(int S = 0; S <= 5; S++){
digitalWrite (LED9[S], HIGH);}
delay(1000);
for(int T=0; T <= 4; T++){
digitalWrite (LED9[T],LOW);}

}**
```

In setup(), you only need to set all your pins to outputs once. Since “8” lights all the segments, you can just use the one loop for LED8.

In general, you make code smaller by recognizing common functions. In your case, the code for each digit is very similar, but your arrays of which segments are used are different sizes, so you’re having to handle each digit separately. Instead, you can “waste” a bit of space in the list of segments so that every digit has 7 pins, and then you can use a two dimensional array, which lets you make the code for any digit be the same as any other digit:

#define NOPIN 99  // magic value indicating a pin not used in a digit.

int LEDS[7][10] =   {  // 10 digits, 7 segments in each digit
  {8,9,10,11, 12,NOPIN,NOPIN},  // zero
  {7,10,NOPIN, NOPIN,NOPIN, NOPIN},  // one
     : //etc
};
displayDigit(int d, pinstate)
{
   for (int i=0; i < 7; i++) {     // for each possible segment
     if (LEDS[i][d] != NOPIN) {  // see if that segment exists
        pinMode(LEDS[i][d], pinstate);  // and set on or off.
     }
}
loop()
{
   for (int num=0; num < 10; num++) {
      displayDigit(num, HIGH); // turn on the digit
      delay(1000);
      displayDigit(num, LOW); // turn off the digit
  } // next digit
}

Later you can notice that the information fits in bytes rather than ints, or even in single bits, which will save quite a bit of ram space.

Thank you for your knowledge i have reduced my 9 output loops to one output loop, and starting to research the two dimensional array, but the program you posted had syntax errors

the program you posted had syntax errors

Well, yeah. It's a hint, not tested code... :-)

ok it have tried to shorten my code, but i ran into an error which i do not know how to deal with could somebody help.


int LEDzero[5] = { 8,9,10,11,12};
int LEDone[2] = { 7,10};
int LEDtwo[5] = { 8,9,10,11,13};
int LEDthree[5] = { 7,8,10,11,13};
int LEDfour[4] = { 7,10,12,13};
int LEDfive[5] = { 7,8,11,12,13};
int LEDsix[6] = { 7,8,9,11,12,13};
int LEDseven[3] = { 7,10,11};
int LEDeight[7] = { 7,8,9,10,11,12,13};
int LEDnine[5] = { 7,10,11,12,13};
int PINS[8] = { 7,8,9,10,11,12,13};
int size[10] = { 4,1,4,4,3,4,5,2,6,4};
char THELED[10] = { ‘LEDzero’, ‘LEDone’, ‘LEDtwo’, ‘LEDthree’, ‘LEDfour’, ‘LEDfive’,‘LEDsix’,‘LEDseven’,‘LEDeight’,‘LEDnine’};
int C ;
int D ;

void setup(){
for (int zero = 0; zero <= 8; zero++){
pinMode (PINS[zero], OUTPUT);}
for(int C = 0; C <= 9; C++);
for(int D = 0; C <= 9; C++);
}

void loop(){
for(int A = 0; A <= size

; A++){
digitalWrite (THELED[D][A], HIGH);}
delay(1000);
for(int B=0; B<= size[C]; B++){
digitalWrite (THELED[D][B],LOW);}
}


------------------------------------------------------------------------------ 


the error is 
In function 'void loop()':
error: invalid types 'char[int]' for array subscript
char* THELED[10] = { 'LEDzero', 'LEDone', 'LEDtwo', 'LEDthree', 'LEDfour', 'LEDfive','LEDsix','LEDseven','LEDeight','LEDnine'}; 

// These ^^^^ should all be enclosed in double quotes and the type is "char*"

char* THELED[10] = { "LEDzero", // etc

Actually, for this application, you don't really want the names of the LEDnnn arrays in THELED, because the names don't do you any good at run-time. You want what C calls POINTERS to those arrays.

int *THELED[10] = {LEDzero, LEDone, LEDtwo, LEDthree, LEDfour, LEDfive, LEDsix, LEDseven, LEDeight, LEDnine };

"int *" is a type designation that tells the compiler that each element of the array is a pointer to another array of integers. Since arrays and pointers mix nicely together in C, this is probably what you want to have happen. The compile errors go away...

Good job, BTW. Setting it up as you did offers an insight to sound thinking about how you wanted it to work. Not entirely correct; but sound nevertheless. And rather less uncertain that trying to initialize two dimension at one time in a massive initialization statement.

Oops - shoulda read on - I saw the "char" and the single quotes. :-[

I would like to thank all you guys for your help i woke up this morning before school and started editing my program, in my class i was never know for having short programs :), teacher decides he wanted me to learn how to shorten my program so i worked on it all weekend and it was worth it. :slight_smile: even though my program isnt looping properly right now cause i just woke up and couldnt tune the program yet i would proply get it working.