Pages: [1]   Go Down
Author Topic: Timer on a KS0108 display  (Read 559 times)
0 Members and 1 Guest are viewing this topic.
Sweden, Malmö
Offline Offline
Full Member
***
Karma: 2
Posts: 200
Rooky
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just wanted to share a timer code that will display in the right lower corner.
Good to have if you want to keep track of the time passed from start up in a application.
Will show, hh:mm:ss
I did found that it only will count up to 27 hours something before it goes from 00:00:00 again, hopefully 27 hours timers will be good enough for people.
And yes, the code is far from perfect but it works.
The time code was found on the forum, thanks to the man that created that.
To get more informtion on the used ks0108 library and set up check:
http://www.arduino.cc/playground/Code/GLCDks0108

Code for clock math to be added before void setup
Code:
unsigned long current_millis_value = 0;
unsigned long previous_millis_value = 0;
unsigned long m = 0;
unsigned int seconds = 0;
unsigned int minutes = 0;
unsigned int hours = 0;

Code for clock math to be added in void loop
Code:
 current_millis_value = millis();
  m += current_millis_value - previous_millis_value;
  seconds += m / 1000;
  m = m % 1000;
  minutes += seconds / 60;
  seconds = seconds % 60;
  hours += minutes / 60;
  minutes = minutes % 60;
  hours = hours % 24;
  previous_millis_value = current_millis_value;

Code for displaying clock to be added in the void loop
Code:
GLCD.FillRect(110, 47, 14, 10, WHITE);
  
  if (seconds < 10){
  GLCD.GotoXY(117, 47);
  GLCD.PrintNumber(seconds);
  GLCD.GotoXY(110, 47);
  GLCD.Puts("0");
  }
  else {
  GLCD.GotoXY(110, 47);
  GLCD.PrintNumber(seconds);
  }
  
  GLCD.GotoXY(108, 46);
  GLCD.Puts(":");
  
  GLCD.FillRect(94, 47, 13, 10, WHITE);
  
  if (minutes < 10){
  GLCD.GotoXY(101, 47);
  GLCD.PrintNumber(minutes);
  GLCD.GotoXY(94, 47);
  GLCD.Puts("0");
  }
  else {
  GLCD.GotoXY(94, 47);
  GLCD.PrintNumber(minutes);
  }
  
  GLCD.GotoXY(92, 46);
  GLCD.Puts(":");
  
  GLCD.FillRect(78, 47, 13, 10, WHITE);
  
  if (hours < 10){
  GLCD.GotoXY(85, 47);
  GLCD.PrintNumber(hours);
  GLCD.GotoXY(78, 47);
  GLCD.Puts("0");
  }
  else {
  GLCD.GotoXY(78, 47);
  GLCD.PrintNumber(hours);
  }
Logged

Pages: [1]   Go Up
Jump to: