Help with Code optimization

I am a newbie in coding.

My code is kinda big; 5,782 Bytes after compiling ( Sketch uses 5,782 bytes (17%) of program storage space. Maximum is 32,256 bytes.
Global variables use 204 bytes (9%) of dynamic memory, leaving 1,844 bytes for local variables. Maximum is 2,048 bytes.) which one is used on the board ? :confused:
Anyways … its currently a header file that gets included into the my sketch using #include < > tag. So any help with optimization would be appreciated.

Options :

  1. I could nest for () loops but how do i do that?

  2. Your recommendations… :stuck_out_tongue:

Numbers.h (9.55 KB)

int latchPin = 4;

Do you anticipate any pin numbers larger than 255? If not, use byte.

void number01(){
  for(int i=0; i < loopTimer; i++){
  digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, LSBFIRST, digits[0]);  
    digitalWrite(latchPin, 1);
    digitalWrite(gnd1, 0);
    digitalWrite(gnd2, 1);
    delay(5);
  digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, LSBFIRST, digits[1]);  
    digitalWrite(latchPin, 1);
    digitalWrite(gnd1, 1);
    digitalWrite(gnd2, 0);
    delay(5);
  }
}

Why do you need so many of these? Why not do this once?

void number(int aNumber){
  for(int i=0; i < loopTimer; i++){
  digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, LSBFIRST, digits[aNumber / 10]);  
    digitalWrite(latchPin, 1);
    digitalWrite(gnd1, 0);
    digitalWrite(gnd2, 1);
    delay(5);
  digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, LSBFIRST, digits[aNumber % 10]);  
    digitalWrite(latchPin, 1);
    digitalWrite(gnd1, 1);
    digitalWrite(gnd2, 0);
    delay(5);
  }
}

That alone would seriously cut down on the code size. And would probably be WAY easier to call from your code. I can’t imagine how the code must be structured if you have to call a different function for each number.

How about something like this:

void writeNumber(byte sel, byte what) {
    digitalWrite(gnd1, sel^1);
    digitalWrite(gnd2, sel);
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, LSBFIRST, what);  
    digitalWrite(latchPin, 1);
    digitalWrite(latchPin, 0);
}  

void numbers(int num) {
byte idx1 = num % 100;
byte idx2 = idx1 % 10;
    writeNumber(0, digits[idx1/10]);
    writeNumber(1, digits[idx2]);
}

I rearranged the accesses a bit, I think it makes more sense selecting the device before using it.
(If gnd1 and gnd2 are the enable bits for the latches)
Maybe the gnd1 gnd2 have to be swapped.

You write the same content 200 times to the latches, you do not trust them, do you?
Ok, that way it wont show that there is a problem with the chipselects.

Delta_G:

void number(int aNumber){

for(int i=0; i < loopTimer; i++){
  digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, LSBFIRST, digits[aNumber / 10]); 
    digitalWrite(latchPin, 1);
    digitalWrite(gnd1, 0);
    digitalWrite(gnd2, 1);
    delay(5);
  digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, LSBFIRST, digits[aNumber % 10]); 
    digitalWrite(latchPin, 1);
    digitalWrite(gnd1, 1);
    digitalWrite(gnd2, 0);
    delay(5);
  }
}




That alone would seriously cut down on the code size. And would probably be WAY easier to call from your code. I can't imagine how the code must be structured if you have to call a different function for each number.

I called them like this :

void loop() {
      number20();
      number19();
      number18();
      number17();
      number16();
      number15();
      number14();
      number13();
      number10();
      number09();
      number08();
      number07();
      number06();
      number05();
      number04();
      number03();
      number02();
      number01();
      number00();
  }

how do i call what u have written for me in the loop ?

like this :

void loop() {
   number();
  }

tried that and it said : void number(int aNumber){

too few arguments to function ‘void number(int)’

could u go through the code and explain what [aNumber / 10] & [aNumber % 10] is doing?

Thanks…

My project is controlling two seven segment displays individually to countdown from an assigned number. that being 10,20,30 or more ... hope that helps ...

i need to shift between each *SSD to show each number next to each other ( so fast the eye cant interpret. hence the 5 milliseconds delay ) for the countdown sequence.

SSD = Seven Segment Display

I would try my function.

void loop() {
static int value = 30;
   numbers(value);
   if (--value<0) {
     value = 30;
     delay(2000);
   }
   delay(100);
}

killerbee98:
My project … assigned…

What do you pay for doing your assignments?

killerbee98:
i need to shift between each *SSD to show each number next to each other ( so fast the eye cant interpret. hence the 5 milliseconds delay ) for the countdown sequence.

I think its is not the way you think it is. :wink:

I believe you have two serial latches enabled via gnd1 rsp gnd2.
These latches drive the 7 segments (and often a dot) directly.
So once written, the state stays until you change it.

Give us a detailed description of the used parts,
the connections, the schematic, the whole program
and more on request.

But guessing is fun, isn't it?

killerbee98:
I called them like this :

void loop() {

number20();
      number19();
      number18();
      number17();
      number16();
      number15();
      number14();
      number13();
      number10();
      number09();
      number08();
      number07();
      number06();
      number05();
      number04();
      number03();
      number02();
      number01();
      number00();
  }




how do i call what u have written for me in the loop ?

like this :



void loop() {
  number();
  }




tried that and it said : void number(int aNumber){

too few arguments to function 'void number(int)'

could u go through the code and explain what [aNumber / 10] & [aNumber % 10] is doing?

Thanks...
void loop() {

number(20);
number(19);
number(18);
// ...etc.

or better yet

void loop() {

  for (int i = 20; i >=0; i--){
      number(i);
   }
}

Delta_G:
or better yet

void loop() {

for (int i = 20; i >=0; i--){
      number(i);
  }
}

What will you see on the display?

My guess is: all segments will be (unevenly) lit.

Whandall:
What will you see on the display?

My guess is: all segments will be (unevenly) lit.

Why?

killerbee98:
could u go through the code and explain what [aNumber / 10] & [aNumber % 10] is doing?

The first one is division.

The second one is modulo.

The loop is hammering the numbers to the display as fast as possible,
which is a very high rate.

You will see each segment lit relativ to the frequency of occurrence
of this single segment in the representation of the displayed patterns.

The first digit will blend 0, 1 and a little 2, the second 0 to 9.

Whandall:
The loop is hammering the numbers to the display as fast as possible,
which is a very high rate.

You will see each segment lit relativ to the frequency of occurrence
of this single segment in the representation of the displayed patterns.

The first digit will blend 0, 1 and a little 2, the second 0 to 9.

Look again at his display functions. There are 100 iterations and 10ms worth of delay for each iteration. He should see each number for about a second.

Delta_G:
Look again at his display functions. There are 100 iterations and 10ms worth of delay for each iteration. He should see each number for about a second.

And where are his versions called in this code?

void loop() {

  for (int i = 20; i >=0; i--){
      number(i);
   }
}

Whandall… the code works by turning gnd1 off and gnd2 on and vice versa… what the code is doing as Delta_G said, There are 100 iterations and 10ms worth of delay for each iteration. meaning the for loop will repeat a hundred times thus creating a one second delay between each number … and 10ms delay, making the two digits show up at the same time.

Delta_G , could u explain what the code is doing ?

void number(int aNumber){
  for(int i=0; i < loopTimer; i++){
  digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, LSBFIRST, digits[aNumber / 10]); 
    digitalWrite(latchPin, 1);
    digitalWrite(gnd1, 0);
    digitalWrite(gnd2, 1);
    delay(5);
  digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, LSBFIRST, digits[aNumber % 10]); 
    digitalWrite(latchPin, 1);
    digitalWrite(gnd1, 1);
    digitalWrite(gnd2, 0);
    delay(5);
  }
}

i understand the for setup but what i don’t is this :
from what i understand … you’ve declared aNumber as an integer and devided it by 10. then for the second digit you modulo aNumber ( meaning ? ) by ten. aNumber has no array for it to do the countdown… and then why divide by ten ? and why modulo it?
the for loop in the void loop is understandable… i get it. but the setup is kinda cheesy… :S

killerbee98:
Whandall... the code works by turning gnd1 off and gnd2 on and vice versa... what the code is doing as Delta_G said, There are 100 iterations and 10ms worth of delay for each iteration. meaning the for loop will repeat a hundred times thus creating a one second delay between each number ... and 10ms delay, making the two digits show up at the same time.

You still don't get it.

You have write to the led latches only ONCE (per change in display)
the display will not change its state on its own.

You have to write the first digit (thats what the divison take care of).

You have to write the last digit (thats what the modulus does).

The whole concept of looping over loopTimer is senseless and
results probably from the incorrect chipselect (select after access).
If you keep looping you won't see the problem.

I already provided you with a code which should work,
but you keep clinging to your absolut senseless loop.

i understood the code... it took some time but i got it ... we void number and then create an argument anumber... then in the for loop the anumber takes the value of i and divides it by ten taking the tens and looping it until the next number ( i.e 99 through 90 / 10 = 9.9,9.8,9.7 ... 9.0 and so on ... ) . then what it does the second time for the second number ( the ones ) , is take the remaining number of what has been taken by the first shiftOut and use it to show on the second SSD ( Modulos. Finaly understood what its function is :smiley: ). so 99 through 90 / 10 = 9.9, 9.8, 9.7 and so but we take the fractions 9,8,7,6,5,4,3,2,1,0 and print them to the SSD based on my array digits. that's it ... WOW!

Thanks y'all for all your support ... i learned alot.

Whandall:
What do you pay for doing your assignments?

-257.6658 USD :stuck_out_tongue:

killerbee98:
i understood the code...
( i.e 99 through 90 / 10 = 9.9,9.8,9.7 ... 9.0 and so on ... )

You still do not understand the code.

99 through 90 / 10 = 9

The looping write to the leds is nonsense.

The only loop eventually nedded is the countdown loop.

The way your program uses gnd1 and gnd2 is probably wrong,
but as you dont tell us what is connected to the pins,
nobody knows.

If I were your instructor,
I would read this forum and issue Fs to the cheaters.

Whandall:
And where are his versions called in this code?

The one I wrote was exactly like all of his except for using a parameter for the number. Try reading the code first and THEN trying to be a smart-ass.

Whandall:
You still do not understand the code.

No, you still don't understand it and you're starting to make yourself look silly.

These are 7 segment displays. You can only show one segment at a time, so he is switching those ground pins to control which of the two displays is currently showing its number. Flicker back and forth fast enough and it looks like both displays are just on and showing their appropriate number.

If he were going to latch them once and just leave them on for a delay(1000) or something then he'd need twice as many output pins to drive two SSD at the same time.