code problems..

hey guys,
so i'm trying to create a simple clock using this code..

void loop(){
  char time[9]={hour,hour2,':',minute,minute2,':',second,second2};
    Serial.print(hour);Serial.print(hour2);Serial.print(':'); 
    Serial.print(minute);Serial.print(minute2);Serial.print(':');
    Serial.print(second);Serial.println(second2); 
  PrintText(old_time,136,108,1);
  PrintText(time,136,108,8);
  
  if(second2>9){
    second2=0; 
    second++;
  }
  if(second>7){
    second=0; 
    minute2++;
  }
  if(minute2>9){
    minute2=0; 
    minute++;
  }
  if(minute>7){
    minute=0; 
    hour2++;
  }
  if(hour2 >9){
    hour2=0; 
    hour++;
   }
   second2++;
  for(byte g=0; g<10; g++){
    old_time[g] = time[g];
  }
  
  ticks = millis();
  if(ticks-old_ticks < 1000-5) k+=5;
  if(ticks-old_ticks >1000+5) k-=5;
  delay(k);
  old_ticks = ticks;
}

i've spent the last 2 hours trying to figure out why it doesnt work.. please help!
this is the output from the serial monitor..

00:00:0204
00:00:0204
00:00:0204
00:00:0204
00:00:0204
00:00:0204
00:00:0204
00:00:0204
00:00:0204
00:00:0204
00:00:0204

Moderator edit: Quote problems swapped for code problems. PLEASE POST ALL YOUR CODE

I suggest you just hold hours, minutes and seconds in separate integer variables and use snprintf() to format them to a string when you want to print them.

I also suggest you don't use delay to control the speed of your sketch. Check the value of millis() and when it shows that 1000 milliseconds have elapsed, increment your second counter:

unsigned long lastTime = 0;
const unsigned long ONE_SECOND = 1000;
...
if(millis() - lastTime >= ONE_SECOND)
{
    // one second has elapsed
    handleElapsedSecond();
    lastTime += ONE_SECOND;
}

Here, handleElapsedSecond() would contain whatever logic you need to calculate the new time and update the display etc.

#include <displayClient.h>
long old_ticks,ticks;
char old_time[8];
int k = 900;
byte second=0,second2=0,minute=0,minute2=0,hour=0,hour2=0;
void setup(){
Serial.begin(9600);
fillScreen(1);
setTextSize(4);
}

as requested this is the missing part of the code..

PeterH thanks for your suggestion, I'll try that code out. I still would like to understand why this code doesn't work.. it's unsettling since it looks logically consistent..

If you want to know what is wrong with the original code then you need to post it all and not just a snippet!

We need to see setup and the data dec's.

  if(second>7){
    second=0; 
    minute2++;
  }

7 is 8 or more and when where there 80 plus mins per hour!

Mark

I have already posted the entire code.. my previous post had the missing data declarations and setup(). thanks for the remark on the >7 that somehow slipped by unnoticed..

so i modified the code but there is still a problem the now the _second is stuck at the value of 21 and i have no idea why or how..

//#include <serial_tftClient.h>
unsigned long lastTime = 0;
const unsigned long ONE___second = 1000;
char old_time[9];
char time[9];

byte ___second=0,second2=0,minute=0,minute2=0,hour=0,hour2=0;

void setup(){
Serial.begin(9600);
// fillScreen(1);
// setTextSize(4);
}
void loop(){

// PrintText(old_time,136,108,1);
// PrintText(time,136,108,8);

if(millis() - lastTime >= ONE___second)
{
handleElapsedSecond();
lastTime += ONE___second;

}
}
void handleElapsedSecond(){

if(second2>9){
second2=0;
___second++;
}
if(___second>5){
___second=0;
minute2++;
}
if(minute2>9){
minute2=0;
minute++;
}
if(minute>5){
minute=0;
hour2++;
}
if(hour2 >9){
hour2=0;
hour++;
}

time[0] = hour;
time[1] = hour2;
time[3]= ':';
time[4] = minute;
time[5] = minute2;
time[6] = ':';
time[7] = ___second;
time[8] = second2;
time[9] = '\0';
// String fuckThisShit = String(*time);
// for(byte g=0; g<10; g++){
// old_time[g] = time[g];
// }
// Serial.print(hour);Serial.print(hour2);Serial.print(':');
// Serial.print(minute);Serial.print(minute2);Serial.print(':');
// Serial.print(___second);Serial.println(second2);
Serial.println(time);
second2++;
// return time;
}

//}

00:35:216
00:36:217
00:37:218
00:38:219
00:39:2110
00:40:211
00:41:212
00:42:213
00:43:214
00:44:215
00:45:216
00:46:217
00:47:218
00:48:219
00:49:2110
00:50:211
00:51:212
00:52:213
00:53:214
00:54:215
00:55:216
00:56:217
00:57:218
00:58:219
00:59:2110
01:00:211
01:01:212

so far i've discovered that if i remove the char array problem goes away, but i need to put those numbers in a string/char array.. what can i do to fix this?

this is driving me crazy.. i can't figure it out.. my char array doesn't store any of the values i'm assigning to it, why is that? if i Serial.print the byte var second,min,hour,..etc it prints if i add those as elements of the array and print the array nothing prints!

man this is diving me insane, so i added 0 as u said to the element before i print it but nothing happens

time[0] = hour;
time[1] = hour2;
time[2] = ':';
time[3] = minute;
time[4] = minute2;
time[5] = ':';
time[6] = ___second;
time[7] = second2;
time[8] = '\0';
for(byte p =0; p<8; p++){
time[p] += 0;
Serial.print(':');
Serial.print(time[p]);
}
Serial.println();

motion86:
man this is diving me insane, so i added 0 as u said to the element before i print it but nothing happens

time[0] = hour;
time[1] = hour2;
time[2] = ':';
time[3] = minute;
time[4] = minute2;
time[5] = ':';
time[6] = ___second;
time[7] = second2;
time[8] = '\0';
for(byte p =0; p<8; p++){
time[p] += 0;
Serial.print(':');
Serial.print(time[p]);
}
Serial.println();

:::::::::
::::::::::
::::::::::
::::::::::
:

only the freaking : print :frowning: not to mention that if i do Serial.print(time); nothing at all prints
and this is if i print the numbers not as elments of the array..

Serial.print(hour);Serial.print(hour2);Serial.print(':');
Serial.print(minute);Serial.print(minute2);Serial.print(':');
Serial.print(___second);Serial.println(second2);

00:03:31
::::::::::

omg man that worked!!!!! thank you so much u saved me from unbearable agony!