我想使用 Arduino Nano 和每个 channel 的 5 个 LED 创建一个音频 VU 表。
问题是我找不到/创建执行此操作的程序。
代码
//STEREO LED VU
const int leftLedCount = 5;
const int rightLedCount = 5;
const int leftLed[leftLedCount] = {2,3,4,5,6};
const int rightLed[rightLedCount] = {7,8,9,10,11};
int leftInput, l;
int rightInput, r;
void setup()
{
for (l = 0; l < leftLedCount; l++)
pinMode(leftLed[l], OUTPUT);
for (r = 0; r < rightLedCount; r++)
pinMode(rightLed[r], OUTPUT);
testLEDs();
}
void testLEDs()
{
for (l = 0; l < leftLedCount; l++)
digitalWrite(leftLed[l], HIGH);
for (r = 0; r < rightLedCount; r++)
digitalWrite(rightLed[r], HIGH);
delay(1000);
turnOffLEDs();
}
void turnOffLEDs()
{
for (l = 0; l < leftLedCount; l++)
digitalWrite(leftLed[l], LOW);
for (r = 0; r < rightLedCount; r++)
digitalWrite(rightLed[r], LOW);
}
void loop()
{
leftInput = analogRead(A0)/12,5;
rightInput = analogRead(A1)/12,5;
//LEFT
for(l=0; l <= leftInput; l++)
{
digitalWrite(leftLed[l], HIGH);
}
//RIGHT
for(r=0; r <= rightInput; r++)
{
digitalWrite(rightLed[r], HIGH);
}
//TURN OFF LEDS
turnOffLEDs();
}
问题:
- 几分钟后它完全停止工作:随机有几个 LED 保持亮起
- 灯光与声音无关
我认为问题在于更改输入电平,因为我不明白应该如何分配信号电平。任何形式的小帮助都会有所帮助。
最佳答案
您可能想在这些表达式中编写 12.5
而不是 12,5
:leftInput = analogRead(A0)/12,5; rightInput = analogRead(A1)/12,5;
但即便如此,analogRead
返回值从 0
到 1023
(包括).因此 left/rightInput
的值范围从 0
到大约 81
,而您需要它在 0 的范围内。 ..4
。我建议设置另一个阈值数组,并根据信号强度点亮所有高于阈值的 LED,例如(未测试)添加它而不是你的 loop()
:
const int thresholdsLeftLed[leftLedCount] = { 4, 208, 412, 616, 820 };
const int thresholdsRightLed[rightLedCount] = { 4, 208, 412, 616, 820 };
void LightLEDs(int signalLeft, int signalRight)
{
for (int l = 0; l < leftLedCount; ++l)
digitalWrite(leftLed[l], signalLeft > thresholdsLeftLed[l] ? HIGH : LOW);
for (int r = 0; r < rightLedCount; ++r)
digitalWrite(rightLed[r], signalRight > thresholdsRightLed[r] ? HIGH : LOW);
}
void loop()
{
LightLEDs(analogRead(A0), analogRead(A1));
}
请注意,您可能不需要关闭所有 LED,因为如果信号低于阈值,它们会在循环中关闭
关于c++ - Arduino 立体声音量表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50319029/