Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 每次我上传我的(播放旋律-)代码到arduino并按下按钮时,它都会跳过数组中相同的音符。怎么会?_C++_Arduino - Fatal编程技术网

C++ 每次我上传我的(播放旋律-)代码到arduino并按下按钮时,它都会跳过数组中相同的音符。怎么会?

C++ 每次我上传我的(播放旋律-)代码到arduino并按下按钮时,它都会跳过数组中相同的音符。怎么会?,c++,arduino,C++,Arduino,我做了一个代码,每当我按下按钮,它就会播放歌曲中的下一个音符。但问题是,出于某种原因,它总是跳过数组中的相同注释 例如(我的代码): int mariomelody[]={ 注E5,注E5,注E5,注E5,注C5,注E5,注G5,注G4, //有两个“音符”,因为第三个音符不起作用 注释C5、注释G4、注释E4、注释A4、注释B5、注释AS4、注释A4、, 注4、注E5、注G5、注A5、注F5、注G5、注E5、, 注释C5、注释D5、注释B4、, //buttonPin坐在地上 int butt

我做了一个代码,每当我按下按钮,它就会播放歌曲中的下一个音符。但问题是,出于某种原因,它总是跳过数组中的相同注释

例如(我的代码):

int mariomelody[]={
注E5,注E5,注E5,注E5,注C5,注E5,注G5,注G4,
//有两个“音符”,因为第三个音符不起作用
注释C5、注释G4、注释E4、注释A4、注释B5、注释AS4、注释A4、,
注4、注E5、注G5、注A5、注F5、注G5、注E5、,
注释C5、注释D5、注释B4、,
//buttonPin坐在地上
int buttonPin=12;
无效设置()
{
//将安装代码放在此处,以便运行一次:
pinMode(按钮输入,输入);
}
void循环()
{
//将主代码放在此处,以便重复运行:
对于(int i=0;i
每个人都知道马里奥的旋律。前三个音符在不同的速度下是相同的(4是8的一半);E(4)E(4)E(4)E(8)。出于某种原因,它只是跳过了第三个
音符(5
),所以我在那里放了另一个
音符(5
),现在它工作“很好”


有人知道它为什么跳过音符吗?是我的密码吗?

我不知道马里奥的旋律,但无论如何我认为你的密码是错的:

试试这个:

void loop()
{
  for (int i = 0; i < sizeof(mariomelody) / sizeof(mariomelody[0]); i++)
  {
    while (digitalRead(buttonPin) == LOW)
    {
      // wait until button is pressed
    }

    tone(8, mariomelody[i], 20);

    while (digitalRead(buttonPin) == HIGH)
    {
      // wait until button is released
    }          
  }
}
void循环()
{
对于(int i=0;i
它似乎正在跳过mariomelody[0],当您启动程序时,针脚12的数字读数将为低。首先,您正在检查是否为高,因此mariomelody[0]音调将不会输出,它从maripmelody[1]开始下一次按时。颠倒For循环的顺序,先检查LOW,然后检查HIGH。我认为它会起作用。谢谢……这就成功了:D
void loop()
{
  for (int i = 0; i < sizeof(mariomelody) / sizeof(mariomelody[0]); i++)
  {
    while (digitalRead(buttonPin) == LOW)
    {
      // wait until button is pressed
    }

    tone(8, mariomelody[i], 20);

    while (digitalRead(buttonPin) == HIGH)
    {
      // wait until button is released
    }          
  }
}