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