-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgbclock.ino
128 lines (96 loc) · 2.95 KB
/
rgbclock.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Code collected and completed by Tobias Floery <[email protected]>
// FastSPI Library from http://code.google.com/p/fastspi/
#include <FastSPI_LED.h>
// Adafruits RTClib from https://github.com/adafruit/RTClib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
// Sometimes chipsets wire in a backwards sort of way
struct CRGB { unsigned char b; unsigned char r; unsigned char g; };
//struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
struct CRGB *leds;
// Number of RGB Pixel for FastSPI
#define NUM_LEDS 60
// Datapin for FastSPI
#define PIN 4
#define FILTER_LEN 8
// Brightness level
int level=15;
// Brightness correction for Leds
unsigned char bright[] = {0, 2, 4, 7, 11, 18, 30, 40, 50, 65, 80,96,125,160, 200, 255};
void setup()
{
// Setup FastSPI Lib
FastSPI_LED.setLeds(NUM_LEDS);
FastSPI_LED.setChipset(CFastSPI_LED::SPI_TM1804);
FastSPI_LED.setPin(PIN);
FastSPI_LED.init();
FastSPI_LED.start();
leds = (struct CRGB*)FastSPI_LED.getRGBData();
// init RTClib
Wire.begin();
RTC.begin();
//Serial.begin(9600);
if (!RTC.isrunning()) {
//Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// setup measurement for LDR
digitalWrite(A0, HIGH); // set pullup on analog pin 0
pinMode(A1, OUTPUT);
digitalWrite(A1, LOW);
}
unsigned int ldr=0;
void doLDR() {
unsigned int ldr_value = analogRead(0); //reads the LDR values
ldr = ((unsigned long)(FILTER_LEN-1)*ldr+ldr_value)/FILTER_LEN;
//Serial.println(ldr_value);
unsigned int light_level=0;
light_level = (1023-ldr)>>6;
if (light_level >= 15) light_level = 15;
if (light_level <= 1) light_level = 1;
level = light_level;
//Serial.println(light_level);
//level=2;
}
DateTime old;
void loop() {
// get time
DateTime now = RTC.now();
if (now.second()!=old.second()) {
old = now;
doLDR();
}
// clear LED array
memset(leds, 0, NUM_LEDS * 3);
if (level < 4) {
// this is the night mode
// set LED background
for (int i=0; i<NUM_LEDS; i++) {
leds[i].r = bright[level];
leds[i].g = (bright[level]+1)/2;
}
// set pixels
//leds[(now.second())%60].r = bright[level];
leds[(now.minute())%60].g = 0;
leds[(now.minute())%60].r = bright[level+2];
unsigned char hourpos = (now.hour()%12)*5;
leds[((59+hourpos)%60)%60].g = 0;
leds[(hourpos)%60].g = 0;
leds[((hourpos+1))%60].g = 0;
leds[(hourpos)%60].r = bright[level+2];
} else {
// set pixels
leds[(now.second())%60].r = bright[level];
leds[(now.minute())%60].b = bright[level];
unsigned char hourpos = (now.hour()%12)*5;
leds[((59+hourpos)%60)%60].g = bright[level/2];
leds[(hourpos)%60].g = bright[level];
leds[((hourpos+1))%60].g = bright[level/2];
}
// update LEDs
FastSPI_LED.show();
// wait some time
delay(500);
}