Same value for global/local variable and parameter gives different results

Hi there!

I wrote a little method to fade LEDs with a shift register.

const byte bitsumme = 8;
boolean ist[bitsumme];
boolean soll[bitsumme];

void fade(int t, int td) {//todo: maxsize of t
  //fade from ist[] to soll[]
  word delaymatrix[(int)(t / td)][2]; //  ¯\_(ツ)_/¯
  for (int i = 1; i <= (int)(t / td); i++) {  //create delaymatrix
    delaymatrix[i][0] = (word)((1.0 - ((float)i / (int)((float)t / td))) * 1000.0 *td); //1=soll; 0=ist ¯\_(ツ)_/¯
    delaymatrix[i][1] = (word)(((float)i / (float)(int)((float)t / td)) * 1000.0 * td);//  ¯\_(ツ)_/¯

  }


  //shifting
  for (int k = 1; k < (int)(t / td); k++) { //Delaymatrix counter
    //write ist[]
    for (int u = bitsumme - 1; u >= 0; u--) {
      if (ist[u] == 0) PORTB &= ~(1 << 2);
      else PORTB |= (1 << 2);
      PORTB &= ~(1 << 4);
      PORTB |= (1 << 4);
    }
    PORTB &= ~(1 << 3);
    PORTB |= (1 << 3);
    delayMicroseconds(delaymatrix[k][0]);


    //write soll[]
    for (int u = bitsumme - 1; u >= 0; u--) {
      if (soll[u] == 0) PORTB &= ~(1 << 2);
      else PORTB |= (1 << 2);
      PORTB &= ~(1 << 4);
      PORTB |= (1 << 4);
    }
    PORTB &= ~(1 << 3);
    PORTB |= (1 << 3);
    delayMicroseconds(delaymatrix[k][1]);
  }
  //End of shifting

  for (int u = bitsumme - 1; u >= 0; u--) { //refresh ist[]
    ist[u] = soll[u];
  }
}

void loop() {
  for (int i = 0; i < bitsumme; i++) {
    soll[i] = 1;
    fade(2000,15);
  }
  delay(500);
  for (int i = bitsumme - 1; i >= 0; i--) {
    soll[i] = 0;
    fade(2000,15);
  
}

And it works like a charm
What bugs me is the parameter td, since this one will always be 15

But the method:

void fade(int t) {
  int td  = 15;
  word delaymatrix[(int)(t / td)][2]; //  ¯\_(ツ)_/¯
  for (int i = 1; i <= (int)(t / td); i++) {  //Delay matrix erstellen
    delaymatrix[i][0] = (word)((1.0 - ((float)i / (int)((float)t / td)) * 1000.0 * td); 
    delaymatrix[i][1] = (word)(((float)i / (float)(int)((float)t / td)) * 1000.0 * td);

  }
...
void loop() {
  for (int i = 0; i < bitsumme; i++) {
    soll[i] = 1;
    fade(2000);
  }
...

Acts very strage

and

int td  = 15;
void fade(int t) {
  word delaymatrix[(int)(t / td)][2]; //  ¯\_(ツ)_/¯
  for (int i = 1; i <= (int)(t / td); i++) {  //Delay matrix erstellen
    delaymatrix[i][0] = (word)((1.0 - ((float)i / (int)((float)t / td))) * 1000.0 * td); 
    delaymatrix[i][1] = (word)(((float)i / (float)(int)((float)t / td)) * 1000.0 * td);

  }
...

dosn’t act very strange, but flickers.

I don’t understand this behaviour, since I think all three variants should do exactly the same.
I have to admit that I was never taught programming and had a hard time finding the error with something like fade(array); didn’t work like expected.
But this strange behaviour is way above my understanding of coding :’(

Does someone have ideas why it does what it does?

oxyfrmbl: But this strange behaviour is way above my understanding of coding :'(

Does someone have ideas why it does what it does?

Well, it seems that you have built a level of complexity into the program, that is not too far above your abilities. It looks well organized, very concise. I think that it is not above your understanding of coding, it is above your understanding of troubleshooting and writing code in a way that other people can maintain it, that is weak.

I have to admit, I couldn't ask for more comments because they wouldn't be useful to me unless they were in English. But you do need a lot more, if for nobody else but yourself. You have familiarized yourself with it. But for anybody else to do that now, would be a huge effort (well, I suppose not for a Mensa genius). The comments will help you guide yourself. Imagine looking at it a year from now.

So that I don't appear to be completely unhelpful, I do suggest that you try to break the program down into smaller modules that you can test independently. Then it will be easier to identify the trouble spots. You can develop branches that are specifically tuned to eke out problems, by running it in a more diagnostic way.

word is a bastard type that should NEVER have been allowed to escape from Redmond. Don't use it.

Casting an int to an int is a waste of effort. Stop doing that.

Arrays should NOT be defined using non-const variables. Dynamically sizing an array as you are doing is going to cause problems. Making the relevant variables const might help. Making the array size static would definitely help.

Thanks aarg, but it's less spectacular than it looks. Actually it's simple percentage calculation or rule of three or whatever. I'm using a separate file for documentation since I need diagrams and human-readable formulas. I was just in a little hurry.