In your "Loop to determin the arrangement", you are erroneously using
time[i]
in several places where you mean tempTime
. Also, to update
tempTime
you only have to subtract steps[j]
. With these fixes, your
loop becomes:
for(int j = 3; j >= 0; j--){//Loop to determin the arrangement
if(tempTime/steps[j] >= 1 && steps[j] > 0 && digitPins[i][j] > 0){
Serial.print("\n");
Serial.print(tempTime);
Serial.print("/");
Serial.print(steps[j]);
Serial.print(" = ");
Serial.print(tempTime/steps[j]);
tempTime = tempTime - steps[j];
displayer[counter1] = digitPins[i][j];
Serial.print("\r\nAdded: ");
Serial.print(digitPins[i][j]);
counter1++;
}
}
Now, if you learn a little bit about how binary numbers work, and about the binary operators in C and C++, you can rewrite your loop in a simpler form:
for (int j = 3; j >= 0; j--) {
int weight = 1 << j; // 2 raised to the power j
if (tempTime & weight) { // if bit j of tempTime is set
displayer[counter1++] = digitPins[i][j];
Serial.print("Added bit ");
Serial.print(j);
Serial.print(", weight ");
Serial.print(weight);
Serial.print(", output ");
Serial.println(digitPins[i][j]);
}
}
As a side note, it is considered good practice to print a new line at
the end of each string you print (using println()
), rather than at
the beginning.
- 45.1k
- 4
- 42
- 81