Clock

This is how I got a working clock. First I need to see if there was an Arduino library that could help me and I found one at http://playground.arduino.cc/Code/Time, which can be used to store and return time data. As I didn’t have any sort of external device to return the exact current time, like a Real-Time Clock (RTC) Chip, I would need a way for the time and date to be changed manually. But first I needed it to display a current time which was done by using the library mentioned earlier as it had a function to set a time with passed variables, so I set a default time of 12:00:00 and a date of 01/01/2016. The library also has very useful functions that could return the hour, minute, second, day, month and year values of the set date so using them I made a function that would display whatever time was set:

void printTimeDate() 
{ 
  lcd.setCursor(4, 0); 
  if (hour() < 10)
    lcd.print(0);
  lcd.print(hour());
  lcd.print(":");
  if (minute() < 10)
    lcd.print(0);
  lcd.print(minute()); 
  lcd.print(":"); 
  if (second() < 10) 
    lcd.print(0); 
  lcd.print(second()); 
  lcd.setCursor(1, 1); 
  lcd.print(dayShortStr(weekday())); 
  lcd.setCursor(5, 1); 
  if (day() < 10) 
    lcd.print(0); 
  lcd.print(day()); 
  lcd.print("/"); 
  if (month() < 10) 
    lcd.print(0); 
  lcd.print(month()); 
  lcd.print("/"); 
  lcd.print(year()); 
}

That provided a date and time  readout that looked exactly how I wanted it:

clock1

The next stage was to make the set date and time changeable. This required using the keypad buttons on the LED shield, which are readable through a single analogue data pin. So by checking if a button has been pressed and then determining which button was pressed allowed me to use the select button the draw another screen on the display, this one for setting the time. Another press of the button would take you to the set date screen. For each screen a press of the left or right buttons would change what value is being modified, indicated by a blinking cursor. The up and down buttons change that value within appropriate constraints. Pressing the select button for a third time returns to the time/date screen but after updating the set time/date with the manually entered values.

Now I have a functioning and modifiable clock I started work on the game part of the project.

Leave a comment