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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
| #include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
int lr = 15;
int lg = 0;
int lb = 0;
unsigned char RainbowCMD[5];
unsigned char State = 0;
unsigned long timeout;
void setup()
{
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
flash(10, 50);
pinMode(2, OUTPUT);
}
void loop(){
DateTime now = RTC.now();
char time1 = '0' + now.hour() / 10; if (time1 == 0){ ShowChar(1,time1, 0, 0, 0,0); } else { ShowChar(1,time1, lr, lg, lb, 0);}
char time2 = '0' + now.hour() % 10; ShowChar(2,time2, lr, lg, lb,0);
char time3 = '0' + now.minute() / 10; ShowChar(3,time3, lr, lg, lb,0);
char time4 = '0' + now.minute() % 10; ShowChar(4,time4, lr, lg, lb,0);
delay(1000);
toggle(5);
}
int toggle(int pin){
// Creator sasaa
// toggles a led on if off.
// and off if on.
if (digitalRead(pin) == HIGH){
digitalWrite(pin, LOW);
} else {
digitalWrite(pin, HIGH);
}
}
int flash(int time,int times){
// Shifts color pads around the 4 matrixes
for (int ax=0; ax <= times; ax++){
ShowColor(1,15,0,0); ShowColor(2,0,15,0); ShowColor(3,0,0,15); ShowColor(4,15,15,15);
delay(time / 4);
ShowColor(4,15,0,0); ShowColor(1,0,15,0); ShowColor(2,0,0,15); ShowColor(3,15,15,15);
delay(time / 4);
ShowColor(3,15,0,0); ShowColor(4,0,15,0); ShowColor(1,0,0,15); ShowColor(2,15,15,15);
delay(time / 4);
ShowColor(2,15,0,0); ShowColor(3,0,15,0); ShowColor(4,0,0,15); ShowColor(1,15,15,15);
delay(time / 4);
}
}
void ShowColor(int Adr, unsigned char R, unsigned char G, unsigned char B) {
unsigned char shift;
RainbowCMD[0]='R';
RainbowCMD[1]=0x03;
RainbowCMD[2] = B;
RainbowCMD[3]=((G<<4)|(R));
SentCMD(Adr);
}
void SetPixelXY(int Adr, unsigned char X, unsigned char Y, unsigned char R, unsigned char G, unsigned char B) {
unsigned char TranslateX[8]={6,7,4,5,2,3,0,1};
X = TranslateX[X];
RainbowCMD[0] = 'R';
RainbowCMD[1] = 0x04;
RainbowCMD[2] = R;
RainbowCMD[3] = ((G<<4)|(B));
RainbowCMD[4] = ((X<<4)|(Y));
SentCMD(Adr);
}
void ShowChar(int Address,unsigned char ASCII,unsigned char red, unsigned char green ,unsigned char blue,unsigned char shift)
{
RainbowCMD[0]='R';
RainbowCMD[1]=0x02;
RainbowCMD[2]=((shift<<4)|(red));
RainbowCMD[3]=((green<<4)|(blue));
RainbowCMD[4]=ASCII;
SentCMD(Address);
}
void SentCMD(int Add)
{ unsigned char OK=0;
unsigned char i,temp;
while(!OK)
{
switch (State)
{
case 0:
Wire.beginTransmission(Add);
for (i=0;i<5;i++) Wire.send(RainbowCMD[i]);
Wire.endTransmission();
delay(5);
State=1;
break;
case 1:
Wire.requestFrom(Add,1);
if (Wire.available()>0)
temp=Wire.receive();
else {
temp =0xFF;
timeout++;
}
if ((temp==1)||(temp==2)) State=2;
else if (temp==0) State=0;
if (timeout>5000)
{
timeout=0;
State=0;
}
delay(5);
break;
case 2:
OK=1;
State=0;
break;
default:
State=0;
break;
}
}
} |