Go Down

Topic: Making an Arduino clock (Read 208 times) previous topic - next topic

lineman2208

Oct 10, 2017, 08:37 pm Last Edit: Oct 10, 2017, 08:44 pm by lineman2208
I am making an arduino clock with LCD screen as output, but the clock runs slow. Also i do not have a RTC, I read about them and I think they are kind of useless, every time the power goes out on a real clock it has to be set. Last night it was 2 min behind now it is 10 min behind. I have serial on to print to the computer so i can debug without being physically with the arduino, does serial printing slow down the arduino? How can I debug how long it takes to go through the code loop? I see there is a RTC library, is that something I would want to use for this?

Code: [Select]

/*
  24 Hour Clock Using 16x2 LCD panel
  Adjustable Minute, Input 6
  Adjustable Hour, Input 5
  Adjustable Lcd Back Lighting, Input 13, Output 3
  3 Brightnesses + off
*/

#include <LiquidCrystal.h>

//initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int BackLight = 3;  //BackLight PWM Output
int BLBut = 13;     //Button to toggle 3 BackLight settings
int ValBLB = 0;     //Value for BackLight Button
int ValBL = 85;     //Value for BackLight Brightness

int MinBut = 6;     //Digital button for minute adjustment
int HrBut = 5;      //Digital button for hour adjustment
int ValM = 0;       //Value for Minute button press
int ValH = 0;       //Value for Hour button press
int Hour = 19;      //Hour integer
int Min = 20;        //Minute integer
int Sec = 0;        //Seconds integer

void setup()
{
  Serial.begin(9600);
  pinMode(BackLight, OUTPUT);
  pinMode(BLBut, INPUT);
  pinMode(MinBut, INPUT);
  pinMode(HrBut, INPUT);
  lcd.begin(16, 2);
  analogWrite(BackLight, ValBL);
}

void loop()
{
  Sec = Sec + 1;
  Seconds();
  Minutes();
  Hours();
  Time();
  PrintTime();
  SerialTime();
  ValBLB = digitalRead(BLBut);
  BackLighting();
  ValM = digitalRead(MinBut);
  SetM();
  ValH = digitalRead(HrBut);
  SetH();
  delay(1000);
  lcd.clear();
}

//Loop to run for BackLight adjustment
void BackLighting()
{
  if (ValBLB == HIGH)
  {
    ValBL = ValBL + 85;
    analogWrite(BackLight, ValBL);
  }
  if (ValBL >= 256)
  {
    ValBL = 0;
    analogWrite(BackLight, ValBL);
  }
}

//Loop to run for Minute adjustment
void SetM()
{
  if ( ValM == HIGH)
  {
    Sec = 00;
    Min = Min + 1;
  }
}

//Loop to run for Hour adjustment
void SetH()
{
  if ( ValH == HIGH)
  {
    Sec = 00;
    Hour = Hour + 1;
  }
}

//Loop that adds the Seconds
void Seconds()
{
  if (Sec == 60)
  {
    Sec = 0;
    Min = Min + 1;
  }
}

//Loop that adds the Minutes
void Minutes()
{
  if (Min == 60)
  {
    Min = 0;
    Hour = Hour + 1;
  }
}

//Loop that resets the Hours
void Hours()
{
  if (Hour == 24)
  {
    Hour = 0;
  }
}

//Loop that keeps the displayed numbers double digits
//And prints the time
void PrintTime()
{
  if (Hour < 10)
  {
    lcd.setCursor(2, 1);
    lcd.print(0);
    lcd.print(Hour);
  }
  else
  {
    lcd.setCursor(2, 1);
    lcd.print(Hour);
  }

  lcd.print(" : ");

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

    if (Sec < 10)
  {
    lcd.print(0);
    lcd.print(Sec);
  }
  else
  {
    lcd.print(Sec);
  }
}

//Loop that runs to welcome the time of day
void Time()
{
  if (Hour > 5 && Hour <= 11)
  {
    lcd.setCursor(2, 0);
    lcd.print("Good Morning");
  }
  if (Hour > 11 && Hour <= 17)
  {
    lcd.setCursor(1, 0);
    lcd.print("Good Afternoon");
  }
  if (Hour > 17 && Hour <= 21)
  {
    lcd.setCursor(2, 0);
    lcd.print("Good Evening");
  }
  if (Hour > 21 || Hour <= 5)
  {
    lcd.setCursor(3, 0);
    lcd.print("Good Night");
  }
}

//Loop that keeps the displayed numbers double digits
//And prints the time
void SerialTime()
{
  if (Hour < 10)
  {
    Serial.print(0);
    Serial.print(Hour);
  }
  else
  {
    Serial.print(Hour);
  }

  Serial.print(" : ");

  if (Min < 10)
  {
    Serial.print(0);
    Serial.print(Min);
  }
  else
  {
    Serial.print(Min);
  }
 
  Serial.print(" : ");

    if (Sec < 10)
  {
    Serial.print(0);
    Serial.println(Sec);
  }
  else
  {
    Serial.println(Sec);
  }
}

Delta_G

#1
Oct 10, 2017, 09:27 pm Last Edit: Oct 10, 2017, 09:29 pm by Delta_G
The Arduino has a ceramic resonator that can be used to keep approximate time but it is affected by temperature and voltage and shouldn't be relied on for accurate time. Over the course of 24 hours they'll ofyen end up off by several minutes.  The amount will vary from time to time so it's not something you can correct for.

Quote
Also i do not have a RTC, I read about them and I think they are kind of useless
Except that they allow something like an Arduino to keep accurate time.

Quote
I see there is a RTC library, is that something I would want to use for this?
That library is to control an RTC.  If you're still set on not using an RTC then I'd say you wouldn't have much use for it. 
If at first you don't succeed, up - home - sudo - enter.

lineman2208

Quote
The Arduino has a ceramic resonator that can be used to keep approximate time but it is affected by temperature and voltage
Knowing this now I will change my approach and get myself a RTC. The reason I thought they were worthless was because the explanation I heard to use it was to keep time in event of power loss, which i do not have a use for.

68tjs

It is not the integrated circuit RTC that keeps the time, it is the battery that keeps it always powered.
Remove the battery and the RTC will lose the time.

Currently the most accurate RTC is the DS3231.

Delta_G

Yeah, if you just want it to lose time when the power goes out so you can reset it then just take the battery out.
If at first you don't succeed, up - home - sudo - enter.

lineman2208

#5
Oct 12, 2017, 07:15 pm Last Edit: Oct 13, 2017, 01:05 am by lineman2208
I could use some help with setting up the 7 segment 4 digit LED display. I am just stuck and do not see a way here. The display i plan to use is: https://www.adafruit.com/product/865. And, I believe I will use an Arduino Micro for the brains. I am retrofitting the LCD code to an LED display.

Code: [Select]

/*
  24 Hour Clock Using A 7 Segment LED Clock Display
  Adjustable Minute, Input 6
  Adjustable Hour, Input 5
*/
#include <Wire.h>
#include <SparkFunDS1307RTC.h>

int MinBut = 4;    //Digital button for minute adjustment
int HrBut = 3;     //Digital button for hour adjustment
int ForBut = 2;    //Digital button for format change
int ValM = 0;      //Value for Minute button press
int ValH = 0;      //Value for Hour button press
int Form = 0;      //Value for format change button press
int Hour = 0;      //Hour integer
int Min = 0;       //Minute integer
bool HR = true;    //Change 24Hr or 12Hr Format

//Common Cathode 7 Segment LED Display
//          Arduino pin: 2,3,4,5,6,7,8
byte seven_seg_digits[10][7] =
{
  { 1, 1, 1, 1, 1, 1, 0 }, // = 0
  { 0, 1, 1, 0, 0, 0, 0 }, // = 1
  { 1, 1, 0, 1, 1, 0, 1 }, // = 2
  { 1, 1, 1, 1, 0, 0, 1 }, // = 3
  { 0, 1, 1, 0, 0, 1, 1 }, // = 4
  { 1, 0, 1, 1, 0, 1, 1 }, // = 5
  { 1, 0, 1, 1, 1, 1, 1 }, // = 6
  { 1, 1, 1, 0, 0, 0, 0 }, // = 7
  { 1, 1, 1, 1, 1, 1, 1 }, // = 8
  { 1, 1, 1, 0, 0, 1, 1 } // = 9
};


void setup()
{
  rtc.begin();
  rtc.autoTime();
  rtc.set24Hour();
  pinMode(MinBut, INPUT);
  pinMode(HrBut, INPUT);
  pinMode(ForBut, INPUT);
  pinMode(5, OUTPUT);   
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop()
{
  static int8_t lastSecond = -1;
  rtc.update();
  if (rtc.second() != lastSecond) // If the second has changed
  {
    PrintTime();
    lastSecond = rtc.second(); // Update lastSecond value
  }
  PrintTime();
  SetM();
  SetH();
  Format();
}


//Loop to run for Minute adjustment
void SetM()
{
  ValM = digitalRead(MinBut);
  if ( ValM == HIGH)
  {
    rtc.setSecond(0);
    Min = rtc.getMinute();
    Min = Min + 1.0;
    rtc.setMinute(Min);
  }
}

//Loop to run for Hour adjustment
void SetH()
{
  ValH = digitalRead(HrBut);
  if ( ValH == HIGH)
  {
    Hour = rtc.getHour();
    Hour = Hour + 1.0;
    rtc.setMinute(Hour);
  }
}

//Loop that changes format upon button press
void Format()
{
  Form = digitalRead(ForBut);
  if (Form == HIGH && HR == true)
  {
    rtc.set12Hour();
    HR = false;
  }
  if (Form == HIGH && HR == false)
  {
    rtc.set24Hour();
    HR = true;
  }
}

//Loop that keeps the displayed numbers double digits
//And prints the time
void PrintTime()
{
  if (Hour < 10)
  {
    lcd.setCursor(2, 1);
    lcd.print(0);
    lcd.print(Hour);
  }
  else
  {
    lcd.setCursor(2, 1);
    lcd.print(Hour);
  }

  lcd.print(" : ");

  if (Min < 10)
  {
    lcd.print(0);
    lcd.print(Min);
  }
  else
  {
    lcd.print(Min);
  }
}

Go Up