Timer on 16x2 LCD display triggered by serial

Hi guys,

I am trying to set up a timer with the serial connection. I am programming an ATTiny44a with an USBtinyISP. The idea is to start timing when there is any data being sent over serial (as soon as anything is typed in the terminal). I have something wrong going on with the while loop in the void loop, but can't figure out what.
What this code does is just prints "00:00:00"

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>          // lib. for LCD
int seconds = 0;                     // count seconds                 
int minutes = 0;                     // count minutes
int hours = 0;                      // count hours
LiquidCrystal lcd(5,4,3,2,1,0);  // pins connected to LCD

SoftwareSerial mySerial(7,8);

void setup()
{lcd.begin(16,2);   
lcd.print("Elapsed Time");// open the LCD
mySerial.begin(9600);
}
void loop()
 {if(mySerial.available() > 0)  
 { char command = mySerial.read(); 

 
 while (command != 0)
{
 lcd.setCursor(0,1);   
 if (hours < 10) {
    lcd.print("0");
    lcd.print(hours);
  }

else lcd.print (hours);
 lcd.print (":");
 if ( minutes == 60)                 // if minute count is 60 
 {
   delay (32);                      //  fine tune timing
   minutes = 0;                      // reset minute count
   hours ++;                           // add +1 hour
 }

 if (minutes < 10) {
    lcd.print("0");
    lcd.print(minutes);
  }
  else
 lcd.print (minutes);
 lcd.print (":");
 if (seconds < 10) {
    lcd.print("0");
    lcd.print(seconds);
  }
  else
 lcd.print (seconds);

 if (seconds == 60)                    // if seconds = 60 do this operation
 {
   minutes ++;                       // add 1 to minute count
   seconds = 0;                      // reset second count
   delay (58);                      // changes ms per min
 } 

if 
 (seconds < 60)                   // do this oper. 59 times   
 {
   delay (988);                     // changing by one = 60 ms a min
   seconds ++;                       // add 1 to Scount
}

}


}
 }

I appreciate your help.

So what you did was basically...

  • time each part of your loop
  • set a delay so that said time + the delay equals 1 sec, 1 min, 1 hour

?

Am I correct?

Instead of giving you an answer, I'm going to ask you a question.
Is this be best way to realize such project?

I mean, why not use millis()?
Why not use an RTC?

First off. Why not press ctrl-T to format your code?
And make it look readable before posting.

Like Ms Fu, I would think life would be easier with millis().

But with your present arrangement, you will loop forever in the while (command != 0) { ... }
You have no escape !

This is a very good reason for viewing indented code.

David.

Hi Nicola and David,

Thanks for responses.
My answers: I am at the 'absolute beginner' level so I am doing what I can. Sorry but I've not been able to put together anything more decent (including using millis). This code (without the serial) has worked. And at this point, I would event be happy to have what is already there, but just making it work with the serial.

Did I format it right this time btw?

#include <LiquidCrystal.h>          // lib. for LCD
int seconds = 0;                     // count seconds                 
int minutes = 0;                     // count minutes
int hours = 0;                      // count hours
LiquidCrystal lcd(5,4,3,2,1,0);  // pins connected to LCD

void setup()
{
 lcd.begin(16,2);   
  lcd.print("Elapsed Time");// open the LCD
   
}
void loop()
 {
 lcd.setCursor(0,1);   
 // sets cursor to 3rd line
 if (hours < 10) {
    lcd.print("0");
    lcd.print(hours);
  }

else lcd.print (hours);
 lcd.print (":");

 if ( minutes == 60)                 // if Mcount is 60 do this operation
 {
   delay (32);                      // good place to fine tune timing
   minutes = 0;                      // reset Mcount
   hours ++;
 }

 if (minutes < 10) {
    lcd.print("0");
    lcd.print(minutes);
  }
  else
 lcd.print (minutes);
 lcd.print (":");
 if (seconds < 10) {
    lcd.print("0");
    lcd.print(seconds);
  }
  else
 lcd.print (seconds);

 if (seconds >59)                    // if do this operation
 {
   minutes ++;                       // add 1 to minutes
   seconds = 0;                      // reset seconds
   delay (58);                      // changes ms per min
 } 

 if 
 (seconds < 59)                   
   // to count the seconds
 {
   delay (988);                     // changing by one = 60 ms a min
   seconds ++;                       // add 1 to seconds
}
 }

Thanks for your time.

It seems like a nice code. Quite intricate but surely it should work.
As David suggested, the problem is not the display, but the serial input triggering the timer.

Simply Serial.print it on your pc screen and you'll see that it doesn't work like it's supposed to.

Ps... are you sure that mySerial.read() is a char type?
It's quite odd (but it might be true, depending on the SoftwareSerial.h library you are using).

Ps: in insist on suggesting you that a simple millis() will give you the exact same result in just 1 line of code. Also, it would be more accurate.

Pps

david_prentice:
Like Ms Fu...

yay, I'm a lady

No, seriously, I'm not :smiley: