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?
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);
}
}
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
Anyway this code works
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);
}
}