Sunday, December 17, 2017

PDC1211: a simple clock sketch




Hello! Upon request I've written a very simple clock sketch. It uses the PDC1211 library for that I wrote for a 7 segments lcd with 12 digits and 1 row. In its own turn this library is based on the HT1621 and LiquidCrystal7S libraries. Please see them all for more informations. Enjoy!

The sketch
It is assumed that you use a PDC1211 module. The clock is pretty precise but to be more precise I've added some compensation to correct the millis lost during the lcd update operations. The compensation has been measure on an Arduino Uno module, if you use a different module, please recompute the compensation.

Indeed, an update is performed every 500 millis. If the variable Blink is true then only the two '-' symbols are updated. Otherwise, a second is passed and all the time variables are updated suitably. The first phase costs about 5 millis, the second 9 millis. If the updating period is changed, then don't forget to measure and change those values.

See here for more informations about pin connections.

/**
 * \file clock.ino
 * \brief Simple clock for the PDC1211 library.
 * \author Enrico Formenti
 * \date 12 17 2017
 * \version 1.0
 * \copyright BSD license, check the License page on the blog for more information. All this text must be
 *  included in any redistribution.
 *  <br><br>
 *  See macduino.blogspot.com for more details.
 */

#include "PDC1211.h"

#define SS 2
#define RW 3
#define DATA 4

PDC1211 lcd(SS,RW,DATA);


uint8_t seconds = 55;
uint8_t minutes = 59;
uint8_t hours = 23;

bool hasToUpdateHours = true;
bool hasToUpdateMinutes = true;
bool Blink = false;
uint16_t compensation = 0;


void setup() {
    lcd.begin();
}

void loop() {

    delay(500);
    if(Blink) {
        lcd.setCursor(2,0);
        lcd.print(" ");
        lcd.setCursor(5,0);
        lcd.print(" ");
        compensation += 5;

    }
    else
    {
        lcd.home();
        ++seconds;
        if(compensation>=1000) {
            ++seconds;
            compensation = 0;
        }
        if(seconds==60) {
            seconds = 0;
            ++minutes;
            hasToUpdateMinutes = true;
        }
        if(minutes==60) {
            minutes = 0;
            ++hours;
            hasToUpdateHours = true;
        }
        if(hours == 24)
            hours=0;
        if(hasToUpdateHours) {
            if(hours<10)
                lcd.print('0');
            lcd.print(hours);
        }
        else
            lcd.setCursor((uint8_t)2,(uint8_t)0);

        lcd.print("-");

        if(minutes<10)
            lcd.print('0');
        if(hasToUpdateMinutes)
            lcd.print(minutes);
        else
            lcd.setCursor((uint8_t)5,(uint8_t)0);
      
        lcd.print("-");
  
        if(seconds<10)
            lcd.print('0');
        lcd.print(seconds);
  
        hasToUpdateHours = false;
        hasToUpdateMinutes = false;
        compensation += 9;
    }
    Blink ^= true;
}