2

I have tried everything but I can't get my binary clock to display correctly. The diagram and code are here: Binary Clock | Electronics Lab.

The hardware is based off this (it also explains the process that my program is trying to do): DIY: Binary Clock with Arduino. The problems lie in a, probably, simple error as my variable tempTime, which is used to convert the individual digits to their binary equivalent, does not change when I tell it to. All this occurs in the display function. (Sorry, I'm new Here and only a high school student)

While debugging it I realized that the only problem is the variable tempTime - the hardware and other functions work as they were intended. Can anyone help? (Sorry if this is not very well documented).

    int digitPins[4][4] = {{13,6,-1,-1},{12,7,3,1},{11,8,4,-1},{10,9,5,2}};
int time[4] = {0,0,0,0};
int steps[4]={1,2,4,8};
int tempTime = 0;
void setup(){
  for(int x = 0; x < 4; x++){
    for(int y =0; y<4; y++){
    pinMode(digitPins[x][y],OUTPUT);
    }
  }
    Serial.begin(9600);
    convertHour(12);
    convertMinute(57);
    Serial.println("The time is:");

  for(int z = 0; z < 4; z++){
    if(z == 2){
    Serial.print(":");
    }
  Serial.print(time[z]);
  }
  display();
}
void loop(){

}
void display(){
  int displayer[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
/*
0 - Hour
1 - Hour
2 - Minute
3 - Minute
4 - Hour(Total)
5 - Minute(Total)
*/

  int counter1 = 0;
  for(int i = 0; i < 4; i++){ //Loop for the individual digits
    tempTime = time[i];
    if(tempTime >= 10){
    tempTime = tempTime % 10;
    }
    Serial.print("\nBegining new iteration i = ");
    Serial.print("\nStarting with digit: ");
    Serial.print(tempTime);
    Serial.print(i);
    Serial.println("");
    for(int j = 3; j >= 0; j--){//Loop to determin the arrangement
      if(time[i]/steps[j] >= 1 && steps[j] > 0 && digitPins[i][j] > 0){
        Serial.print("\n");
        Serial.print(time[i]);
        Serial.print("/");
        Serial.print(steps[j]);
        Serial.print(" = ");
        Serial.print(time[i]/steps[j]);
        tempTime =tempTime - (time[i]*steps[j]);
        displayer[counter1] = digitPins[i][j];
        Serial.print("\nAdded: ");
        Serial.print(digitPins[i][j]);
        counter1++;
      }
    }

  }
  clear();
  for(int a = 0; a < 13; a++){
  digitalWrite(displayer[a],HIGH);
  }
}
void clear(){
  for(int x = 0; x < 4; x++){
    for(int y =0; y<4; y++){
    digitalWrite(digitPins[x][y],LOW);
    }
  }
}
  void convertHour(int value){
    int digits[] = {0,0};
    if(value >= 10){
        debug("Bigger than 10");
        if(value >= 20){
            digits[0] = 2;  
        }else{
            digits[0] = 1;
        }
    }
    digits[1] = value - (digits[0] * 10);
    //String hour = digits[0]+""+digits[1];
    //Serial.print(digits[0]);
    //Serial.print(digits[1]);
    time[0] = digits[0];
    time[1] = digits[1];
  }
void convertMinute(int value){
  int digits[2] = {0,0};
  int val = value;
  if(value >= 10){
    digits[0] = val/10;
  }
  digits[1] = value - (digits[0] * 10);
  Serial.print("\n");
  Serial.print(digits[0]);
  Serial.print(digits[1]);
  time[2] = digits[0];
  time[3] = digits[1];
}
  void debug(String value){
    boolean de = true;
    if(de){
    Serial.println(value);
    }
  }
5
  • Serial and LED on the same pin? Feb 11, 2016 at 21:12
  • Calculate the power requirements for all these LEDs (and 120 ohm). Will the Uno be able to deliver that? Feb 11, 2016 at 21:42
  • 1
    We need more information. What should it do? What does it do? What have you tried?
    – Craig
    Feb 11, 2016 at 22:46
  • Very Sorry wrote that post in a hurry. The hardware is based off this (it also explains the proccess that my program is trying to do): danielandrade.net/2008/07/15/binary-clock-with-arduino The problems lie in a probably simple error as my variable tempTime which is used to convert the individual digits to their binary equivalent does not change when I tell it to. All this occurs in the display function. (Sorry New Here and only a highschool student)
    – Cam Steele
    Feb 11, 2016 at 23:31
  • 1
    Can you edit your question and paste the code into it please? This is looking like a link-only question. Format the code using the code formatting markdown (four leading spaces). For help see Markdown help. You should be able to do this by selecting the code and pressing Ctrl+K to have your browser do this for you. If that link goes down the question, and any following answer, will be meaningless.
    – Nick Gammon
    Feb 12, 2016 at 2:29

1 Answer 1

0

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));
}
2
  • Thank you so much, I'm relatively new to c++ with a background in js and php, still trying to get the hang of it.
    – Cam Steele
    Feb 12, 2016 at 16:16
  • Well, the bitwise operators of C and C++ are the same you already know from JavaScript and PHP. Feb 12, 2016 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.