Go Down

Topic: How do I get a melody to play after pushing a button? (Read 579 times) previous topic - next topic

253990

I don't know much about code and can't figure out how to play the music after pushing a button. It's for a final and I'm stuck. Any ideas on how to help?



//Set pin as speaker pin
#define speakerPin 8


#define NOTE_G4  392
#define NOTE_C5  523
#define NOTE_E5  659
#define NOTE_B3  247
#define NOTE_G5  784

   
// notes in the melody(reveille:
int melody[] = {
  NOTE_G4,NOTE_C5,NOTE_E5,NOTE_C5,NOTE_G4,NOTE_E5,
  NOTE_C5,NOTE_E5,NOTE_C5,NOTE_G4,NOTE_E5,NOTE_C5,
  NOTE_E5,NOTE_C5,NOTE_G4,NOTE_C5,NOTE_E5,NOTE_C5,
  NOTE_G4,NOTE_C5,NOTE_E5,NOTE_C5,NOTE_G4,NOTE_E5,
  NOTE_C5,NOTE_E5,NOTE_C5,NOTE_G4,NOTE_E5,
  NOTE_C5,NOTE_E5,NOTE_C5,NOTE_G4,NOTE_G4,NOTE_C5

};
 

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  8,8,16,16,8,8,8,16,16,8,8,8,16,16,8,8,4,8,8,8,16,
  16,8,8,8,16,16,8,8,8,16,16,8,8,4
};

void setup() {
 
 pinMode(8, OUTPUT);
}


 int button = 0;

void loop() {
 
 button = digitalRead(13);
  //If button is pushes play speaker
  if(button == HIGH) {
    digitalWrite(speakerPin,HIGH);
  delay(1000);
}
//If button is not pushed don't play speaker
  else(button == LOW); {
    digitalWrite(speakerPin,LOW);
  delay(10);
  }
     
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1300;
    delay(pauseBetweenNotes);
  }

//Turn off music
noTone(8);

}

slipstick

You have a bit of code that checks if a button is pushed. And you have a for loop that plays a melody. But there doesn't seem to be any real connection between them.

Why not put the if statement that checks the button immediately before the for loop so it has a chance of controlling it?

BTW why do have to do a program for your final if you don't know much about code?

Steve

253990

Thanks for the reply. I tried your suggestion but it still didn't work. Don't know if it was just me something else. Also to answer your question, in physics, we did a pretty brief unit on code and didn't go into much depth. My teacher was really enthusiastic about teaching it to us. The final was to make an original piece of code so he could see what we learned and how creative we were but with limited knowledge there little we can do.

slipstick

When you change the code (as in "tried your suggestion") please post the changed version so we can see what you've done and maybe help you to fix it.

Steve

cattledog

Code: [Select]
button = digitalRead(13);

What is connected to pin 13 and how is it wired?

Go Up