-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_SR_Handler.ino
More file actions
202 lines (162 loc) · 5.85 KB
/
Arduino_SR_Handler.ino
File metadata and controls
202 lines (162 loc) · 5.85 KB
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
194
195
196
197
198
199
200
201
202
//Pin connected to ST_CP of 74HC595
int latchPin = 5;
//Pin connected to SH_CP of 74HC595
int clockPin = 3;
//Pin connected to DS of 74HC595
int dataPin = 2;
int oePin = 6;
int mrPin = 4;
// How many 74hct595 are in cascade
const int N_REGISTERS = 8;
// How long to illuminate each layer
const int FLASH_PERIOD = 3;
// Layer counter
int layer = 0;
// For recieving data
byte recieved = 0;
int last_layer_recieved = 0;
int BAUDRATE = 9600;
// This array actually does the legwork
byte cube[N_REGISTERS*N_REGISTERS];
// Initial state
char initial[8*N_REGISTERS*N_REGISTERS] = {
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '0', '0', '0', '0', '0', '0', '1',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'1', '0', '0', '0', '0', '0', '0', '1',
'1', '0', '0', '0', '0', '0', '0', '1',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'1', '0', '0', '0', '0', '0', '0', '1',
'1', '0', '0', '0', '0', '0', '0', '1',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '1', '1', '0', '0', '0',
'0', '0', '0', '1', '1', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'1', '0', '0', '0', '0', '0', '0', '1',
'1', '0', '0', '0', '0', '0', '0', '1',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '1', '1', '0', '0', '0',
'0', '0', '0', '1', '1', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'1', '0', '0', '0', '0', '0', '0', '1',
'1', '0', '0', '0', '0', '0', '0', '1',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'1', '0', '0', '0', '0', '0', '0', '1',
'1', '0', '0', '0', '0', '0', '0', '1',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
'1', '0', '0', '0', '0', '0', '0', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1'};
void setup() {
// set pins to output because they are
// addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(mrPin, OUTPUT);
pinMode(oePin, OUTPUT);
digitalWrite(oePin, LOW);
digitalWrite(mrPin, HIGH);
digitalWrite(clockPin, LOW);
Serial.begin(BAUDRATE);
Serial.write("I've been reset...\n");
char2binary();
}
void loop() {
if (Serial.available()) recieveBytes();
writeLayer(layer);
// Iterate up through the layers
layer += 1;
layer = layer % N_REGISTERS;
}
// Convert the initial state to the binary arrays
void char2binary(){
// Convert the layer to it's binary representation
for (int i=0; i<N_REGISTERS*N_REGISTERS; i++){
// Reset the row to all off
cube[i] = 0;
// Loop through this layer and convert to binary
cube[i] += initial[8*i] - 48;
for (int j=1; j<8; j++){
cube[i] <<= 1;
cube[i] += initial[8*i + j] - 48;
}
}
}
void addressLayer(int layer){
registerWrite(layer, true);
}
void registerWrite(int whichPin, int whichState) {
// the bits you want to send
byte bitsToSend = 0;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin, LOW);
// turn on the next highest bit in bitsToSend:
bitWrite(bitsToSend, whichPin, whichState);
// shift the bits out:
shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);
}
// Shove the rows array out to the shift registers
void writeLayer(int mylayer){
// Write the rows to the registers
// Disable pushing the output
digitalWrite(latchPin, false);
// Address the desired layer
registerWrite(mylayer, true);
// Push the rows of the layer out
for (int i=mylayer*N_REGISTERS; i<(1+mylayer)*N_REGISTERS; i++){
shiftOut(dataPin, clockPin, LSBFIRST, cube[i]);
}
// Push the output
digitalWrite(latchPin, true);
}
// Recieve all the bytes that were pushed across
// and push them into the layers array
void recieveBytes(){
while(Serial.available()){
recieved = Serial.read();
// Serial.print("Recieved: ");
// Serial.println(recieved, BIN);
cube[last_layer_recieved] = recieved;
last_layer_recieved++;
last_layer_recieved = last_layer_recieved % (N_REGISTERS*N_REGISTERS);
}
}