-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino code
More file actions
161 lines (134 loc) · 4.87 KB
/
Arduino code
File metadata and controls
161 lines (134 loc) · 4.87 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
#include <arduinoFFT.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#define SAMPLES 64 //for FFT, Must be a power of 2
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW // Set display type so that MD_MAX72xx library treets it properly
#define MAX_DEVICES 4 // Total number display modules
#define CLK_PIN 13 // Clock pin to communicate with display
#define DATA_PIN 11 // Data pin to communicate with display
#define CS_PIN 10 // Control pin to communicate with display
#define xres 32 // Total number of columns in the display, must be <= SAMPLES/2
#define yres 8 // Total number of rows in the display
int MY_ARRAY[]={0, 128, 192, 224, 240, 248, 252, 254, 255};
int MODE_1[]={0, 128, 192, 224, 240, 248, 252, 254, 255}; // standard pattern
int MODE_2[]={0, 1, 3, 7, 15, 31, 63, 127, 255}; // upside down
int MODE_3[]={0, 128, 192, 224, 208, 200, 196, 194, 193}; // peak and bottom 2 points
int MODE_4[]={0, 1, 192, 7, 240, 31, 252, 127, 255};//every other column is upside down
int MODE_5[]={0, 128, 64, 32, 16, 8, 4, 2, 1}; // only peak pattern
int MODE_6[]={0, 128, 128, 160, 160, 168, 168, 170, 170};// every other light is on
double vReal[SAMPLES];
double vImag[SAMPLES];
char vReal2[xres];
int yvalue;
int displaycolumn , displayvalue;
int peaks[xres];
const int buttonPin = 4;
const int pinAdc = A0;
int state = HIGH; // the current reading from the input pin
int previousState = LOW; // the previous reading from the input pin
int displaymode = 1;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50; // the debounce time for button
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES); // display object
arduinoFFT FFT = arduinoFFT(); // FFT object
void setup() {
ADCSRA &= ~(bit (ADPS0) | bit (ADPS1) | bit (ADPS2)); // clear prescaler bits
ADCSRA |= bit (ADPS0) | bit (ADPS2); // set prescaler to be 32, gives a 38kHz sampling frequency
Serial.begin(115200);
pinMode(buttonPin, INPUT);
mx.begin(); // initialize display
delay(50); // wait to get reference voltage stabilized
}
void loop() {
for(int i=0; i<SAMPLES; i++){
long sum = 0;
for(int j=0; j<32; j++)
{
sum += analogRead(pinAdc);//return digital value
}
sum >>= 5;//averaging
vReal[i]= sum/6; // Copy to bins after compressing
vImag[i] = 0;
// Serial.println(sum);
}
// FFT
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
// -- FFT
// re-arrange FFT result to match with num of columns on display
int step = (SAMPLES/2)/xres;
int c=0;
for(int i=0; i<(SAMPLES/2); i+=step)
{
vReal2[c] = 0;
for (int k=0 ; k< step ; k++) {
vReal2[c] = vReal2[c] + vReal[i+k];
}
vReal2[c] = vReal2[c]/step;
c++;
}
// send data to LCD, and display
for(int i=0; i<xres; i++)
{
vReal2[i] = constrain(vReal2[i],0,80); // set max & min values for buckets
vReal2[i] = map(vReal2[i], 0, 80, 0, yres); // remap averaged values to yres
yvalue=vReal2[i];
peaks[i] = peaks[i]-1; // decay by one light
if (yvalue > peaks[i])
peaks[i] = yvalue ;
yvalue = peaks[i];
displayvalue=MY_ARRAY[yvalue];
displaycolumn=31-i;
mx.setColumn(displaycolumn, displayvalue); // for left to right
}
displayModeChange (); // check if button pressed to change display mode
}
void displayModeChange() {
int reading = digitalRead(buttonPin);
//Serial.println(reading);
if (reading == LOW && previousState == HIGH&& millis() - lastDebounceTime > debounceDelay) // works only when pressed
{
//Serial.print("button");
switch (displaymode) {
case 1: // move from mode 1 to 2
displaymode = 2;
for (int i=0 ; i<=8 ; i++ ) {
MY_ARRAY[i]=MODE_2[i];
}
break;
case 2: // move from mode 2 to 3
displaymode = 3;
for (int i=0 ; i<=8 ; i++ ) {
MY_ARRAY[i]=MODE_3[i];
}
break;
case 3: // move from mode 3 to 4
displaymode = 4;
for (int i=0 ; i<=8 ; i++ ) {
MY_ARRAY[i]=MODE_4[i];
}
break;
case 4: // move from mode 4 to 5
displaymode = 5;
for (int i=0 ; i<=8 ; i++ ) {
MY_ARRAY[i]=MODE_5[i];
}
break;
case 5: // move from mode 5 to 6
displaymode = 6;
for (int i=0 ; i<=8 ; i++ ) {
MY_ARRAY[i]=MODE_6[i];
}
break;
case 6: //5 to 1
displaymode = 1;
for (int i=0 ; i<=8 ; i++ ) {
MY_ARRAY[i]=MODE_1[i];
}
break;
}
lastDebounceTime = millis();
}
previousState = reading;
}