在虛擬電路模擬器中實(shí)現(xiàn)LED矩陣的音頻頻譜
為我大學(xué)的數(shù)字電路課程做的項(xiàng)目,目的是有一些很酷的東西放在你的桌子上。
在我的數(shù)字電路課程中,我們的任務(wù)是創(chuàng)建一個(gè)項(xiàng)目提案,主題可以自由選擇,然后將課程中學(xué)到的知識應(yīng)用到最終的項(xiàng)目中,該項(xiàng)目必須在虛擬電路模擬器中實(shí)現(xiàn)。我選擇使用Wokwi,一個(gè)我在這個(gè)項(xiàng)目之前從未使用過的平臺。經(jīng)過一番思考,我決定制作一個(gè)音頻頻譜可視化器作為我自己辦公桌的裝飾品。經(jīng)過一番研究,我發(fā)現(xiàn)這個(gè)想法已經(jīng)被做過很多次了,因此不是很原創(chuàng)。因此,我決定以其中一個(gè)項(xiàng)目為基礎(chǔ),為交互式桌面裝飾構(gòu)建額外的功能。
項(xiàng)目:
當(dāng)我開始做這個(gè)項(xiàng)目時(shí),我意識到我所選擇的項(xiàng)目和我在提案中概述的項(xiàng)目與我所選擇的平臺不兼容。這是一個(gè)物理項(xiàng)目,使用了Wokwi上沒有的庫,而且我無法導(dǎo)入它們,因?yàn)槲沂褂玫氖窃撈脚_的免費(fèi)版本。我找了另一個(gè)項(xiàng)目作為基礎(chǔ),一個(gè)利用LED矩陣,但我找不到任何。我只找到了使用顯示器的類似項(xiàng)目。所以,我必須從頭開始,學(xué)習(xí)LED矩陣是如何工作的,它的庫,以及如何操縱它。我開始創(chuàng)建列,然后使它們上下移動(dòng),最后使它們彼此獨(dú)立。完成這一部分后,我開始研究音頻頻譜可視化是如何工作的,并從類似的項(xiàng)目中尋求靈感。我選擇了被稱贊的項(xiàng)目,因?yàn)樗拇a看起來更可讀,也就是在那時(shí),我了解到fix_fft庫的存在,它救了我的命。使用庫,檢查文檔,并了解我受到啟發(fā)的項(xiàng)目如何工作,我逐漸更改了我在LED矩陣中創(chuàng)建的列。
遇到的困難:
最初的想法是使用蜂鳴器和麥克風(fēng)來顯示移動(dòng)的音頻頻譜。然而,該平臺的麥克風(fēng)傳感器缺乏文檔,似乎功能不全,因?yàn)樗划a(chǎn)生噪音,而不檢測蜂鳴器發(fā)出的聲音。在教授的指導(dǎo)下,我用一個(gè)簡單的電位器代替了蜂鳴器和麥克風(fēng),這個(gè)電位器可以模擬麥克風(fēng)檢測蜂鳴器聲音的輸出。我花了幾個(gè)小時(shí)重新組織項(xiàng)目,并添加了最后的潤色。但它是這樣的:在最后期限內(nèi)完成,比我最初計(jì)劃的項(xiàng)目范圍付出了更多的努力和工作。
代碼
#include
#include "fix_fft.h"
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
#define inAudio A0
#define inPot A1
char re[128], im[128];
void modLine(int line, int val1, int val2, int val3, int val4){
digitalWrite(CS_PIN, LOW);
/* The standard LED matrix is 8x8;
For some reason, Wokwi uses an 8x32 matrix, so it's as if there are 4 modules connected together.
Therefore, for every 8 columns, a new HEX value must be sent to control the next module.
It's not possible to use a loop or skip modules; each module must be updated every frame,
or the last modules will be treated as non-existent.*/
SPI.transfer(line);
SPI.transfer(val4); // Last 8x8 module
SPI.transfer(line);
SPI.transfer(val3);
SPI.transfer(line);
SPI.transfer(val2);
SPI.transfer(line);
SPI.transfer(val1); // First 8x8 module
digitalWrite(CS_PIN, HIGH);
}
void setup(){
Serial.begin(6900);
pinMode(CS_PIN, OUTPUT);
SPI.begin();
}
void loop(){
int i=0, j=0;
/*This is a conversion of the values I have to apply to the FFT;
FFT: "Fast Fourier Transform";
Basically, it's mathematics (i.e., magic) that converts a signal into sound frequency.*/
//Serial.println(analogRead(inAudio)); //Uncomment accordantly to visualize the frequency input
//Serial.println(analogRead(inPot)); //Uncomment accordantly to visualize the frequency input
for (i = 0; i < 128; i++){
int mic1 = analogRead(inPot); //Potentiometer so we can emulate a microphone
re[i] = mic1 / 4 - 128; // AnalogRead returns value from 0 to 1023, here, we convert it to go from -128 to 127
im[i] = 0; // Imaginary part of the FFT
}
/* Eu vou dar um BEIJO em quem criou essa bib.h*/
fix_fft(re, im, 7, 0);
int height[4] = {0, 0, 0, 0}; //Height for every column
static int prevHeight[4] = {0, 0, 0, 0}; //This for the climb/drop animation of the column of LEDS
int magnitude=0;
for (j = i * 16; j < (i + 1) * 16; j++){ //Considers only the first 64 frequencies
magnitude = magnitude + sqrt(re[j] * re[j] + im[i] * im[i]);
}
//Serial.println(magnitude); //Uncomment to see the resulting magnitude
for (i = 0; i < 4; i++) {
height[i] = map(magnitude * (i + 1) / 4, 0, 2080, 0, 8); //Divides and maps the frequency into 4 major groups that go from 0 to 8 each
height[i] = constrain(height[i], 0, 8); // Guarantees that the value is between 0 e 8
//Serial.println(height[i]); //Uncomment to see the height of each column
}
for (int i = 0; i < 4; i++) { //Here were doing the animation previously mentioned
while (prevHeight[i] != height[i]) {
if (prevHeight[i] < height[i]) {
prevHeight[i]++;
} else {
prevHeight[i]--;
}
for (int j = 0; j < 8; j++) {
int line = 8 - j;
int val1 = (j < prevHeight[0]) ? 0x7E : 0x00;
int val2 = (j < prevHeight[1]) ? 0x7E : 0x00;
int val3 = (j < prevHeight[2]) ? 0x7E : 0x00;
int val4 = (j < prevHeight[3]) ? 0x7E : 0x00;
modLine(line, val1, val2, val3, val4);
}
delay(10); //Slows down the process so that the animation is smoother
}
}
}
本文編譯自hackster.io