1#include <Wire.h>
2#include <Adafruit_GFX.h>
3#include "Adafruit_LEDBackpack.h"
4
5
6Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
7
8
9const int switchPin = 2;
10int switchState = 0;
11
12
13const int numReadings = 10;
14double potReadings[numReadings];
15int readIndex = 0;
16int potTotal = 0;
17int potAverage = 0;
18
19
20int state = 0;
21unsigned long previousTime = 0;
22int interval;
23
24
25
26const int x0[] = {0,0,4,4,0,0,4,4,0,0,4,4,0,0,4,4};
27const int y0[] = {0,4,4,0,0,4,4,0,0,4,4,0,0,4,4,0};
28const int z0[] = {2,2,2,2,3,3,3,3,1,1,1,1,0,0,0,0};
29const int w0[] = {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4};
30const int h0[] = {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4};
31
32
33const int x1[] = {2,0,4,2,0,4,2,0,4,2,0,4};
34const int y1[] = {0,4,4,0,4,4,0,4,4,0,4,4};
35const int z1[] = {2,2,2,3,3,3,1,1,1,0,0,0};
36const int w1[] = {4,4,4,4,4,4,4,4,4,4,4,4};
37const int h1[] = {4,4,4,4,4,4,4,4,4,4,4,4};
38
39
40const int x2[] = {0,0,4,4,0,0,0,0,4,4,0,0};
41const int y2[] = {4,0,4,0,4,0,4,0,4,0,4,0};
42const int z2[] = {1,1,1,3,3,3,2,2,2,0,0,0};
43const int w2[] = {4,8,4,4,8,4,4,8,4,4,8,4};
44const int h2[] = {4,4,4,4,4,4,4,4,4,4,4,4};
45
46
47const int *xp = x0;
48const int *yp = y0;
49const int *zp = z0;
50const int *wp = w0;
51const int *hp = h0;
52
53
54int stateLimit = 16;
55
56
57int TimeSig = 0;
58int TimeSigLimit = 3;
59
60
61const int piezoPin = 8;
62int modulo = 4;
63
64
65void setup() {
66 Serial.begin(9600);
67 Serial.println("8x8 LED Matrix Test");
68
69 matrix.begin(0x70);
70 matrix.setRotation(3);
71
72 pinMode(switchPin, INPUT);
73
74
75 for (int thisReading = 0; thisReading < numReadings; thisReading++) {
76 potReadings[thisReading] = 0;
77 }
78}
79
80
81void loop() {
82
83
84 potTotal = potTotal - potReadings[readIndex];
85 potReadings[readIndex] = analogRead(A0);
86 potTotal = potTotal + potReadings[readIndex];
87 readIndex = readIndex + 1;
88
89 if (readIndex >= numReadings) {
90
91 readIndex = 0;
92 }
93 potAverage = sqrt(potTotal/numReadings) * 100;
94 delay(1);
95
96
97 interval = map(potAverage,0,3200,1500,200);
98 Serial.println(interval);
99
100
101 switchState = digitalRead(switchPin);
102 if(switchState == HIGH){
103
104 TimeSig += 1;
105
106 if (TimeSig == TimeSigLimit){
107 TimeSig = 0;
108 }
109
110 switch(TimeSig){
111
112 case 0:
113 xp = x0;
114 yp = y0;
115 zp = z0;
116 wp = w0;
117 hp = h0;
118 stateLimit = 16;
119 modulo = 4;
120 break;
121
122 case 1:
123 xp = x1;
124 yp = y1;
125 zp = z1;
126 wp = w1;
127 hp = h1;
128 stateLimit = 12;
129 modulo = 3;
130 break;
131
132 case 2:
133 xp = x2;
134 yp = y2;
135 zp = z2;
136 wp = w2;
137 hp = h2;
138 stateLimit = 12;
139 modulo = 6;
140 break;
141 }
142
143 state = 0;
144
145 matrix.clear();
146 matrix.writeDisplay();
147
148 delay(250);
149 }
150
151
152 matrix.fillRect(xp[state],yp[state],wp[state],hp[state],zp[state]);
153
154
155 unsigned long currentTime = millis();
156
157
158 if (currentTime - previousTime > interval){
159
160 previousTime = currentTime;
161
162 matrix.writeDisplay();
163
164 if(state % modulo == 0){
165 tone(piezoPin,880,20);
166 }
167
168 else{
169 tone(piezoPin,440,20);
170 }
171
172 state += 1;
173
174 if(state == stateLimit){
175 state = 0;
176 }
177 }
178}
179