LED clock with different time speed

Back up a bit.

but it counts with both "if" in the code now but no action when button is pressed.

Is this still the case (without the new braces) and how fast does it count?

Now it acts as the previous counter ss:mm:hh

All it should do at present is count the same either way, just slower when the button is pressed.

Weird, it counts the same without the brackets. Wonder what I was looking at... So the brackets do nothing.

All it should do at present is count the same either way, just slower when the button is pressed.

No action when pressing button.

   if (timing == FALSE) {
 [glow]     digitalWrite(latchpin, LOW);
      for (int x = 0; x < 6; x++)
         shiftOut(datapin, clockpin, MSBFIRST, segdisp[digits[x]]);
      digitalWrite(latchpin, HIGH);  [/glow]  
 } else {
      if (ticks == 0) {
         digitalWrite(latchpin, LOW);
         for (int x = 0; x < 6; x++)
            shiftOut(datapin, clockpin, MSBFIRST, segdisp[digits[x]]);
         digitalWrite(latchpin, HIGH);  
      }
   }

If we are not timing the 10 seconds the yellow bit gets executed 10 times a sec.

If we are timing the other half runs and this tests for tick == 0, therefore it only runs every 10 times, ie every second.

What speed is it counting at?

No action when pressing button.

Do you mean it stops or that nothing changes?

Okay,

it counts 10th of seconds.
When I press the button nothing changes.

Change

   if (timing == FALSE) {

to

timing = TRUE;
   if (timing == FALSE) {

Does it now count at 1Hz?

Add the yellow line

   // if it's been 10secs since we started timing reset the flag
   if (millis () > time_but_pressed + 10 * 1000) {
 [glow]     button_state == HIGH;[/glow]      
      timing = FALSE;
   }

It counts at 1 Hz on two displays simultanously at first and then when it hits 10 it differ on number.
No change when pressing button.

Comment out these two lines

[glow] if (ticks == 0) {[/glow]         
   digitalWrite(latchpin, LOW);
         for (int x = 0; x < 6; x++)
            shiftOut(datapin, clockpin, MSBFIRST, segdisp[digits[x]]);
         digitalWrite(latchpin, HIGH);  
      [glow]}[/glow]

counting properly at 10 Hz , still no change with button.

I tried both with and without

timing = TRUE;

as you earlier post and both and without

 if (ticks == 0) {

Put that lot back, it should look like this

timing = TRUE;
   if (timing == FALSE) {
      digitalWrite(latchpin, LOW);
      for (int x = 0; x < 6; x++)
         shiftOut(datapin, clockpin, MSBFIRST, segdisp[digits[x]]);
      digitalWrite(latchpin, HIGH);  
   } else {
      if (ticks == 0) {
         digitalWrite(latchpin, LOW);
         for (int x = 0; x < 6; x++)
            shiftOut(datapin, clockpin, MSBFIRST, segdisp[digits[x]]);
         digitalWrite(latchpin, HIGH);  
      }
   }

Found two more bugs. Change the two settings of button state from

         button_state [glow]==[/glow] HIGH;
         button_state [glow]==[/glow] LOW;

to

         button_state = HIGH;
         button_state = LOW;

Can you post the entire code you have, I'm worried we're singing from different hymn sheets.

Haven't test run these last changes but here is what we have now. Will do later tonight so I guess that will be morning for you.

Can you double check those == and = because I have now change three == to = according to your last post.

#define SECONDS 4
#define MINUTES 2
#define HOURS   0


#define TRUE 1  // these may cause an error if already defined
#define FALSE 0
#define BUTTON 5 // push button on pin 5


unsigned int latchpin = 8;  // connect to pin 12 on the '595
unsigned int clockpin = 12; // connect to pin 11 on the '595
unsigned int datapin  = 11; // connect to pin 14 on the '595

unsigned int speed = 1000;    // used to control speed of counting
unsigned int ticks = 0;
unsigned int seconds = 0;
unsigned int minutes = 0;
unsigned int hours = 0;
unsigned int digits[6];
unsigned int button_state = HIGH;
unsigned long time_but_pressed;

unsigned int timing = FALSE;

unsigned int segdisp[10] = {63,6,91,79,102,109,125,7,127,111};

void setup() {

   pinMode(latchpin, OUTPUT);
   pinMode(clockpin, OUTPUT);
   pinMode(datapin, OUTPUT);

}

void loop(){
  {
   ticks++;
   if (ticks > 10) 
      ticks = 0;
      seconds++; 
    }
   if (seconds > 59) {
      seconds = 0;
      minutes++;
   }
   if (minutes > 59) {
      minutes = 0;
      hours++;
   }
   if (hours > 23) {
      hours = 0;
   }

  // detect falling edge of button
   // record the time and set flag to indicate we are timing for 10 secs
 if 
  (digitalRead (BUTTON) == LOW && button_state = HIGH); {
       time_but_pressed = millis();
       button_state = LOW;
        timing = TRUE;
   }
if
   // if it's been 10secs since we started timing reset the flag
    (millis () > time_but_pressed + 10 * 1000); {
      button_state = HIGH; 
      timing = FALSE;
   } 
   digits[SECONDS]   = seconds / 10;
   digits[SECONDS+1] = seconds % 10;
   digits[MINUTES]   = minutes / 10;
   digits[MINUTES+1] = minutes % 10;
   digits[HOURS]     = hours   / 10;
   digits[HOURS+1]   = hours   % 10;

   timing = TRUE;
   if (timing == FALSE) {
      digitalWrite(latchpin, LOW);
      for (int x = 0; x < 6; x++)
         shiftOut(datapin, clockpin, MSBFIRST, segdisp[digits[x]]);
      digitalWrite(latchpin, HIGH);  
   } else {
      if (ticks == 0) {
         digitalWrite(latchpin, LOW);
         for (int x = 0; x < 6; x++)
            shiftOut(datapin, clockpin, MSBFIRST, segdisp[digits[x]]);
         digitalWrite(latchpin, HIGH);  
      }
   }  
 


   delay (100);
}

Just check two of those "if"s, they are located pretty wierdly. Apart form that it looks OK.

It should now count slowly.

To test if the button is actually working replace

   } else {
      if (ticks == 0) {
         digitalWrite(latchpin, LOW);
         for (int x = 0; x < 6; x++)
            shiftOut(datapin, clockpin, MSBFIRST, segdisp[[glow]digits[x][/glow]]);
         digitalWrite(latchpin, HIGH);  
      }
   }

with

   } else {
      if (ticks == 0) {
         digitalWrite(latchpin, LOW);
         for (int x = 0; x < 6; x++)
            shiftOut(datapin, clockpin, MSBFIRST, segdisp[[glow]digitalRead (BUTTON)[/glow]]);
         digitalWrite(latchpin, HIGH);  
      }
   }

So we can see exactly what is being returned by the button. Presumably it should be 111111 when the button is not pressed and 000000 when it is.

Presumably it should be 111111 when the button is not pressed and 000000 when it is.

It is the opposite;
111111 when pressed and
000000 when not pressed.

The code posted counts at 1Hz to 59 on the two seconds displays but in a weird way. It is as I wrote before firstly simultaneously but then weird because the 10 second just counts to 6 (as if to 59).
11,22,33,44,55,06,17,28,39...

If I comment out timing = TRUE then it counts at 10 Hz properly up to 59

 //timing = TRUE;
   if (timing == FALSE) {
      digitalWrite(latchpin, LOW);

I sent you a PM, can you email me all the current code?

Still can't send email.

Your compiling problem:

unsigned int segdisp = {63,6,91,79,102,109,125,7,127,111};

to

unsigned int segdisp[10] = {63,6,91,79,102,109,125,7,127,111};

actuall ytake the entire code, who knows what else was stuffed up in the PM

#define SECONDS 4
#define MINUTES 2
#define HOURS 0

#define TRUE 1 // these may cause an error if already defined
#define FALSE 0
#define BUTTON 5 // push button on pin 5

unsigned int latchpin = 8; // connect to pin 12 on the '595
unsigned int clockpin = 12; // connect to pin 11 on the '595
unsigned int datapin = 11; // connect to pin 14 on the '595

unsigned int speed = 1000; // used to control speed of counting
unsigned int ticks = 0;
unsigned int seconds = 0;
unsigned int minutes = 0;
unsigned int hours = 0;
unsigned int digits[6];
unsigned int button_state = HIGH;
unsigned long time_but_pressed;

unsigned int timing = FALSE;

unsigned int segdisp[10] = {63,6,91,79,102,109,125,7,127,111};

void setup() {

pinMode(latchpin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(datapin, OUTPUT);

}

void loop(){
ticks++;

if (ticks > 9) {
ticks = 0;
seconds++;
if (seconds > 59) {
seconds = 0;
minutes++;
}
if (minutes > 59) {
minutes = 0;
hours++;
}
if (hours > 23) {
hours = 0;
}
}

digits[SECONDS+1] = seconds % 10; // 5 1s
digits[SECONDS] = seconds / 10; // 4 10s
digits[MINUTES+1] = minutes % 10; // 3 1s
digits[MINUTES] = minutes / 10; // 2 10s
digits[HOURS+1] = hours % 10; // 1 1s
digits[HOURS] = hours / 10; // 0 10s

if (digitalRead (BUTTON) == HIGH)
timing = TRUE;
else
timing = FALSE;

if (timing == FALSE) {
writeDigits(1);
} else {
if (ticks == 0) writeDigits(2);
}

delay (100);
}

void writeDigits (int xx) {
digitalWrite(latchpin, LOW);
for (int x = 0; x < 6; x++)
shiftOut(datapin, clockpin, MSBFIRST, segdisp[digits[x]]);
digitalWrite(latchpin, HIGH);
}

1:30AM here, gotta hit the sack. Email me with results from last version.

Rob helped me a lot with this one and I have it working now. It counts at 10 Hz 0-9 on all segments simultaneously and when you press the button it displays the time hh:mm:ss.

The last thing I want to finish this is to display the time 10 seconds after the button is pressed.
That is, quicker counter without button and with button it displays time at 1 Hz and should keep this for 10 seconds before going back to the quick counter.

This is the working code.

#define TRUE 1  // these may cause an error if already defined 
#define FALSE 0 
#define BUTTON 5 // push button on pin 5

#define SECONDS 4
#define MINUTES 2
#define HOURS   0


unsigned int latchpin = 8;  // connect to pin 12 on the '595 
unsigned int clockpin = 12; // connect to pin 11 on the '595 
unsigned int datapin  = 11; // connect to pin 14 on the '595

unsigned int ticks = 0;
unsigned int seconds = 0;
unsigned int minutes = 0;
unsigned int hours = 0;
unsigned int digits[6];
unsigned int button_state = HIGH;
unsigned long time_but_pressed;

unsigned int timing = FALSE;

unsigned int segdisp[10] = {63,6,91,79,102,109,125,7,127,111};

void setup() {

  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(datapin, OUTPUT);

}

void loop(){
  ticks++;

  if (ticks > 9) {
     ticks = 0;
     seconds++; 
     if (seconds > 59) {
        seconds = 0;
        minutes++;
     }
     if (minutes > 59) {
        minutes = 0;
        hours++;
     }
     if (hours > 23) {
        hours = 0;
       }
   }

   digits[SECONDS+1] = seconds % 10; // 5  1s
   digits[SECONDS]   = seconds / 10; // 4  10s
   digits[MINUTES+1] = minutes % 10; // 3  1s
   digits[MINUTES]   = minutes / 10; // 2  10s
   digits[HOURS+1]   = hours   % 10; // 1  1s
   digits[HOURS]     = hours   / 10; // 0  10s

   if (digitalRead (BUTTON) == HIGH) 
      timing = TRUE;
   else
      timing = FALSE;


   if (timing == FALSE) {
      writeDigits(ticks);
   } else {
      writeDigits1();
   }

   delay (100);
}

void writeDigits (int xx) {
   digitalWrite(latchpin, LOW);
   for (int x = 0; x < 6; x++)
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[xx]);
   digitalWrite(latchpin, HIGH);  
}

void writeDigits1 () {
   digitalWrite(latchpin, LOW);
   for (int x = 0; x < 6; x++)
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[digits[x]]);
   digitalWrite(latchpin, HIGH);  
}

Can someone with fresh eyes read it through, please.

Have tried to add this the delay but then there is no change when the button is pressed

#define TRUE 1  // these may cause an error if already defined 
#define FALSE 0 
#define BUTTON 5 // push button on pin 5

#define SECONDS 4
#define MINUTES 2
#define HOURS   0


unsigned int latchpin = 8;  // connect to pin 12 on the '595 
unsigned int clockpin = 12; // connect to pin 11 on the '595 
unsigned int datapin  = 11; // connect to pin 14 on the '595

unsigned int ticks = 0;
unsigned int seconds = 0;
unsigned int minutes = 0;
unsigned int hours = 0;
unsigned int digits[6];
unsigned int button_state = HIGH;
unsigned long time_but_pressed;

unsigned int timing = FALSE;

unsigned int segdisp[10] = {63,6,91,79,102,109,125,7,127,111};

void setup() {

  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(datapin, OUTPUT);

}

void loop(){
  ticks++;

  if (ticks > 9) {
     ticks = 0;
     seconds++; 
     if (seconds > 59) {
        seconds = 0;
        minutes++;
     }
     if (minutes > 59) {
        minutes = 0;
        hours++;
     }
     if (hours > 23) {
        hours = 0;
       }
   }

   digits[SECONDS+1] = seconds % 10; // 5  1s
   digits[SECONDS]   = seconds / 10; // 4  10s
   digits[MINUTES+1] = minutes % 10; // 3  1s
   digits[MINUTES]   = minutes / 10; // 2  10s
   digits[HOURS+1]   = hours   % 10; // 1  1s
   digits[HOURS]     = hours   / 10; // 0  10s


  // detect falling edge of button
  // record the time and set flag to indicate we are timing for 10 secs
  if (digitalRead (BUTTON) == LOW && button_state == HIGH); {
      time_but_pressed = millis();
      button_state = LOW;
      timing = TRUE;
  }

  // if it's been 10secs since we started timing reset the flag
  if (millis () > time_but_pressed + 10 * 1000); {
    button_state = HIGH;
    timing = FALSE;
  }



   if (timing == FALSE) {
      writeDigits(ticks);
   } else {
      writeDigits1();
   }

   delay (100);
}

void writeDigits (int xx) {
   digitalWrite(latchpin, LOW);
   for (int x = 0; x < 6; x++)
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[xx]);
   digitalWrite(latchpin, HIGH);  
}

void writeDigits1 () {
   digitalWrite(latchpin, LOW);
   for (int x = 0; x < 6; x++)
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[digits[x]]);
   digitalWrite(latchpin, HIGH);  
}

Also tried to change the button part to this

 // detect falling edge of button
  // record the time and set flag to indicate we are timing for 10 secs
  if (digitalRead (BUTTON) == HIGH && button_state == LOW); {
      time_but_pressed = millis();
      button_state = HIGH;
      timing = TRUE;
  }

  // if it's been 10secs since we started timing reset the flag
  if (millis() > time_but_pressed + 10 * 1000); {
    button_state = LOW;
    timing = FALSE;
  }

/ Linus