displaying time on LCD

Hello everybody!
I m writing a program and among it I have to show the time in an lcd. The time library works perfectly and shows the time in serial monitor but once I try to show the time in the LCD the time starts to run like crazy. This is my code:

/*
   AdjustClockTime sketch
   buttons on pins 2 and 3 adjust the time
 */

#include <Time.h>
#include <LiquidCrystal.h>
const int  btnForward = 2;  // button to move time forward
const int  btnBack = 3;     // button to move time back
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long  prevtime;   // when the clock was last displayed

void setup()
{
 lcd.begin(2, 16);
 
  digitalWrite(btnForward, HIGH);  // enable internal pull-up resistors
  digitalWrite(btnBack, HIGH);
  setTime(16,42,0,13,6,-30); // start with the time set to noon Jan 1 2011
  Serial.begin( 9600);
  Serial.println("ready");
  
}

void loop()
{
  prevtime = now();   // note the time
  while( prevtime == now() )    // stay in this loop till the second changes
  {
      // check if the set button pressed while waiting for second to roll over
     if(checkSetTime())
         prevtime = now();   // time changed so reset start time
  }
  digitalClockDisplay();
  lcddisplay();
}
    
// functions checks to see if the time should be adjusted
// returns true if time was changed
boolean checkSetTime()
{
int step;   // the number of seconds to move (backwards if negative) 
boolean isTimeAdjusted = false;  // set to true if the time is adjusted
  step = 1;   // ready to step forwards
  while(digitalRead(btnForward)== LOW)
  {
     adjustTime(step);
     isTimeAdjusted = true; // to tell the user that the time has changed
     step = step + 1; // next step will be bigger
     digitalClockDisplay(); // update clock   
     delay(100); 
  }
  step = -1;   // negative numbers step backwards
  while(digitalRead(btnBack)== LOW)
  {
     adjustTime(step);
     isTimeAdjusted = true; // to tell the user that the time has changed
     step = step - 1; // next step will be a bigger negative number
     digitalClockDisplay(); // update clock   
     delay(100); 
  }
  return isTimeAdjusted;  // tell the user if the time was adjusted
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
void lcddisplay(){
  lcd.print(second());
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Hello everybody!
I m writing a program and among it I have to show the time in an lcd. The time library works perfectly and shows the time in serial monitor but once I try to show the time in the LCD the time starts to run like crazy. This is my code:

/*
   AdjustClockTime sketch
   buttons on pins 2 and 3 adjust the time
 */

#include <Time.h>
#include <LiquidCrystal.h>
const int  btnForward = 2;  // button to move time forward
const int  btnBack = 3;     // button to move time back
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long  prevtime;   // when the clock was last displayed

void setup()
{
 lcd.begin(2, 16);
 
  digitalWrite(btnForward, HIGH);  // enable internal pull-up resistors
  digitalWrite(btnBack, HIGH);
  setTime(16,42,0,13,6,-30); // start with the time set to noon Jan 1 2011
  Serial.begin( 9600);
  Serial.println("ready");
  
}

void loop()
{
  prevtime = now();   // note the time
  while( prevtime == now() )    // stay in this loop till the second changes
  {
      // check if the set button pressed while waiting for second to roll over
     if(checkSetTime())
         prevtime = now();   // time changed so reset start time
  }
  digitalClockDisplay();
  lcddisplay();
}
    
// functions checks to see if the time should be adjusted
// returns true if time was changed
boolean checkSetTime()
{
int step;   // the number of seconds to move (backwards if negative) 
boolean isTimeAdjusted = false;  // set to true if the time is adjusted
  step = 1;   // ready to step forwards
  while(digitalRead(btnForward)== LOW)
  {
     adjustTime(step);
     isTimeAdjusted = true; // to tell the user that the time has changed
     step = step + 1; // next step will be bigger
     digitalClockDisplay(); // update clock   
     delay(100); 
  }
  step = -1;   // negative numbers step backwards
  while(digitalRead(btnBack)== LOW)
  {
     adjustTime(step);
     isTimeAdjusted = true; // to tell the user that the time has changed
     step = step - 1; // next step will be a bigger negative number
     digitalClockDisplay(); // update clock   
     delay(100); 
  }
  return isTimeAdjusted;  // tell the user if the time was adjusted
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
void lcddisplay(){
  lcd.print(second());
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Please don't post same question in numerous places; one question, one discussion. No Cross posting.

This is not the place to be asking this type of question, but for a quick answer, you need to set the cursor. lcd.setCursor(0,0); or wherever you want to show the numbers.

I got a library of time in arduino witch works properly but once I join it to the rest of the program every 2 seconds it skips one second. I have no idea y this happens can anybody help?? thnx

not without code ...

/**
 * ReadSHT1xValues
 *
 * Read temperature and humidity values from an SHT1x-series (SHT10,
 * SHT11, SHT15) sensor.
 *
 * Copyright 2009 Jonathan Oxer <jon@oxer.com.au>
 * www.practicalarduino.com
 */
#include <Time.h>
#include <SHT1x.h>
#include <LiquidCrystal.h>
// Specify data and clock connections and instantiate SHT1x object
#define dataPin  10
#define clockPin 9
SHT1x sht1x(dataPin, clockPin);
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long  prevtime;   // when the clock was last displayed

void setup()
{
  
  
   Serial.begin(38400); // Open serial connection to report values to host
   Serial.println("Starting up");
   // set up the LCD's number of columns and rows: 
  lcd.begin(2, 16);    //sets up the LCD to use 2 lines, each 16 characters long.
  
  setTime(16,42,0,13,6,-30); // start with the time set to noon Jan 1 2011
}

struct record
{
  int temp_c;
  int temp_f;
  int humidity;
  
};

void loop()
{
 
  int i=0;
  record var[5000];
  // Read values from the sensor
  var[i].temp_c = sht1x.readTemperatureC();
  var[i].temp_f = sht1x.readTemperatureF();
  var[i].humidity = sht1x.readHumidity();
  // Print the values to the serial port
  Serial.print("Temperature: ");
  Serial.print(var[i].temp_c, DEC);
  Serial.print("C / ");
  Serial.print(var[i].temp_f, DEC);
  Serial.print("F. Humidity: ");
  Serial.print(var[i].humidity);
  Serial.println("%");
  // Print a message to the LCD.
  lcd.print("Temp ");
  lcd.print(var[i].temp_c);
  lcd.print(" Humid ");
  lcd.print(var[i].humidity);
  //lcd.setCursor(0, 1);
  digitalClockDisplay(); // update clock
  i++,i++;
float voltx;
float volt;
voltx=analogRead(A0);
   Serial.print("Voltage: ");
   volt=voltx*5/1023;
   Serial.println(volt,DEC);
   lcd.print("  ");
  lcd.print("V ");
  lcd.print(volt);
   lcd.print("  ");
  
  //digitalClockDisplay();
  //delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
  lcd.setCursor(0,1);
  lcd.print(hour());
  lcd.print(":");
  lcd.print(minute());
  lcd.print(":");
  lcd.print(second());
  
}

void printDigits(int digits){
  // utility function for clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
void lcddisplay(){
  lcd.setCursor(0,1);
  lcd.print(minute());
  lcd.print(" haha ");
  lcd.print(second());
}

record var[5000];

How much RAM do you think the Arduino has?

@eagle_101, DO NOT CROSS-POST.

In case you get another urge to cross-post I refer you to this.

you are write but I was just trying it and the rest of the program works with 5000 but anyway that is not the problem bcz i tried it.

Serial.print takes time to display, and since you have multiple of them your code is going to slow down a bit.

You can condense a lot the Serial.prints with the sprintf or sprintf_P functions.

I tryed and it doesnt work but the think is that it doesnt slow down but actually the seconds work just fine but once in a while it skips one. and it does that very frequently. And thanks by the way for your time

I would add some code to track the run time of the loop and see if it explains the skipped seconds. You could actually test at different places in the loop to see what is taking the time.

Yea, use the millis() or micros() function.

unsigned long prevTime = millis(); // current time in milliseconds

/* some function or code segment */

Serial.println( millis() - prevTime ); // time difference