gpt4 book ai didi

audio - arduino tone命令问题

转载 作者:行者123 更新时间:2023-12-03 00:49:26 25 4
gpt4 key购买 nike

我正在尝试使用模拟传感器作为按键来制作Arduino钢琴。但是,所有键都产生相同的音调,所产生的音调不会出现在程序中的任何位置。同时按下多个键时,该音调会在两个期望的音调之间交替,并且不会产生不确定的音调。我检查了按下键时是否还激活了其他命令,并且只有在按下相应的键时它们才会激活。

#define Note_C 65.41   //Hz   
#define Note_D 73.42 //Hz
#define Note_E 82.41 //Hz
#define Note_F 87.31 //Hz
#define Note_G 98 //Hz
#define Note_A 110 //Hz
#define threshold 1000


const int speaker=3;
const int B_1=A0; // pins A0-A5 have sensors attatched to them
const int B_2=A1; // pins 13-8 are being used to power each sensor
const int B_3=A2;
const int B_4=A3;
const int B_5=A4;
const int B_6=A5;
const int P_1=13;
const int P_2=12;
const int P_3=11;
const int P_4=10;
const int P_5=9;
const int P_6=8;


int val_1=0;
int val_2=0;
int val_3=0;
int val_4=0;
int val_5=0;
int val_6=0;


void setup()
{
Serial.begin(9600);
pinMode(P_1, OUTPUT);
digitalWrite(P_1, HIGH);
pinMode(P_2, OUTPUT);
digitalWrite(P_2, HIGH);
pinMode(P_3, OUTPUT);
digitalWrite(P_3, HIGH);
pinMode(P_4, OUTPUT);
digitalWrite(P_4, HIGH);
pinMode(P_5, OUTPUT);
digitalWrite(P_5, HIGH);
pinMode(P_6, OUTPUT);
digitalWrite(P_6, HIGH);
}


void loop()
{
analogRead(B_1); // checks each sensor value and stores it
delay(1); // each value must be checked twice with a
val_1=analogRead(B_1); // delay inbetween to provide consistant values
analogRead(B_2);
delay(1);
val_2=analogRead(B_2);
analogRead(B_3);
delay(1);
val_3=analogRead(B_3);
analogRead(B_4);
delay(1);
val_4=analogRead(B_4);
analogRead(B_5);
delay(1);
val_5=analogRead(B_5);
analogRead(B_6);
delay(1);
val_6=analogRead(B_6);

if (val_1 < threshold)
{
tone (speaker, Note_C);
Serial.println("1");
}
else
{
noTone (speaker);
}
if (val_2 < threshold)
{
Serial.println("2");
tone (speaker, Note_D);
}
else
{
noTone(speaker);
}
if (val_3 < threshold)
{
Serial.println("3");
tone (speaker, Note_E);
}
else
{
noTone (speaker);
}
if (val_4 < threshold)
{
Serial.println("4");
tone (speaker, Note_F);
}
else
{
noTone (speaker);
}
if (val_5 < threshold)
{
Serial.println("5");
tone (speaker, Note_G);
}
else
{
noTone (speaker);
}
if (val_6 < threshold)
{
Serial.println("6");
tone (speaker, Note_A);
}
else
{
noTone (speaker);
}
noTone(speaker);
}

我知道数组会更有效,但是我想先让它工作。我也是一名新手程序员,因此我们将不胜感激。

最佳答案

我怀疑问题在于,每次循环时,您都在不断激活和停用音调。我认为更好的方法是在激活特定输入时启动音调,并继续播放直到禁用输入为止。 (您不能使用tone()同时播放多个音符。)

您需要将当前正在播放的音调的编号存储在变量中。这样的事情可能会起作用:

int currentTone = 0;

void loop()
{
// (take your analog readings here)


if (val_1 < threshold)
{
if (currentTone == 0)
{
tone(speaker, Note_C);
currentTone = 1;
}
}
else if (currentTone == 1)
{
noTone(speaker);
currentTone = 0;
}

if (val_2 < threshold)
{
if (currentTone == 0)
{
tone(speaker, Note_D);
currentTone = 2;
}
}
else if (currentTone == 2)
{
noTone(speaker);
currentTone = 0;
}

// etc.
}

这将通过每个输入来检查它是否已被激活。如果是这样,并且如果当前没有其他音调在播放,则它将开始播放相应的音符。如果输入未激活,但正在播放其相应的音符,它将停止输入。

关于audio - arduino tone命令问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23069702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com