Skip to main content
Arduino

Return to Answer

+ Simpler display().
Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81

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.

Edit: As your display() function is way too complex for what it does, I did not resist the temptation to simplify it:

void display() {
 for (int i = 0; i < 4; i++)
 for (int j = 0; j < 4 && digitPins[i][j] >= 0; j++)
 digitalWrite(digitPins[i][j], time[i] & (1<<j));
}

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.

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.

Edit: As your display() function is way too complex for what it does, I did not resist the temptation to simplify it:

void display() {
 for (int i = 0; i < 4; i++)
 for (int j = 0; j < 4 && digitPins[i][j] >= 0; j++)
 digitalWrite(digitPins[i][j], time[i] & (1<<j));
}
Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81

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.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /