// Based on : http://www.breadedescalope.com/index.php/shadowplay-uhr-fuer-einen-salon
#include <TinyWireM.h>
#include <Adafruit_NeoPixel.h> //needed for the WS2812
#include <TinyDebug.h>
#define TOTAL_LED 32
#define LED_PIN PB1
#define BRIGHTNESS 255
#define HALF_LED TOTAL_LED/2
Adafruit_NeoPixel strip = Adafruit_NeoPixel(TOTAL_LED, LED_PIN, NEO_GRB + NEO_KHZ800);
#define COLOR_SECONDS strip.Color(255, 255, 255)
#define COLOR_MINUTES strip.Color(255, 0, 0)
#define COLOR_HOURS strip.Color(0, 0, 255)
#define DS1307_ADDR 0x68
byte seconds,minutes,hours,days,months,years;
#define PIR PB3
byte bcdToDec(byte val) { // Convert binary coded decimal to normal decimal numbers
return ((val / 16 * 10) + (val % 16));
}
void getTime(){
byte wireRet = 0;
TinyWireM.beginTransmission(DS1307_ADDR); // reset DS1307 register pointer
TinyWireM.send(0);
wireRet = TinyWireM.endTransmission();
if (wireRet) delay(1500);
wireRet = TinyWireM.requestFrom(DS1307_ADDR, 7); // request 7 bytes from DS1307
if (wireRet) delay(1500);
seconds = bcdToDec(TinyWireM.receive()); // handle the 7 bytes received
minutes = bcdToDec(TinyWireM.receive());
hours = bcdToDec(TinyWireM.receive()) %12;
TinyWireM.receive();
days = bcdToDec(TinyWireM.receive());
months = bcdToDec(TinyWireM.receive());
years = bcdToDec(TinyWireM.receive());
Debug.print(hours);
Debug.print(":");
Debug.print(minutes);
Debug.print(":");
Debug.println(seconds);
}
void setup() {
pinMode(PIR, INPUT);
TinyWireM.begin();
Debug.begin();
Debug.println(F("Hello, TinyDebug!"));
strip.begin();
strip.setBrightness(BRIGHTNESS); // set brightness
strip.show();
}
// Seconds - Green
// Minuts - Red
// Hours - Other
void loop() {
strip.clear();
getTime();
int led_seconds = map(seconds, 0, 60, 0, TOTAL_LED);
int led_minutes = map(minutes, 0, 60, 0, TOTAL_LED);
int led_hours = map(hours, 0, 12, 0, HALF_LED) * 2;
if (digitalRead(PIR) == LOW) {
strip.setPixelColor(led_seconds, COLOR_SECONDS);
strip.setPixelColor(led_minutes, COLOR_MINUTES);
strip.setPixelColor(led_hours , COLOR_HOURS);
} else {
strip.setPixelColor((led_seconds + HALF_LED) % TOTAL_LED, COLOR_SECONDS);
strip.setPixelColor((led_minutes + HALF_LED) % TOTAL_LED, COLOR_MINUTES);
strip.setPixelColor((led_hours + HALF_LED) % TOTAL_LED, COLOR_HOURS);
}
strip.show();
delay(1000);
}