-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLEDMatrix.ino
More file actions
237 lines (210 loc) · 4.77 KB
/
LEDMatrix.ino
File metadata and controls
237 lines (210 loc) · 4.77 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
randomSeed(analogRead(0));
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
// pinMode(13, OUTPUT); debug
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
}
uint8_t image[3][3] = {
{0, 0, 0},
{0, 1, 0},
{0, 0, 0}
};
uint8_t button_prev;
uint8_t button;
uint8_t button_prev1;
uint8_t button1;
uint8_t button_prev2;
uint8_t button2;
uint8_t button_prev3;
uint8_t button3;
uint8_t button_prev4;
uint8_t button4;
void drawCol(uint8_t * rows,int col){
digitalWrite(col+1,HIGH);
digitalWrite(1,!rows[0]);
digitalWrite(2,!rows[1]);
digitalWrite(3,!rows[2]);
delay(1);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(col+1,LOW);
delay(1);
}
void draw(int n){
for(int j = 0; j < n; j++){
for(int i = 0; i < 3; i++){
drawCol(image[i],i+3);
}
}
}
void iterate(){
button_prev1 = button1;
button1 = !digitalRead(7);
button_prev2 = button2;
button2 = !digitalRead(8);
if(button1 && !button_prev1){
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
if((x < 2) && (image[x][y] == 1)){
image[x+1][y] = 1;
image[x][y] = 0;
x++;
}
else if((x == 2) && (y < 2) && (image[x][y] == 1)){
image[0][y+1] = 1;
image[x][y] = 0;
x = 3;
y = y+1;
}
else if((y == 2) && (x == 2) && (image[x][y] == 1)){
image[0][0] = 1;
image[x][y] = 0;
x = 3;
y = 3;
}
}
}
}
if(button2 && !button_prev2){
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
if((y < 2) && (image[x][y] == 1)){
image[x][y+1] = 1;
image[x][y] = 0;
y++;
}
else if((y == 2) && (x < 2) && (image[x][y] == 1)){
image[x+1][0] = 1;
image[x][y] = 0;
y = 3;
x = x+1;
}
else if((x == 2) && (y == 2) && (image[x][y] == 1)){
image[0][0] = 1;
image[x][y] = 0;
x = 3;
y = 3;
}
}
}
}
draw(1);
}
void up(){
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
if((x > 0) && (image[x][y] == 1)){
image[x-1][y] = 1;
image[x][y] = 0;
}
}
}
}
void down(){
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
if((x < 2) && (image[x][y] == 1)){
image[x+1][y] = 1;
image[x][y] = 0;
x++;
}
}
}
}
void left(){
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
if((y > 0) && (image[x][y] == 1)){
image[x][y-1] = 1;
image[x][y] = 0;
}
}
}
}
void right(){
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
if((y < 2) && (image[x][y] == 1)){
image[x][y+1] = 1;
image[x][y] = 0;
y++;
}
}
}
}
void traverse(){
draw(1);
button_prev1 = button1;
button1 = !digitalRead(7);
button_prev2 = button2;
button2 = !digitalRead(8);
button_prev3 = button3;
button3 = !digitalRead(9);
button_prev4 = button4;
button4 = !digitalRead(10);
// if the 'up' button has been pressed since the last time this ran, call up()
if(button1 && !button_prev1){
up();
}
// if the 'right' button has been pressed since the last time this ran, call right()
if(button2 && !button_prev2){
right();
}
// if the 'down' button has been pressed since the last time this ran, call down()
if(button3 && !button_prev3){
down();
}
// if the 'left' button has been pressed since the last time this ran, call left()
if(button4 && !button_prev4){
left();
}
}
void boxes(){
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
if(random(1)!= image[x][y]){
image[x][y] = !image[x][y];
}
}
}
draw(50);
}
int set = 0;
// the loop function runs over and over again forever
void loop() {
//traverse(); debug
//iterate(); debug
//boxes(); debug
//draw(1); debug
// determines if mode button (digitalRead(11)) has been pressed since the last time the line ran
button_prev = button;
button = !digitalRead(11);
// if the mode button has been pressed since the last time this ran, change the mode.
if(button && !button_prev){
set = !set;
}
if(set){
traverse();
}
if(!set){
iterate();
}
}