# Trying to create a BCD clock

**URL:** <https://discourse.processing.org/t/trying-to-create-a-bcd-clock/8560>\
**Category:** Coding Questions\
**Created:** [February 20, 2019, 12:03pm UTC](https://discourse.processing.org/t/trying-to-create-a-bcd-clock/8560 "2019-02-20T12:03:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Banansplitt](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Banansplitt](https://discourse.processing.org/u/Banansplitt)\
**Post date:** [February 20, 2019, 12:03pm UTC](https://discourse.processing.org/t/trying-to-create-a-bcd-clock/8560/1 "2019-02-20T12:03:48Z")

</div>

I’m trying o create a BCD clock, but something seems very off. The value of s = second(); returns a negative value, and the clock isn’t displaying correctly. What am I doing wrong?

```auto
int h;
int m;
int s;

void setup(){
  size(200, 350);
  noCursor();   
  strokeWeight(2);

  textAlign(CENTER);
}

void draw(){
  background(0);
  h = hour();
  m = minute();
  s = second();
  LED();
  
  fill(255);
  text( h + " : " + m + " : " + s, width/2, 17);
  
  
}

void LED(){
  rectMode(CENTER);
  for (int i=5;i>=0;i--) {
      
    fill(#B00000);
    if(h >= pow(2,i)) s-=pow(2,i);
    else fill(#FF0000);
    rect(50,height-(i*50)-50,50,50, 5);
    
    fill(#00B000);
    if(m >= pow(2,i)) s-=pow(2,i);
    else fill(#00FF00);
    rect(100,height-(i*50)-50,50,50, 5);
    
    fill(#0000B0);
    if(s >= pow(2,i)) s-=pow(2,i);
    else fill(#0000FF);
    rect(150,height-(i*50)-50,50,50, 5);
  }
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 20, 2019, 4:33pm UTC](https://discourse.processing.org/t/trying-to-create-a-bcd-clock/8560/2 "2019-02-20T16:33:44Z")

</div>

> [@Banansplitt](#):
>
> The value of s = second(); returns a negative value

No it doesn’t you are changing the value of s in the LED() function so this should be called after you have displayed the text value. Also you have used the copy-paste-change programming technique but forgot to make all the changes. Can you see the mistake in this line

> [@Banansplitt](#):
>
> if(h \>= pow(2,i)) s-=pow(2,i);

Anyway this code works

```auto
int h;
int m;
int s;

void setup() {
  size(200, 350);
  noCursor();   
  strokeWeight(2);

  textAlign(CENTER);
}

void draw() {
  background(0);
  h = hour();
  m = minute();
  s = second();
  fill(255);
  text( h + " : " + m + " : " + s, width/2, 17);
  LED();
}

void LED() {
  rectMode(CENTER);
  for (int i=5; i>=0; i--) {

    fill(#B00000);
    if (h >= pow(2, i)) h-=pow(2, i);
    else fill(#FF0000);
    rect(50, height-(i*50)-50, 50, 50, 5);

    fill(#00B000);
    if (m >= pow(2, i)) m-=pow(2, i);
    else fill(#00FF00);
    rect(100, height-(i*50)-50, 50, 50, 5);

    fill(#0000B0);
    if (s >= pow(2, i)) s-=pow(2, i);
    else fill(#0000FF);
    rect(150, height-(i*50)-50, 50, 50, 5);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Banansplitt](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Banansplitt](https://discourse.processing.org/u/Banansplitt)\
**Post date:** [March 6, 2019, 10:32am UTC](https://discourse.processing.org/t/trying-to-create-a-bcd-clock/8560/3 "2019-03-06T10:32:30Z")

</div>

Thank you 😅 I appreciate you helping me even though it was a mistake I should have seen myself
