Zápisník experimentátora
Hierarchy: WS2812
In the last article of the series on creating clock from NeoPixel Ring we connect the real-time clock circuit. We will use IC DS1307, which contains in itself real-time clock. We will need only a few external components.
![]()
Against the previous scheme we added I2C real-time clock. I2C uses two specific pins and two pull-up resistors. For details about using DS1307, see separate article.
![]()
We need real time during the start of the program. According to it, we set the starting position for the hour, minute and second hands.
#include <Time.h>
#include <DS1307RTC.h>
///
/// Program configuration
///
struct Configuration
{
ClockState cs;
bool show_millis;
int hour;
int minute;
int second;
Configuration()
: show_millis(SHOWMILLIS), cs(csClock), hour(STARTHOUR), minute(STARTMINUTE), second(0) {}
};
Configuration cfg;
///
/// Update all positions
///
void NeoClock::Update()
{
// we use fake time
uint32_t m=millis()+1000L*cfg.second+60000L*cfg.minute+3600000L*cfg.hour;
second = map ((m % 60000), 0, 60000, 0, CNT);
milli = map ((m % 1000), 0, 1000, 0, CNT);
minute = map (((m/60000) % 60), 0, 60, 0, CNT);
hour = map (((m/60000/60) % 12), 0, 12, 0, CNT);
}
///
/// Setup
///
void setup()
{
Serial.begin(9600);
while (!Serial) ; // wait for serial
Serial.println("NeoPixel Clock v. 1.4");
Serial.println("-------------------");
tmElements_t tm;
if (RTC.read(tm)) {
Serial.print("Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
cfg.second=tm.Second;
cfg.minute=tm.Minute;
cfg.hour=tm.Hour;
}
strip.begin();
strip.show(); // Initialize all pixels to 'off'
strip.setBrightness(0); // disable flickering
}
In this series of articles we went through several successive points and we designed a clock with all the basic functions. We still have room for improvement, but it is a motivation for you to do it. You can improve source code.
10.09.2015