【搬运】通过超声波控制LED颜色变化
前面提到日本用户用chirp进行声波通讯,下面的案例无需进行声音验证,无需app,只要通过麦克风采集指定频率的声音就可以控制灯光颜色。这里利用了arduinoFFT这个库,分析声音种的指定频率,由于频率较高,人耳难以察觉,因此可以作为通讯手段夹杂在其他声音当中,作为信号发送。代码通过15KHz频率控制红色,16KHz频率控制绿色,17KHz控制蓝色。同样的M5StickC的屏幕也显示相应颜色,由于LED色彩为RGB888,而屏幕色彩为RGB565,因此需要进行转换。用到了三个库,i2s与neopixel可以参考相关例程.原文地址https://homemadegarbage.com/m5stickc02#include <M5StickC.h>
#include <driver/i2s.h>
#include "arduinoFFT.h"
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel pixels(6, 26, NEO_GRB + NEO_KHZ800);//定义26引脚
#define PIN_CLK0
#define PIN_DATA 34//I2S麦克风引脚
#define READ_LEN (2 * 1024)
#define SAMPLING_FREQUENCY 44100
uint8_t BUFFER = {0};
uint8_t red = 0x00, green = 0x00, blue = 0x00;
uint16_t *adcBuffer = NULL;
const uint16_t FFTsamples = 256;// 采样数
double vReal;//将采样保存到数组
double vImag;
arduinoFFT FFT = arduinoFFT(vReal, vImag, FFTsamples, SAMPLING_FREQUENCY);// 创建FFT对象
unsigned int sampling_period_us;
float dmax = 10000.0;
void i2sInit(){ //配置I2S
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
.sample_rate =44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // is fixed at 12bit, stereo, MSB
.channel_format = I2S_CHANNEL_FMT_ALL_RIGHT,
.communication_format = I2S_COMM_FORMAT_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 2,
.dma_buf_len = 128,
};
i2s_pin_config_t pin_config;
pin_config.bck_io_num = I2S_PIN_NO_CHANGE;
pin_config.ws_io_num = PIN_CLK;
pin_config.data_out_num = I2S_PIN_NO_CHANGE;
pin_config.data_in_num= PIN_DATA;
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM_0, &pin_config);
i2s_set_clk(I2S_NUM_0, 44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
}
void mic_record_task (void* arg){
while(1){
i2s_read_bytes(I2S_NUM_0,(char*)BUFFER,READ_LEN,(100/portTICK_RATE_MS));
adcBuffer = (uint16_t *)BUFFER;
fft();
vTaskDelay(100 / portTICK_RATE_MS);
}
}
void setup() {
Serial.begin(115200);
M5.begin();
M5.Axp.ScreenBreath(9); //屏幕亮度,最大12
pixels.begin();
pixels.setBrightness(50); //LED亮度
sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY)); //计算采样率
i2sInit();
xTaskCreatePinnedToCore(mic_record_task,"mic_record_task",2048,NULL,1,NULL,1);
}
void fft(){
for (int i = 0; i < FFTsamples; i++) {
unsigned long t = micros();
vReal = adcBuffer;
vImag = 0;
while ((micros() - t) < sampling_period_us) ;
}
FFT.Windowing(FFT_WIN_TYP_HAMMING, FFT_FORWARD);// 视图
FFT.Compute(FFT_FORWARD); // FFT处理 复数计算
FFT.ComplexToMagnitude(); // 复数与实数转换
int nsamples = FFTsamples/2;
for (int band = 0; band < nsamples; band++) { //计算频段频率
float d = vReal / dmax;
Serial.print(band);
Serial.print(" : ");
Serial.print((band * 1.0 * SAMPLING_FREQUENCY) / FFTsamples / 1000);
Serial.print("kHz : ");
Serial.println(d);
}
//15kHz
if(vReal / dmax > 0.3){
red = 0xff;
}else{
red = 0x00;
}
//16kHz
if(vReal / dmax > 0.3){
green = 0xff;
}else{
green = 0x00;
}
//17kHz
if(vReal / dmax > 0.3){
blue = 0xff;
}else{
blue = 0x00;
}
uint32_t RGB888 = red << 16 | green << 8 | blue;
Serial.println(RGB888);
for(int i=0; i<6; i++) {
pixels.setPixelColor(i, RGB888);
}
pixels.show();
//RGB888转RGB565
uint16_t RGB565 = (((RGB888 >> 19) & 0x1f) << 11) | (((RGB888 >> 10) & 0x3f) <<5) | (((RGB888 >>3) & 0x1f));
M5.Lcd.fillScreen(RGB565);
}
void loop() {
}
页:
[1]