简体   繁体   中英

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. 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. Change your if statement to this...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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