Arduino Uno code for rtc 3231 clock
#include <LiquidCrystal.h>
#include "BigFont.h"
#include <Wire.h>
#include <DS3231.h>
const byte hourPin = 2;
const byte minPin = 3;
bool up_hour = true;
bool up_min = true;
byte HR, MIN, oldMIN;
byte clock_sign = 0;
unsigned long lastmills = 0;
DS3231 ddRTC;
BigFont BF(4);
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void print_time() {
byte hr2, hr1, min2, min1;
bool _h12, _PM_time;
HR = ddRTC.getHour(_h12, _PM_time);
hr2 = HR / 10; // 05 13 22
hr1 = HR % 10;
min2 = MIN / 10; // 59
min1 = MIN % 10;
BF.printbigchar(lcd,hr2,0,0); // lcd, digit, col, row
BF.printbigchar(lcd,hr1,5,0);
BF.printbigchar(lcd,min2,11,0);
BF.printbigchar(lcd,min1,16,0);
oldMIN = ddRTC.getMinute();
}
void setup() {
Wire.begin();
lcd.begin(20, 4);
BF.loadchars(lcd); // load special chars in lcd
pinMode(hourPin, INPUT_PULLUP);
pinMode(minPin, INPUT_PULLUP);
MIN = ddRTC.getMinute();
print_time();
}
void loop() {
MIN = ddRTC.getMinute();
// output only when minutes have changed
if (MIN != oldMIN) {
print_time();
}
if ((digitalRead(hourPin) == 0) && (up_hour)) {
up_hour = false;
HR++;
if (HR > 23) HR = 0;
ddRTC.setHour(HR);
print_time();
delay(100); //debounce
}
if ((digitalRead(minPin) == 0) && (up_min)) {
up_min = false;
MIN++;
if (MIN > 59) MIN = 0;
ddRTC.setMinute(MIN);
ddRTC.setSecond(0);
print_time();
delay(100); // debounce
}
if (millis() - lastmills > 500) { // half a second has passed
lastmills = millis();
lcd.setCursor(10,0);
(clock_sign==7)? clock_sign = 254 : clock_sign = 7;
lcd.write(clock_sign); // blink clock sign
}
if (digitalRead(hourPin)) up_hour = true;
if (digitalRead(minPin)) up_min = true;
} // loop
BigFont.h
/*
* BigFont.h - Big Font library for Arduino - Version 0.21
*
* Derived from https://github.com/wa1hco/BigFont
* Original library (0.1) by Tom Igoe and others
* Modified work Copyright 2017 Gianluca Cassarino
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Drives 4 row, character type Liquid Crystal Displays with 3x2, 3x3, 4x4 fonts of special symbols
*
*/
// ensure this library description is only included once
#ifndef BigFont_h
#define BigFont_h
#include <LiquidCrystal.h>
#include <Arduino.h>
#ifndef LiquidCrystal_h // Using the new LiquidCrystal library
#ifdef LiquidCrystal_4bit_h || LiquidCrystal_I2C_h || _LIQUIDCRYSTAL_SR_ || _LIQUIDCRYSTAL_SR2W_ ||
_LIQUIDCRYSTAL_SR3W_H_
#include "LiquidCrystal.h"
// #include <LiquidCrystal_I2C.h>
#else
#error You must install New LiquidCrystal library to work with non-4bit projects:
http:/bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
#endif
#endif
// library interface description
class BigFont {
public:
// constructor:
BigFont(uint8_t size = 3);
// load special characters
#ifndef LiquidCrystal_I2C_h // New liquid crystal library
void loadchars(LiquidCrystal& lcd);
#else
void loadchars(LiquidCrystal_I2C& lcd);
#endif
// print char method
#ifndef LiquidCrystal_I2C_h // New liquid crystal library
void printblankchar(LiquidCrystal& lcd, byte col, byte row);
void printbigchar(LiquidCrystal& lcd, byte digit, byte col, byte row);
#else
void printblankchar(LiquidCrystal_I2C& lcd, byte col, byte row);
void printbigchar(LiquidCrystal_I2C& lcd, byte digit, byte col, byte row);
#endif
uint8_t getFontSize();
void setFontSize(uint8_t size);
int version(void);
private:
uint8_t _fontSize;
// Define the custom characters parts
// The order is 1,2,3 for curved top, 4,5,6 for curved bottom
// 0 is used for number 4, 7 is the alarm bell symbol
// parts for 3x2 bigfont
// byte custchar2[8][8] =
byte custchar2[7][8] =
{
{ // 0
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111
},
{ // 1
B00111,
B01111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
},
{ // 2
B11111,
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000
},
{ // 3
B11100,
B11110,
B11111,
B11111,
B11111,
B11111,
B11110,
B11100
},
{ // 4
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B01111,
B00111
},
{ // 5
B11111,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111
},
{ // 6
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11110,
B11100
}/*,
{ // 7 (alarm symbol) same for all fonts, defined below
B00100,
B01110,
B01110,
B01110,
B11111,
B00000,
B00100,
B00000
}*/
}; // custchar[]
// custchar (3x3, 4x4)
byte custchar3[8][8] =
{
{ // 0 // slash used for number 4
B00001,
B00011,
B00111,
B01111,
B11111,
B11110,
B11100,
B11000
},
{ // 1
B00111,
B01111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
},
{ // 2
B11111,
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000
},
{ // 3
B11100,
B11110,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
},
{ // 4
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B01111,
B00111
},
{ // 5
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111
},
{ // 6
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11110,
B11100
},
{ // 7 (alarm symbol)
B00100,
B01110,
B01110,
B01110,
B11111,
B00000,
B00100,
B00000
}
}; // custchar[]
// Define the large fonts, borrowed from GreenHerron RT-21 pictures
// Define here globally for Class so not define on each call to print character
// FONTS 4x4
byte bignums[10][4][4] =
{
{ // 0
{ 1, 2, 2, 3},
{255, 254, 254, 255},
{255, 254, 254, 255},
{ 4, 5, 5, 6}
},
{ // 1 ok
{254, 1, 255, 254},
{254, 254, 255, 254},
{254, 254, 255, 254},
{254, 5, 255, 5}
},
{ // 2 ok
{ 1, 2, 2, 3},
{254, 254, 5, 6},
{ 1, 2, 254, 254},
{255, 5, 5, 5}
},
{ // 3
{ 1, 2, 2, 3},
{254, 254, 5, 6},
{254, 254, 2, 3},
{ 4, 5, 5, 6}
},
{ // 4
{255, 254, 254, 255},
{ 4, 5, 5, 255},
{254, 254, 254, 255},
{254, 254, 254, 255}
},
{ // 5
{255, 2, 2, 2}, // 2= top half
{ 4, 5, 5, 5}, // 3 = top right corner
{254, 254, 254, 255}, // 254 = blank 255= full
{ 4, 5, 5, 6} // 4= bottom left corner 5= bottom half 6= bottom right corner
},
{ // 6
{ 1, 2, 2, 254},
{255, 254, 254, 254},
{255, 2, 2, 3},
{ 4, 5, 5, 6}
},
{ // 7
{ 4, 2, 2, 255},
{254, 254, 254, 6},
{254, 254, 1, 254},
{254, 254, 255, 254}
},
{ // 8 ok
{ 1, 2, 2, 3},
{ 4, 5, 5, 6},
{ 1, 2, 2, 3},
{ 4, 5, 5, 6}
},
{ // 9 ok
{ 1, 2, 2, 3},
{ 4, 5, 5, 255},
{254, 254, 254, 255},
{254, 5, 5, 6}
}
}; // bignums[]
// FONTS 3x3
byte bignums3x3[10][3][3] =
{
{ // 0
{ 1, 2, 3},
{255, 254, 255},
{ 4, 5, 6},
},
{ // 1
{ 0, 255, 254},
{254, 255, 254},
{ 5, 255, 5},
},
{ // 2
{ 1, 2, 3},
{254, 5, 6},
{ 1, 5, 5},
},
{ // 3
{ 1, 2, 3},
{254, 2, 3},
{ 4, 5, 6},
},
{ // 4
{254, 0, 255},
{ 1, 5, 6},
{254, 254, 3},
},
{ // 5
{255, 2, 2},
{ 2, 2, 3},
{ 5, 5, 6},
},
{ // 6
{ 1, 2, 254},
{255, 2, 3},
{ 4, 5, 6},
},
{ // 7
{ 4, 2, 255},
{254, 1, 6},
{254, 255, 254},
},
{ // 8
{ 1, 2, 3},
{ 1, 2, 3},
{ 4, 5, 6},
},
{ // 9
{ 1, 2, 3},
{ 4, 5, 255},
{ 4, 5, 6},
}
}; // bignums3x3[]
// FONTS 3x2
// byte bignums3x2[10][3][3] =
byte bignums3x2[10][2][3] =
{
{ // 0
{ 1, 2, 255},
{ 4, 0, 6}
},
{ // 1
{ 47, 255, 254},
{254, 255, 254}
},
{ // 2
{ 2, 5, 3},
{ 1, 5, 0},
},
{ // 3
{ 2, 5, 3},
{ 0, 5, 3}
},
{ // 4
{ 1, 254, 255},
{254, 2, 6}
},
{ // 5
{255, 5, 2},
{ 0, 5, 3}
},
{ // 6
{ 1, 2, 254},
{ 4, 5, 3}
},
{ // 7
{ 4, 2, 6},
{254, 47, 254}
},
{ // 8
{ 1, 5, 3},
{ 1, 5, 3}
},
{ // 9
{ 1, 2, 255},
{ 0, 5, 6}
}
}; // bignums3x2[]
}; // Class BigFont
#endif
BigFont.cpp
/*
* BigFont.cpp - Big Font library for Arduino - Version 0.21
*
* Derived from Stepper.cpp
* Original library (0.1) by Tom Igoe et al...
* Modified work Copyright 2017 Gianluca Cassarino
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "BigFont.h"
#include "LiquidCrystal.h"
#include "Arduino.h"
/*
* Big Font constructor
*/
BigFont::BigFont(uint8_t size)
{
if(size > 4){ size = 4; }
if(size < 2){ size = 2; }
this->_fontSize = size;
}
/*
* Load the special characters in LCD driver chip
* Called once on configuring custom characters
*/
#ifndef LiquidCrystal_I2C_h // New liquid crystal library
void BigFont::loadchars(LiquidCrystal& lcd)
#else
void BigFont::loadchars(LiquidCrystal_I2C& lcd)
#endif
{
if(this->_fontSize == 2) {
// FONT 3x2
// load custom characters into display controller memory
lcd.createChar(0, custchar2[0]);
lcd.createChar(1, custchar2[1]);
lcd.createChar(2, custchar2[2]);
lcd.createChar(3, custchar2[3]);
lcd.createChar(4, custchar2[4]);
lcd.createChar(5, custchar2[5]);
lcd.createChar(6, custchar2[6]);
lcd.createChar(7, custchar3[7]); // bell (same for all fonts)
} else {
// FONTS 3x3 e 4x4
// load custom characters into display controller memory
lcd.createChar(0, custchar3[0]);
lcd.createChar(1, custchar3[1]);
lcd.createChar(2, custchar3[2]);
lcd.createChar(3, custchar3[3]);
lcd.createChar(4, custchar3[4]);
lcd.createChar(5, custchar3[5]);
lcd.createChar(6, custchar3[6]);
lcd.createChar(7, custchar3[7]);
}
}
// print blank character
#ifndef LiquidCrystal_I2C_h // New liquid crystal library
void BigFont::printblankchar(LiquidCrystal& lcd, byte col, byte row)
#else
void BigFont::printblankchar(LiquidCrystal_I2C& lcd, byte col, byte row)
#endif
{
for(int i = 0; i < 4; i++) // for each row
{
lcd.setCursor(col, row + i);
for(int j = 0; j < 4; j++) // for each column
{
lcd.write(254); // blank char
} // for(i...
} // for(j...
} // printblankchar()
// digit 0-9; col, row in LCD character, symbol
#ifndef LiquidCrystal_I2C_h // New liquid crystal library
void BigFont::printbigchar(LiquidCrystal& lcd, byte digit, byte col, byte row)
#else
void BigFont::printbigchar(LiquidCrystal_I2C& lcd, byte digit, byte col, byte row)
#endif
{
if ((digit > 9) | (digit < 0)) return;
// loop over the font and write to lcd
if(this->_fontSize == 2){ // 3x2 fonts
for (int i = 0; i < this->_fontSize; i++) // for each row
{
lcd.setCursor(col, row + i); //set the row address
for (int j = 0; j < 3; j++) // for each column of font
{
lcd.write(this->bignums3x2[digit][i][j]);
}
}
} else if(this->_fontSize == 3){ // 3x3 fonts
for (int i = 0; i < this->_fontSize; i++) // for each row
{
lcd.setCursor(col, row + i); //set the row address
for (int j = 0; j < 3; j++) // for each column of font
{
lcd.write(this->bignums3x3[digit][i][j]);
}
}
} else { // 4x4 fonts
for (int i = 0; i < this->_fontSize; i++) // for each row
{
lcd.setCursor(col, row + i); //set the row address
for (int j = 0; j < 4; j++) // for each column of font
{
lcd.write(this->bignums[digit][i][j]);
}
}
}
}
uint8_t BigFont::getFontSize(){
return this->_fontSize;
}
void BigFont::setFontSize(uint8_t size) {
if(size > 4){ size = 4; }
if(size < 2){ size = 2; }
this->_fontSize = size;
}
// version() returns the version of the library:
int BigFont::version(void) {
return 2;
}