Skip to content

Instantly share code, notes, and snippets.

@djsirius
Last active April 1, 2017 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djsirius/a27122bdc4f23643c0ce06ec5d1b2fd2 to your computer and use it in GitHub Desktop.
Save djsirius/a27122bdc4f23643c0ce06ec5d1b2fd2 to your computer and use it in GitHub Desktop.
/*
Based on RV8523 RTC (Real-Time-Clock) Example from Watterott Electronic
Uno A4 (SDA), A5 (SCL)
Mega 20 (SDA), 21 (SCL)
Leonardo 2 (SDA), 3 (SCL)
Note: To enable the I2C pull-up resistors on the RTC-Breakout, the jumper J1 has to be closed.
*2016/4/2 Modification for a Tutorial @Arduino/Genuino Day 2016 @Watterott electronic
*2016/4/4 Modification by Maik H�bscher
- configurable Led-count on LED-Ring
- Serial communication deactivable if neccessary
- LED dimming via const or Analog Input
- implementing Time Setup *contributor mschlenker
To set the time using the serial console. On linux use a command like:
stty -F /dev/ttyUSB1 speed 9600
date '+T%H%M%S%d%m%Y' > /dev/ttyUSB1
to read the current local time from the serial console. Or just type a string like
T23595924122015
to set the clock to 23:59:59 24th of December 2015 using Arduinos/Genuinos serial monitor.
Type an arbitary string not beginning with 'T' to show the current time.
*/
#include <Wire.h>
#include <RV8523.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define NeoPin 13 // Data out pin of the WS2812 Ring
#define AnalogIn A0 // AnalogIn for LED dimming
#define SerialData 0 // Send Data via UART 1=enable 0=disable
#define SerialBaud 9600 // Baudrate for communication
#define BUFF_MAX 32
unsigned int recv_size = 0;
char recv[BUFF_MAX];
#define PIXELCOUNT 60 //Numbers of pixels
#define PIXELOFFSET 30 //Offset for first pixel
#define LiveDimActive 1 //Live dimming 1=Yes 0=No
#define DimMin 40 //Minimum dimming value 0-255
#define DimMax 255 //Maximum dimming value 0-255
//if LiveDimActive is deactivated, the Value of Dim is the Led-brightness
int Dim = 127; //Init Value for LED dimming
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELCOUNT, NeoPin, NEO_GRB + NEO_KHZ800);
int Pos = 60 / PIXELCOUNT;
int previousSec = 0;
RV8523 rtc;
void setup()
{
//init Serial port
Serial.begin(SerialBaud);
while(!Serial); //wait for serial port to connect - needed for Leonardo only
//init RTC
Serial.println("Init RTC...");
printTime();
//set 24 hour mode
rtc.set24HourMode();
//start RTC
rtc.start();
//When the power source is removed, the RTC will keep the time.
rtc.batterySwitchOver(1); //battery switch over on
strip.begin();
strip.show();
}
void loop()
{
uint8_t sec, min, hour, day, month;
uint16_t year;
if (Serial.available() > 0) {
setClock();
}
//get time from RTC
rtc.get(&sec, &min, &hour, &day, &month, &year);
if(LiveDimActive > 0){
Dim = analogRead(AnalogIn);
Dim = map(Dim, 0, 1023, DimMin, DimMax);
}
if(previousSec != sec){
ShowClock(hour, min, sec);
previousSec = sec;
if(SerialData > 0){
printTime();
}
}
}
/*
* Subroutines
*/
void ShowClock (int hour, int minute, int sec) {
for(int i=0; i<PIXELCOUNT; i++){
strip.setPixelColor(i, strip.Color(0,0,0)); //delete all LEDs
}
strip.setPixelColor((sec/Pos+PIXELOFFSET)%PIXELCOUNT, strip.Color(Dim,0,0));
strip.setPixelColor((minute/Pos+PIXELOFFSET)%PIXELCOUNT, strip.Color(0,0,Dim));
strip.setPixelColor((hour*Pos*5+PIXELOFFSET)%PIXELCOUNT, strip.Color(Dim,Dim,Dim));
strip.show();
}
void setClock()
{
char in;
in = Serial.read();
Serial.print(in);
if((in == 10 || in == 13) && (recv_size > 0))
{
parseCmd(recv, recv_size);
printTime();
recv_size = 0;
recv[0] = 0;
return;
}
else if (in < 48 || in > 122) //ignore ~[0-9A-Za-z]
{
//do nothing
}
else if (recv_size > BUFF_MAX - 2) //drop lines that are too long
{
recv_size = 0;
recv[0] = 0;
}
else if (recv_size < BUFF_MAX - 2)
{
recv[recv_size] = in;
recv[recv_size + 1] = 0;
recv_size += 1;
}
}
// Parse the time string and set the clock accordingly
void parseCmd(char *cmd, int cmdsize)
{
uint8_t i;
uint8_t reg_val;
char buff[BUFF_MAX];
//ThhmmssDDMMYYYY aka set time
if (cmd[0] == 84 && cmdsize == 15)
{
rtc.set(
inp2toi(cmd, 5),
inp2toi(cmd, 3),
inp2toi(cmd, 1),
inp2toi(cmd, 7),
inp2toi(cmd, 9),
inp2toi(cmd, 11) * 100 + inp2toi(cmd, 13)
); //sec, min, hour, day, month, year
Serial.println("OK");
}
}
// just output the time
void printTime()
{
uint8_t sec, min, hour, day, month;
uint16_t year;
//get time from RTC
rtc.get(&sec, &min, &hour, &day, &month, &year);
//serial output
Serial.print("\nTime: ");
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(min, DEC);
Serial.print(":");
Serial.print(sec, DEC);
Serial.print("\nDate: ");
Serial.print(day, DEC);
Serial.print(".");
Serial.print(month, DEC);
Serial.print(".");
Serial.print(year, DEC);
}
uint8_t inp2toi(char *cmd, const uint16_t seek)
{
uint8_t rv;
rv = (cmd[seek] - 48) * 10 + cmd[seek + 1] - 48;
return rv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment