Ken.Yama Ken.Yama - 16 days ago 4
C++ Question

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;
}
}

Answer

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];
}