MAX7219 with 7segments error in for() loop ! help !

hello guys,
i manage to make the MAX7219 work with Common Anode double digit 7-segment, i am using this library
https://github.com/deruiter/DCF77-Analyzer-Clock/tree/master/LedControl%20added%20COMMON%20ANODE%20display%20support

what is happening is that i am making a count up from 0 to 9999, so the count is stopping at 0001 and doesn’t go up, so i removed the for() loop and tested it without it to see if it can print any number i give to the variable i and it worked totally fine but when i add the for() loop it’s stopping at the 0001
this happened to me when i used a CC display and programed it but i found the solution but this solution is not working here so can anyone help please ?

here are the codes :

//We always have to include the library
#include <LedControl.h>

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1,true); //true for Common Anode

void setup() {
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,15);
  /* and clear the display */
  lc.clearDisplay(0);
  
  Serial.begin(9600);
}


/*
 This method will display the characters for the
 word "Arduino" one after the other on digit 0. 
 */

void displayCount(){
  int i;
  int ones;
  int tens;
  int hundreds;
  int thousands;
  
  for(i=0; i<10000; i++){
    ones=i%10;
    i=i/10;
    tens=i%10;
    i=i/10;
    hundreds=i%10;
    i=i/10;
    thousands = i;
    lc.setDigit(0, 0, ones, false);
    lc.setDigit(0, 1, tens, false);
    lc.setDigit(0, 2, hundreds, false);
    lc.setDigit(0, 3, thousands, false);
    delay(100);
      
  }
}


void loop() { 
  displayCount();

}

What is the value of i at the end of your for loop?

You are changing the loop index in the loop, you should make a copy of the index and change that.

Whandall: What is the value of i at the end of your for loop?

it is the value that it started with in the for loop between 0 and 10000

Whandall: You are changing the loop index in the loop, you should make a copy of the index and change that.

what do you ?

the same example worked for 2 digits but not with 4 :/ why ?!

do you mean i must take the i and put it at the top of the file sketch ?

firashelou: it is the value that it started with in the for loop between 0 and 10000 what do you ?

That's not true.

i contains the tenthousands of the index, which will stay zero.

Whandall:
That’s not true.

i contains the tenthousands of the index, which will stay zero.

what do you mean ?
example which i wrote on my notebook :
i = 1521
ones = i%10 = 1
i=i/10 = 152
tens = i%10=2
i=i/10=15
hundreds = i%10=5
i=i/10=1
thousands = i = 1

check this it works totally fine for 2 digits :

//We always have to include the library
#include <LedControl.h>

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);

void setup() {
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,15);
  /* and clear the display */
  lc.clearDisplay(0);
  
  Serial.begin(9600);
}


/*
 This method will display the characters for the
 word "Arduino" one after the other on digit 0. 
 */

void displayCount(){
  int i;
  int ones;
  int tens;
  
  for(i=0; i<100; i++){
    ones=i%10;
    tens = i/10;
    lc.setDigit(0, 0, ones, false);
    lc.setDigit(0, 1, tens, false);
    delay(100);
    
  }
}



void loop() { 
  displayCount();

}

In the code for the two digits you do not change i inside the loop, you act on copies, as I suggested to do.

And if you don't know how to/understand

'make a copy of an int and use that',

I'm very sorry, but I will not write your code, even though writing the denial consumes more time and keystrokes.

In your first version you have

    i=i/10;
...
    i=i/10;
...
    i=i/10;

that changes the value of i, doesn't it?

Whandall: In your first version you have

    i=i/10;
...
    i=i/10;
...
    i=i/10;

that changes the value of i, doesn't it?

yes of course it changes to the new value let's say it shrinks ^_^

i am using the method in this example from the section of controlling the 7-segment http://playground.arduino.cc/Main/LedControl

but why when i wrote it on paper it is right ?

can you give me a hint please ? because i made this post after many tries and frustrations which kept me no choice :/

you telling me to make a new int variable let's say int a and use it to store the new values ?

I would suggest using something like

for(i=0; i<10000; i++){
int number = i;

    number = number / 10;
...
    number = number / 10;
...
    number = number / 10;

or shorter

for(i=0; i<10000; i++){
int number = i;

    number /= 10;
...
    number /= 10;
...
    number /= 10;

and using number for the output of the digits.

Whandall:
I would suggest using something like

for(i=0; i<10000; i++){

int number = i;

number = number / 10;

    number = number / 10;

    number = number / 10;


or shorter

for(i=0; i<10000; i++){
int number = i;

number /= 10;

    number /= 10;

    number /= 10;


and using number for the output of the digits.

yes exactly as i thought not mess with the i because it’s for the for loop it worked fine thanks a lot :smiley: