0

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

1 Answer 1

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];
}
2
  • operands -> operator
    – Borgleader
    Commented Nov 22, 2016 at 20:30
  • Thanks alot! it was so obvious i forgot it!
    – Ken.Yama
    Commented Nov 22, 2016 at 21:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.