Go Down

Topic: Displaying time on LCD with ChronoDot 2 (Read 734 times) previous topic - next topic

saintdismas

I'm trying to get a clock working on an Arduino Uno.  I've double checked everything but I can't find what I've got wrong.  The code I'm using is taken directly from here.  For whatever reason though, the hours and minutes displays just count up to 23 and then reset to 00.  They increment once per second.  And the seconds display is constantly at 00.  I have no idea what could be causing this odd behavior.  Can anyone tell me what I'm doing wrong?  As far as I can tell, I did everything exactly as shown.

I've changed all the Wire.send and Wire.receive to their updated terms. 

PaulS

Quote
Can anyone tell me what I'm doing wrong?  As far as I can tell, I did everything exactly as shown.

I can't see YOUR code, so no.

saintdismas

It's really a straight copy/paste with the two corrections that I noted but here you go:

Code: [Select]
/*
Code under (cc) by Manuel Gonzalez, www.codingcolor.com
http://creativecommons.org/license/cc-gpl
Pins 12, 11, 5, 4, 3, 2 to LCD
Analog pins 4 (SDA),5(SCL) to Chronodot
Pins 6 (hour), 7(min) buttons
*/

#include <Wire.h>
#include <LiquidCrystal.h>


const int hourButtonPin = 6;
const int minButtonPin = 7;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int hourButtonState;
int minButtonState;

int seconds; //00-59;
int minutes; //00-59;
int hours;//1-12 - 00-23;
int day;//1-7
int date;//01-31
int month;//01-12
int year;//0-99;




void setup()
{

  pinMode(hourButtonPin,INPUT);
  pinMode(minButtonPin,INPUT);

  Wire.begin();
  lcd.begin(16, 2);
 
  hourButtonState = 0;
  minButtonState = 0;
  ////////////////////////////////
  seconds = 00;
  minutes = 26;
  hours = 17;
  day = 7;
  date = 31;
  month = 5;
  year = 10;
  //initChrono();//just set the time once on your RTC
  ///////////////////////////////
}

void loop()
{
  check_buttons();
  get_time();
  get_date();
  display_time();
  display_date();
  delay(1000);
 
}
void display_time()
{
  char buf[12];
 
  lcd.setCursor(0, 0);
 
  if(hours == 0)
  {
    lcd.clear();
  }
 
  lcd.print("Time ");   
  lcd.print(itoa(hours, buf, 10));
  lcd.print(":");
 
  if(minutes < 10)
  {
    lcd.print("0");
  }
  lcd.print(itoa(minutes, buf, 10));
 
  lcd.print(":");
 
  if(seconds < 10){
    lcd.print("0");
  }
  lcd.print(itoa(seconds, buf, 10));

}
void display_date()
{
  char buf[12];
 
  lcd.setCursor(0, 1);
  lcd.print("Date ");
 
if(month < 10){
    lcd.print("0");
  } 
 
  lcd.print(itoa(month, buf, 10));
  lcd.print("/");
  lcd.print(itoa(date, buf, 10));
  lcd.print("/");
 
  if(year < 10){
    lcd.print("0");
  }
 
  lcd.print(itoa(year, buf, 10));
}
void initChrono()
{
  set_time();
  set_date();
}


void set_date()
{
  Wire.beginTransmission(104);
  Wire.write(3);
  Wire.write(decToBcd(day));
  Wire.write(decToBcd(date));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.endTransmission();
}
void get_date()
{
  Wire.beginTransmission(104);
  Wire.write(3);//set register to 3 (day)
  Wire.endTransmission();
  Wire.requestFrom(104, 4); //get 5 bytes(day,date,month,year,control);
  day   = bcdToDec(Wire.read());
  date  = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year  = bcdToDec(Wire.read());
}

void set_time()
{
   Wire.beginTransmission(104);
   Wire.write(0);
   Wire.write(decToBcd(seconds));
   Wire.write(decToBcd(minutes));
   Wire.write(decToBcd(hours));
   Wire.endTransmission();
}
void get_time()
{
  Wire.beginTransmission(104);
  Wire.write(0);//set register to 0
  Wire.endTransmission();
  Wire.requestFrom(104, 3);//get 3 bytes (seconds,minutes,hours);
  seconds = bcdToDec(Wire.read() & 0x7f);
  minutes = bcdToDec(Wire.read());
  hours = bcdToDec(Wire.read() & 0x3f);
 



 
}
void setHour()
{
  hours++;
  if(hours > 23)
  {
   hours = 0;
   seconds = 0;
   minutes = 0;
  }
  set_time();
 
}
void setMinutes()
{
  minutes++; 
  if(minutes > 59)
  {
   minutes = 0;
   
  }
  seconds=0;
 
  set_time();
 
}
void check_buttons()
{
  hourButtonState = digitalRead(hourButtonPin);
  minButtonState = digitalRead(minButtonPin);
 
  if(hourButtonState == HIGH){
    setHour();
  }
 
  if(minButtonState == HIGH){
    setMinutes();
  }
}
///////////////////////////////////////////////////////////////////////

byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}
//////////////////////////////////////////////////////////////////////

PaulS

That code is crap. The LiquidCrystal class derives from Print. It KNOWS how to convert an int to a string. There is no reason to be calling itoa().

Code: [Select]
  Wire.requestFrom(104, 4); //get 5 bytes(day,date,month,year,control);
If you are going to have useless comments, at least they should be correct useless comments.

Code: [Select]
  Wire.requestFrom(104, 3);//get 3 bytes (seconds,minutes,hours);
How can 3 bytes be seconds. minutes, and hours when 4 bytes are day, date, month, and year?

Look at the data sheet for your device, and see what the numbers really need to be. Typically, one gets all the data (day, date, month, year, seconds, minutes, and hours) at one time, then uses whatever is needed.

macegr

The requestFrom lines are correct because immediately prior to the day/month etc request, the starting address register is written to the device.
Unique RGB LED Modules and Arduino shields: http://www.macetech.com/store

PaulS

Quote
The requestFrom lines are correct because immediately prior to the day/month etc request, the starting address register is written to the device.

But, it's the same value in both cases. How can that be right?

macegr

I don't see the same value, I see it setting register address 3 for the get_date function, and register address 0 for the get_time function.
Unique RGB LED Modules and Arduino shields: http://www.macetech.com/store

PaulS

Quote
I don't see the same value, I see it setting register address 3 for the get_date function, and register address 0 for the get_time function.

Oops. I was looking at set_date() and get_date(), not get_time() and get_date().

Go Up