LCD-Menu with time

24 Aug 2012

I am taking a time without the time-functions on an LCD display. The seconds and minutes etc., must continue.

This is the current code:

  1. include "mbed.h"
  2. include "TextLCD.h"

TextLCD lcd(p26, p25, p24, p23, p22, p20, p19, TextLCD::LCD20x4);

int hours = 23; int minutes = 58; int seconds = 59;

int day = 1; int month = 1; int year = 1;

Ticker rhythm;

void myrhythm() { seconds++; if(seconds>59) { minutes++; seconds=0; }

if(minutes>59) { hours++; minutes=0; }

if(hours>23) { hours=0; day++; }

if(day>31) { month++; day=1; }

if(month>12) { year++; month=1; } }

int main() { while (1) { lcd.locate(0,0); lcd.printf("%02d:%02d:%02d", hours, minutes, seconds); lcd.locate(0,1); lcd.printf("%02d/%02d/%02d", day, month, year);

rhythm.attach (&myrhythm, 1.0); } }

Please help me, I don't know what I must do. ;)

24 Aug 2012

Please use <<code>> and <</code>> tags around your code when posting. This will improve readability.

#include "mbed.h" 
#include "TextLCD.h" 

TextLCD lcd(p26, p25, p24, p23, p22, p20, p19, TextLCD::LCD20x4);
 
int hours = 23;
int minutes = 58;
int seconds = 59;
int day = 1;
int month = 1;
int year = 1;
 
Ticker rhythm;
 
void myrhythm() {
  seconds++;
  if(seconds>59) { minutes++; seconds=0; }
  
  if(minutes>59) { hours++; minutes=0; }
 
  if(hours>23) { hours=0; day++; }
 
  if(day>31) { month++; day=1; }
 
  if(month>12) { year++; month=1; }
}
 
int main() {
  while (1) {
    lcd.locate(0,0);
    lcd.printf("%02d:%02d:%02d", hours, minutes, seconds);
    lcd.locate(0,1);
    lcd.printf("%02d/%02d/%02d", day, month, year);
    rhythm.attach (&myrhythm, 1.0);
  }
}

The problem is that you attach the Ticker again and again inside the while(1) loop. This restarts it over and over again without ever giving it a chance to reach the first 1 sec timeout. Attach the ticker only once before you enter the while(1), it will then continue running and call the myrhythm function at 1 sec intervals.

Note that you could also include a wait in the endless while(1). It is updating the display at a very rate without any need. You only need to update it once per second.

int main() {
  rhythm.attach (&myrhythm, 1.0);

  while (1) {
    lcd.locate(0,0);
    lcd.printf("%02d:%02d:%02d", hours, minutes, seconds);
    lcd.locate(0,1);
    lcd.printf("%02d/%02d/%02d", day, month, year);
  }
}
24 Aug 2012

Thank you :) It works :)