Go Down

Topic: LED clock with different time speed (Read 4398 times) previous topic - next topic

Graynomad

#45
Sep 21, 2010, 04:38 pm Last Edit: Sep 21, 2010, 04:38 pm by graynomad Reason: 1
Add the yellow line

Code: [Select]
  // 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;
  }
Rob Gray aka the GRAYnomad www.robgray.com

sinuslinus

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.

Graynomad

Comment out these two lines

Code: [Select]
[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]

Rob Gray aka the GRAYnomad www.robgray.com

sinuslinus

#48
Sep 21, 2010, 05:03 pm Last Edit: Sep 21, 2010, 05:04 pm by sinuslinus Reason: 1
counting properly at 10 Hz , still no change with button.

I tried both with and without
Code: [Select]
timing = TRUE;

as you earlier post and both and without
Code: [Select]
if (ticks == 0) {

Graynomad

#49
Sep 21, 2010, 05:16 pm Last Edit: Sep 21, 2010, 06:15 pm by graynomad Reason: 1
Put that lot back, it should look like this

Code: [Select]
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);  
     }
  }  
Rob Gray aka the GRAYnomad www.robgray.com

Graynomad

#50
Sep 21, 2010, 05:39 pm Last Edit: Sep 21, 2010, 06:00 pm by graynomad Reason: 1
Found two more bugs. Change the two settings of button state from

Code: [Select]

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


to

Code: [Select]

        button_state = HIGH;
        button_state = LOW;



Can you post the entire code you have, I'm worried we're singing from different hymn sheets.
Rob Gray aka the GRAYnomad www.robgray.com

sinuslinus

#51
Sep 21, 2010, 06:31 pm Last Edit: Sep 21, 2010, 06:32 pm by sinuslinus Reason: 1
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.

Code: [Select]
#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);
}


Graynomad

#52
Sep 21, 2010, 06:39 pm Last Edit: Sep 21, 2010, 06:45 pm by graynomad Reason: 1
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

Code: [Select]
  } 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

Code: [Select]
  } 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.
Rob Gray aka the GRAYnomad www.robgray.com

sinuslinus

Quote
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
Code: [Select]
//timing = TRUE;
  if (timing == FALSE) {
     digitalWrite(latchpin, LOW);


Graynomad

#54
Sep 22, 2010, 03:48 am Last Edit: Sep 22, 2010, 06:31 am by graynomad Reason: 1
I sent you a PM, can you email me all the current code?

Rob Gray aka the GRAYnomad www.robgray.com

Graynomad

#55
Sep 22, 2010, 07:22 pm Last Edit: Sep 22, 2010, 07:23 pm by graynomad Reason: 1
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
  • ]);
       digitalWrite(latchpin, HIGH);  
    }
Rob Gray aka the GRAYnomad www.robgray.com

Graynomad

1:30AM here, gotta hit the sack. Email me with results from last version.
Rob Gray aka the GRAYnomad www.robgray.com

sinuslinus

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.

Code: [Select]
#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
Code: [Select]
#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
Code: [Select]
// 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

Go Up