简体   繁体   English

在Arduino IDE中使用数组

[英]Using an array in the Arduino ide

Hi i'm trying to use an array to make a code for the arduino to play music using a piezo speaker, however it's having trouble receiving data from the array. 嗨,我正在尝试使用数组为arduino编写代码,以便使用压电扬声器播放音乐,但是从数组接收数据时遇到了麻烦。 Please help, thanks! 请帮忙,谢谢!

int y = 0;
int x = 1600;
int song[8]={653,4,494,8,523,8,578,4};
int dur;
int note;
void setup() {
  Serial.begin(9600);
}

void loop() {
  int n = 0;
  while (n<2){
    Serial.print(y);
    if (n=0){
      note = song[y]; 
      Serial.print(song[y]) ;
    }
    else if(n=1){
      dur = song[y+1];
    }
    n++;
  }
  Serial.print(note);
  tone(11,note);
  delay(x/dur);
  y+=2;
  if (y>7){
    y = 0;
  }
}

You're not using the proper operator in your if statements. 您没有在if语句中使用适当的运算符。 Change your if statement to this... 将您的if语句更改为此...

if (n==0){
  note = song[y]; 
  Serial.print(song[y]) ;
}
else if(n==1){
  dur = song[y+1];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM