338 lines
9.2 KiB
C++
338 lines
9.2 KiB
C++
/* 7 Segment 4 Digits clock with digit point separator.
|
|
*
|
|
* It uses display module with common anode and DS1307/DS3231 RTC module to
|
|
* enhance time keeping ability of Atmega 328p based Arduino Nano
|
|
* microcontroller and 2 buttons for adjusting time. */
|
|
|
|
/* uRTCLib
|
|
* https://github.com/Naguissa/uRTCLib
|
|
* it has support for both DS1307 and DS3231, that I have.
|
|
* It is more fat than ds1307-dedicated or ds3231-dedicated lib, but I do not
|
|
* want to re-invent wheel if I have one. */
|
|
#include <uRTCLib.h>
|
|
|
|
/* SevSeg
|
|
* https://github.com/DeanIsMe/SevSeg
|
|
* it has simple api and ideally works in my case. */
|
|
#include <SevSeg.h>
|
|
|
|
/* Disable time compensation.
|
|
* At last we borrow more accurate chrystal and solder it to rtc breakboard.
|
|
*/
|
|
//#define TIMECOMP
|
|
// 0.5 second/day compensation flag.
|
|
//#define OFIVECOMP
|
|
|
|
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
|
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
|
|
|
uRTCLib clock;
|
|
SevSeg Display;
|
|
|
|
// Push button stuff.
|
|
const long initial_delay = 600; // wait a bit before spin numbers
|
|
const long adjust_interval = 200; // delay between consecutive number increments
|
|
const long bounce_delay = 20; // contact bounce compensation
|
|
const int hrs_btn = A0; // put hour adjusting button on A0 lane
|
|
const int min_btn = A1; // put minute adjusting button on A1 lane
|
|
volatile bool btn_pushed = false; // is button pushed?
|
|
unsigned long previousPushTimer = 0;
|
|
|
|
// Dot blinking stuff.
|
|
volatile bool dot = true;
|
|
|
|
#ifdef TIMECOMP
|
|
// Time inaccuracy compensation flag.
|
|
volatile bool compflag1 = true;
|
|
volatile bool compflag2 = false;
|
|
const uint8_t timeSkew = 9; // Assume that clock runs faster than should.
|
|
#ifdef OFIVECOMP
|
|
volatile bool timeSkewEven = false; // Comp. 1 additional second per 2 days.
|
|
#endif
|
|
#endif
|
|
|
|
// Ambient light compensation aka night mode.
|
|
const int photoPin = A2;
|
|
volatile bool measureAmbLight = true;
|
|
unsigned int ambLight = 0; // Actual value.
|
|
unsigned long previousLightTimer = 0;
|
|
const long refresh_interval = 40; // Refresh rate 25Hz.
|
|
|
|
// Time vars.
|
|
volatile uint8_t mysecond;
|
|
volatile uint8_t myminute;
|
|
volatile uint8_t myhour;
|
|
|
|
// RTC sync flag.
|
|
volatile bool rtcsync = true;
|
|
|
|
void setup()
|
|
{
|
|
// Setup display.
|
|
byte hardwareConfig = COMMON_ANODE;
|
|
byte numDigits = 4;
|
|
byte digitPins[] = {9, 10, 11, 12};
|
|
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 13};
|
|
bool resistorsOnSegments = true; // 8 resistors, 330 Ohms each
|
|
/* updateWithDelays defaults to false and it is recommended value,
|
|
* but I was unable to make my display to change brightness notably with
|
|
* recommended value, so i have to use updateWithDelays=true,
|
|
* in this case brightness gets changed. */
|
|
bool updateWithDelays = true;
|
|
|
|
/* Without it there is visual bug at 0 o'clock, and in current version
|
|
* of SevSeg' setChars() does not dots at all. */
|
|
bool leadingZeros = false;
|
|
|
|
Display.begin(hardwareConfig, numDigits, digitPins, segmentPins,
|
|
resistorsOnSegments, updateWithDelays, leadingZeros);
|
|
Display.setBrightness(100);
|
|
|
|
// Init i2c bus and setup clock address.
|
|
Wire.begin();
|
|
clock.set_rtc_address(0x68);
|
|
|
|
// If rtc returns incorrect time, reset it to some default values.
|
|
clock.refresh();
|
|
|
|
if (clock.lostPower()) {
|
|
clock.set(30, 52, 15, 6, 23, 2, 19); // Saturday, 23 Feb 2019 15:52:30
|
|
clock.lostPowerClear();
|
|
}
|
|
|
|
if ((clock.hour() > 23) || (clock.minute() > 59)) {
|
|
clock.set(30, 52, 15, 6, 23, 2, 19); // Saturday, 23 Feb 2019 15:52:30
|
|
}
|
|
|
|
/* In case of ds1307 this will be noop, but ds3231/ds3232 sometimes does
|
|
* not enable battery by default. */
|
|
clock.enableBattery();
|
|
|
|
// Setup control buttons.
|
|
pinMode(hrs_btn, INPUT_PULLUP);
|
|
pinMode(min_btn, INPUT_PULLUP);
|
|
|
|
/* Timer stuff.
|
|
* timer0 being used in millis() micros() delay() etc, it has 8 bit resolution.
|
|
* timer2 being used in analogWrite() tone() notone(), it has 8 bit resolution.
|
|
* timer1 being used in servo lib has 16 bit resolution.
|
|
* We'll use timer1, since we have no mechanical parts and don't need servo lib */
|
|
|
|
// Initialize timer1.
|
|
noInterrupts(); // Disable all interrupts.
|
|
TCCR1A = 0;
|
|
TCCR1B = 0;
|
|
|
|
TCNT1 = 34286; // Preload timer 65536-16MHz/256/2Hz
|
|
TCCR1B |= (1 << CS12); // 256 prescaler
|
|
TIMSK1 |= (1 << TOIE1); // Enable timer overflow interrupt.
|
|
interrupts(); // Enable all interrupts.
|
|
|
|
/* Increase sampling rate of analogRead()
|
|
* by setting an ADC prescale from 128 to 16, so it takes less time to read
|
|
* sensor. */
|
|
sbi(ADCSRA, ADPS2);
|
|
cbi(ADCSRA, ADPS1);
|
|
cbi(ADCSRA, ADPS0);
|
|
|
|
// Setup ambient brigtness sensor.
|
|
pinMode(photoPin, INPUT);
|
|
ambLight = analogRead(photoPin);
|
|
}
|
|
|
|
ISR(TIMER1_OVF_vect)
|
|
{
|
|
// Feed clock twice per second.
|
|
TCNT1 = 34286; // Preload timer.
|
|
// Do not read rtc if time beng adjusted by buttons.
|
|
(btn_pushed) ? rtcsync : rtcsync = true;
|
|
|
|
// Also switch dot.
|
|
if (dot)
|
|
dot = false;
|
|
else
|
|
dot = true;
|
|
|
|
#ifdef TIMECOMP
|
|
// Time compensation magic.
|
|
if ((myhour == 21) and (myminute == 21) and (mysecond == 21)
|
|
and (compflag2)) {
|
|
compflag1 = true;
|
|
#ifdef OFIVECOMP
|
|
(timeSkewEven) ? timeSkewEven = false : timeSkewEven;
|
|
#endif
|
|
}
|
|
|
|
if ((myhour == 21) and (myminute == 21) and (mysecond == 23)) {
|
|
compflag1 = false;
|
|
compflag2 = true;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
unsigned long currentMillis = millis();
|
|
|
|
#ifdef TIMECOMP
|
|
/* Let's implement time compensation for individual rtc module time
|
|
* characteristics. In in my case quartz resonator as inaccurate as
|
|
* about +9.5 seconds per 24 hours. */
|
|
if (compflag1 && compflag2) {
|
|
#ifdef OFIVECOMP
|
|
if (timeSkewEven) {
|
|
clock.set(
|
|
(mysecond - timeSkew - 1),
|
|
clock.minute(),
|
|
clock.hour(),
|
|
clock.dayOfWeek(),
|
|
clock.day(),
|
|
clock.month(),
|
|
clock.year()
|
|
);
|
|
} else {
|
|
#endif
|
|
clock.set(
|
|
(mysecond - timeSkew),
|
|
clock.minute(),
|
|
clock.hour(),
|
|
clock.dayOfWeek(),
|
|
clock.day(),
|
|
clock.month(),
|
|
clock.year()
|
|
);
|
|
#ifdef OFIVECOMP
|
|
}
|
|
#endif
|
|
|
|
compflag2 = false;
|
|
}
|
|
#endif
|
|
|
|
if (rtcsync) {
|
|
clock.refresh();
|
|
mysecond = clock.second();
|
|
myminute = clock.minute();
|
|
myhour = clock.hour();
|
|
updateTimeStr(myhour, myminute);
|
|
rtcsync = false;
|
|
}
|
|
|
|
// Push button magic.
|
|
if ((!btn_pushed) and
|
|
((digitalRead(hrs_btn) == LOW) or (digitalRead(min_btn) == LOW))) {
|
|
if (currentMillis - previousPushTimer >= initial_delay) {
|
|
previousPushTimer = currentMillis;
|
|
btn_pushed = true;
|
|
}
|
|
}
|
|
|
|
if (btn_pushed and
|
|
(digitalRead(hrs_btn) != LOW) and (digitalRead(min_btn) != LOW)) {
|
|
// compensate possible contact bounce with delay
|
|
if (currentMillis - previousPushTimer >= bounce_delay) {
|
|
btn_pushed = false;
|
|
}
|
|
}
|
|
|
|
if (btn_pushed) {
|
|
if (currentMillis - previousPushTimer >= adjust_interval) {
|
|
previousPushTimer = currentMillis;
|
|
|
|
if (digitalRead(hrs_btn) == LOW) {
|
|
myhour++;
|
|
(myhour > 23) ? myhour = 0 : myhour;
|
|
clock.set(
|
|
0,
|
|
myminute,
|
|
myhour,
|
|
clock.dayOfWeek(),
|
|
clock.day(),
|
|
clock.month(),
|
|
clock.year()
|
|
);
|
|
}
|
|
|
|
if (digitalRead(min_btn) == LOW) {
|
|
myminute++;
|
|
(myminute > 59) ? myminute = 0 : myminute;
|
|
clock.set(
|
|
0,
|
|
myminute,
|
|
myhour,
|
|
clock.dayOfWeek(),
|
|
clock.day(),
|
|
clock.month(),
|
|
clock.year()
|
|
);
|
|
}
|
|
}
|
|
|
|
updateTimeStr(myhour, myminute);
|
|
}
|
|
|
|
if ((currentMillis - previousLightTimer >= refresh_interval) or
|
|
(previousLightTimer > currentMillis)) { // millis() reset every ~49 days
|
|
previousLightTimer = currentMillis;
|
|
ambLight = analogRead(photoPin);
|
|
|
|
/* Ambient light compensation.
|
|
* ambLight can be in range of 0..1023, if it 910+, it has point to use
|
|
* fullbright.
|
|
* Use table comparsion (not function of ambLight) because of non-linear
|
|
* characteristics of led indicator, that cannot be described via simple
|
|
* formula. */
|
|
if (ambLight >= 910) {
|
|
Display.setBrightness(100);
|
|
} else if ((ambLight < 900) and (ambLight >= 860)) {
|
|
Display.setBrightness(75);
|
|
} else if ((ambLight < 850) and (ambLight >= 810)) {
|
|
Display.setBrightness(45);
|
|
} else if ((ambLight < 800) and (ambLight >= 760)) {
|
|
Display.setBrightness(25);
|
|
} else if ((ambLight < 750) and (ambLight >= 710)) {
|
|
Display.setBrightness(18);
|
|
} else if ((ambLight < 700) and (ambLight >= 660)) {
|
|
Display.setBrightness(12);
|
|
} else if ((ambLight < 650) and (ambLight >= 610)) {
|
|
Display.setBrightness(10);
|
|
} else if ((ambLight < 600) and (ambLight >= 560)) {
|
|
Display.setBrightness(7);
|
|
} else if ((ambLight < 550) and (ambLight >= 410)) {
|
|
Display.setBrightness(4);
|
|
} else if ((ambLight < 400) and (ambLight >= 360)) {
|
|
Display.setBrightness(0);
|
|
} else if ((ambLight < 350) and (ambLight >= 310)) {
|
|
Display.setBrightness(-3);
|
|
} else if ((ambLight < 300) and (ambLight >= 260)) {
|
|
Display.setBrightness(-6);
|
|
} else if ((ambLight < 250) and (ambLight >= 210)) {
|
|
Display.setBrightness(-10);
|
|
} else if ((ambLight < 200) and (ambLight >= 160)) {
|
|
Display.setBrightness(-14);
|
|
} else if ((ambLight < 150) and (ambLight >= 110)) {
|
|
Display.setBrightness(-18);
|
|
} else if (ambLight < 100) {
|
|
Display.setBrightness(-22);
|
|
}
|
|
}
|
|
|
|
Display.refreshDisplay();
|
|
}
|
|
|
|
void updateTimeStr(uint8_t h, uint8_t m)
|
|
{
|
|
if (dot) {
|
|
char str[5];
|
|
sprintf(str, "%2d.%02d", h, m);
|
|
Display.setChars(str);
|
|
} else {
|
|
char str[4];
|
|
sprintf(str, "%2d%02d", h, m);
|
|
Display.setChars(str);
|
|
}
|
|
}
|
|
|
|
|
|
/* vim: set filetype=cpp: */
|