595 binary countdown timer - weird issue?

hi i have pieced together a simple binary clock/timer using 2 74hc595 shift registers (NXP 74hc595n) and wire 6 led to the first 6 outputs of each register. i used this code:

int clockPin = 12; // SH_CP  Shift register clock pin : IC pin 11
int latchPin = 8; // ST_CP Storage register clock pin (latch pin) : IC pin 12
int dataPin = 11; // DS data pin : IC pin 14

int Seconds = 59;
int Minutes = 59;
int Hours = 23;
int timer = 1000;

void setup() {

  pinMode(dataPin, OUTPUT);       
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  
}
void loop() {

{
Seconds =Seconds -1;
if(Seconds <=0)
{
Minutes =Minutes -1;
Seconds =59;
} 
if( Minutes<=0)
{
Minutes =59;
Hours =Hours -1;
}
if (Hours < 0)
{
Hours =23;
}

}



digitalWrite(latchPin, LOW);
shiftOut(dataPin,clockPin,MSBFIRST, Minutes);
shiftOut(dataPin,clockPin,MSBFIRST, Seconds);// shifting out Seconds only.

digitalWrite(latchPin, HIGH);

delay(timer); // one second delay to make it work like a clock.
}

i made the timer count down to invert the leds to go high for it to look right… the problem is that it starts the count at 5 seconds and 4 minutes… how can this be?? maybe i need to use the LAST 6 outputs instead of the FIRST 6??

to clarify i grounded the last two outputs because they aren’t being used…

i’m a total noob so please explain your answer thoroughly…

// Søren Brun

You should always post your code in [code]code tags[/code], not inline. You can do this using the </> button in the post or “Reply” windows. It’s not too late to edit your post and do this.
It makes it easier to read, and also easier to select and copy for testing in an IDE or simulator.


First thing I notice - you’re using ‘shiftOut()’ to send ‘int’ variables, but it only sends a ‘byte’. Since you’re sending them MSB first, you won’t be clocking out the values that you expect. On second thought, I guess the high byte will be ignored. (It’s midnight here - I think I’m just too tired to think clearly. :slight_smile: )

i grounded the last two outputs because they aren’t being used…

Do you mean that you hard-wired two of the shift register outputs to ground? If so, is that a good idea? What happens if you accidentally clock high values into these positions and latch it? Goodbye 74HC595.

"to clarify i grounded the last two outputs because they aren't being used.. "
Remove those. If they happen to go high, you will damage the chips.
You are expecting the first display to be
minutes = 59 = 0x3B = 00111011, inverted = 11000100, seen as 4
seconds = 58 = 0x3A = 00111010, inverted = 11000101, seen as 5
(since Seconds = Seconds -1 right at the start of loop. Set Seconds = 60 if you want 59 to be the first thing displayed)
Looks to me like you're getting what you programmed.
I'd suggest get rid of the inversion.

CrossRoads:
"to clarify i grounded the last two outputs because they aren't being used.. "
Remove those. If they happen to go high, you will damage the chips.

I was editing and said the same, but I think you beat me.

Maybe by a few minutes, if you believe the post times.

to be honest i'm not wuite sure what i am doing i'm finding it hard to grasp the concept of how the shift registers work..

i thought that if you output x=60 the shiftout function would then convert 60 into 8 bit binary code..

i will try to change seconds and minutes to 60 instead of 59 and remove the "unused output wires to ground" and see how it works..

i tried this and now the seconds start at four and the minutes at 3..


the hex code you mentioned is that the value for 59 or 60?

what i meant by inverted code is that when i write the counter like this

{
Seconds =Seconds +1;
if(Seconds >=59)
{
Minutes =Minutes +1;
Seconds =0;
}

a one is displayed as every led but the one light up

that's why i'm counting down instead of up

thanks for your replys :smiley:

EDIT: btw. the reason why i grounded the two pins i don't use was to avoid "floating" pins

okay now it kinda works... i changed the counter from counting down to counting up.. now it works but i would like to invert the outputs and make 0's into 1's and vice versa.. my understanding of coding is very limited hope some one can make this clear to me!

thank you!