所以在上周,我一直在处理我的 ESP32 的问题。有关背景信息,我正在从事 Music Meter 项目。我的目标是让 ESP32 对两个音频信号进行采样,并让两组 LED 灯串对两个相应的音频信号做出反应。我在同时使用两个内核(使用 Arduino IDE)时遇到的问题是,当我对音频进行采样时,其中一个内核经常冻结。我知道核心正在冻结并且它不会卡在循环中,因为我设置了一个条件,即如果长时间没有任何活动,其中一个 LED 将改变颜色,并且它从未改变颜色.
同样奇怪的是作为基线测试,我想看看我是否可以控制 LED,但有一个简单的 for 循环可以打开和关闭每个条带,效果很好。只有当我开始在多个内核上采样音频并让 LED 做出反应时才会出现问题。如果我单独做每个核心,那么核心永远不会冻结,它会按照我想要的方式工作。
/* This is in Core 1 */
for (int i = 0; i < MIDDLE_LED_COUNT; i++) {
middle_strip.setPixelColor(i, color);
middle_strip.show();
}
for (int i = (MIDDLE_LED_COUNT - 1); i >= 0 ; i--) {
middle_strip.setPixelColor(i, 0, 0, 0, 0);
middle_strip.show();
}
/* This is in Core 0 */
for (int i = 0; i < LOWER_LED_COUNT; i++) {
lower_strip.setPixelColor(i, color);
lower_strip.show();
}
for (int i = (LOWER_LED_COUNT - 1); i >= 0 ; i--) {
lower_strip.setPixelColor(i, 0, 0, 0, 0);
lower_strip.show();
}
其他需要注意的事项:当 ESP 通过 WiFi.mode( WIFI_MODE_NULL ) 处于“Music Visualizer”模式时,WiFi 已断开连接。我已将所有可能发生变化的变量设置为 volatile,以查看是否会使其变得更好。它稍微改进了它,但并没有解决问题。
在这一点上,我的绳子用完了,我不知道如何解决这个问题。我当前的代码如下:
/* Core 1 */
void musicVisualizer(void) {
middle_strip.clear();
upper_strip.clear();
middle_strip.show();
upper_strip.show();
WiFi.mode( WIFI_MODE_NULL );
delay(100);
// Add code to tell Core 0 to wait until LED's are cleared.
while (vu_meter) {
tweeter_prev = tweeter;
if (analogRead(tweeter_sample) <= 490)
tweeter_adc = 0;
else {
tweeter_adc = (analogRead(tweeter_sample) - 490);
if (tweeter_adc < 0)
tweeter_adc = 0; // Filter for changing reference voltage
}
tweeter_filter.Filter(tweeter_adc);
tweeter = tweeter_filter.Current() * 4; // 6 scaler is for when Alexa is at Volume Level 4
/* LED's turning on and off in response to audio */
if ((tweeter > tweeter_prev) && (tweeter < MIDDLE_LED_COUNT)) {
for (int x = tweeter_prev; x <= tweeter; x++) {
middle_strip.setPixelColor(x, 0, 0, middle_strip.gamma32(BRIGHTNESS));
}
middle_strip.setPixelColor(0, 0, 0, middle_strip.gamma32(BRIGHTNESS)); // Removes status LED if active
tweeter_count = 0;
middle_strip.show();
}
else if (tweeter_prev > tweeter) {
for (int x = tweeter_prev; x >= tweeter; x--) {
middle_strip.setPixelColor(x, 0, 0, 0);
}
middle_strip.setPixelColor(0, 0, 0, middle_strip.gamma32(BRIGHTNESS)); // Removes status LED if active
tweeter_count = 0;
middle_strip.show();
}
else {
tweeter_count += 1;
if (tweeter_count < 1000)
middle_strip.setPixelColor(0, middle_strip.gamma32(BRIGHTNESS), 0, 0, 0);
else
middle_strip.setPixelColor(0, 0, 0, 0, middle_strip.gamma32(BRIGHTNESS));
middle_strip.show();
}
// Blynk.run();
}
/* Leaving VU Meter, returning to current lamp color set */
if (led_on) {
if (!color_white) {
for (int i = 0; i < LOWER_LED_COUNT; i++) {
lower_strip.setPixelColor(i, color);
lower_strip.show();
}
for (int i = 0; i < MIDDLE_LED_COUNT; i++) {
middle_strip.setPixelColor(i, color);
middle_strip.show();
}
for (int i = 0; i < UPPER_LED_COUNT; i++) {
upper_strip.setPixelColor(i, color);
upper_strip.show();
}
}
else {
for (int i = 0; i < LOWER_LED_COUNT; i++) {
lower_strip.setPixelColor(i, 0, 0, 0, lower_strip.gamma32(BRIGHTNESS));
lower_strip.show();
}
for (int i = 0; i < MIDDLE_LED_COUNT; i++) {
middle_strip.setPixelColor(i, 0, 0, 0, middle_strip.gamma32(BRIGHTNESS));
middle_strip.show();
}
for (int i = 0; i < UPPER_LED_COUNT; i++) {
upper_strip.setPixelColor(i, 0, 0, 0, upper_strip.gamma32(BRIGHTNESS));
upper_strip.show();
}
}
}
else {
upper_strip.clear();
middle_strip.clear();
lower_strip.clear();
upper_strip.show();
middle_strip.show();
lower_strip.show();
}
}
/* Core 0 */
void vu_meter_sub(void * parameter) {
while (true) {
esp_task_wdt_init(30, false);
if (vu_meter) {
lower_strip.clear();
lower_strip.show();
while (vu_meter) {
sub_prev = sub;
sub_adc = analogRead(sub_sample);
sub_filter.Filter(sub_adc);
sub = sub_filter.Current();
if ((sub > sub_prev) && (sub < LOWER_LED_COUNT)) {
for (int x = sub_prev; x <= sub; x++) {
lower_strip.setPixelColor(x, 0, lower_strip.gamma32(BRIGHTNESS), 0);
}
lower_strip.setPixelColor(0, 0, lower_strip.gamma32(BRIGHTNESS), 0);
sub_count = 0;
lower_strip.show();
}
else if (sub_prev > sub) {
for (int x = sub_prev; x >= sub; x--) {
lower_strip.setPixelColor(x, 0, 0, 0);
}
lower_strip.setPixelColor(0, 0, lower_strip.gamma32(BRIGHTNESS), 0);
sub_count = 0;
lower_strip.show();
}
else {
sub_count += 1;
if (sub_count < 1000)
lower_strip.setPixelColor(0, lower_strip.gamma32(BRIGHTNESS), 0, 0, 0);
else
lower_strip.setPixelColor(0, 0, 0, 0, lower_strip.gamma32(BRIGHTNESS));
lower_strip.show();
}
}
}
}
}