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);
}
...
```

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?