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>
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>
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
/**
* 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());
}
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.