-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpioneers_brick.ino
193 lines (165 loc) · 3.95 KB
/
pioneers_brick.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <SoftwareSerial.h>
#include <EEPROM.h>
// serial pins
#define rxPin 2
#define txPin 3
// initialice serial port
SoftwareSerial mySerial(rxPin, txPin);
// EEPROM read variable and last pos
int zz;
int zzLast = 0;
// color of this brick between 1 and 252
const int myColor = 2;
const char myCharColor = myColor;
const int EEsize = 256; // size in bytes of attiny45 EEPROM
void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(4, OUTPUT);
// Configure counter/timer0 for fast PWM on PB0 and PB1
TCCR0A = 3<<COM0A0 | 3<<COM0B0 | 3<<WGM00;
TCCR0B = 0<<WGM02 | 3<<CS00; // Optional; already set
// Configure counter/timer1 for fast PWM on PB4
GTCCR = 1<<PWM1B | 3<<COM1B0;
TCCR1 = 3<<COM1A0 | 7<<CS10;
mySerial.begin(9600); //initilize serial line
// initialize EEPROM if empty
resetEE(true);
}
/**
* Software Pulse Width Modulation for RGB
*/
void rgbLed(int rVal, int gVal, int bVal) {
*&OCR1B = 255 - rVal;
*&OCR0B = 255 - gVal;
*&OCR0A = 255 - bVal;
}
void loop() {
for (int a=0; a<EEsize; a++) {
zz = EEPROM.read(a);
if( zz == 255) {
zzLast = a;
break;
} else {
// mySerial.print("EEPROM position: ");
// mySerial.print(a);
// mySerial.print(" contains ");
// mySerial.println(zz);
rgbLed(getColor(zz, 0), getColor(zz, 1), getColor(zz, 2));
if(mySerial.available() && zzLast != 0) {
int inChar = mySerial.read();
mySerial.print("Catched char: ");
mySerial.println(inChar);
EEPROM.write(zzLast, inChar);
zzLast = 3;
while(zzLast != 0) {
rgbLed(0,0,0);
delay(500);
rgbLed(0,255,0);
delay(500);
zzLast--;
}
rgbLed(0,0,0);
delay(500);
mySerial.flush();
}
delay(1000);
mySerial.println(myCharColor);
}
}
}
/*
/ get Color travels in 252 steps trough 24 bit color space
/
/ 252,0,0 -> 252,252,0 -> 0,252,0 -> 0,252,252 -> 0,0,252, -> 252,0,252, -> reset
/
*/
int getColor(int val, int color) {
if(val <=42) {
switch (color) {
case 0:
return 252;
break;
case 1:
return 6 * val;
break;
case 2:
return 0;
break;
}
} else if(val <=84) {
switch (color) {
case 0:
return 252 - (6*(val - 42));
break;
case 1:
return 252;
break;
case 2:
return 0;
break;
}
} else if(val <=126) {
switch (color) {
case 0:
return 0;
break;
case 1:
return 252;
break;
case 2:
return 6 * (val - 84);
break;
}
} else if(val <=168) {
switch (color) {
case 0:
return 0;
break;
case 1:
return 252 - (6*(val - 126));
break;
case 2:
return 252;
break;
}
} else if(val <=210) {
switch (color) {
case 0:
return 6*(val - 168);
break;
case 1:
return 0;
break;
case 2:
return 252;
break;
}
} else if(val <= 252) {
switch (color) {
case 0:
return 252;
break;
case 1:
return 0;
break;
case 2:
return 252 - (6*(val - 210));
break;
}
}
}
//// reset EEPROM if 0 is not sed yet or force
void resetEE(boolean force) {
zz = EEPROM.read(0);
if( force || zz == 255) {
for (int a=0; a<EEsize; a++) {
if( a == 0) {
EEPROM.write(a, myColor);
} else {
EEPROM.write(a, 255);
}
}
}
}