Adding a button to start code

I'm adding a button to make a song play when its pushed once. Here is the start code:

void loop()
{
  //sing the tunes
  sing(2);
  sing(1);
  sing(1);
  sing(3);
  sing(3);
  sing(4);
  sing(3);
  sing(5);
  sing(6);
  sing(6);
  sing(6);
  sing(6);
  sing(2);
  sing(4);
}

I tried doing this

void loop(){
  switchState = digitalRead(2);
  if (switchState == LOW) {
    //sing the tunes
    sing(2);
    sing(1);
    sing(1);
    sing(3);
    sing(3);
    sing(4);
    sing(3);
    sing(5);
    sing(6);
    sing(6);
    sing(6);
    sing(6);
    sing(2);
    sing(4);
  }
}

but then I had to hold down the button for it to play, and it started in the middle of the song. How can I push the button once and make it start playing the song?

Complete code:
https://github.com/Banansplitt/Starlight-Muse/blob/master/NoButton

Complete code with a button that isn't working:
https://github.com/Banansplitt/Starlight-Muse/blob/master/WithButton%200.1

Please post your complete code. Otherwise it is impossible to help you.

Sorry, I’m new to this. Added a github link to the code

Thank you for the link, but that is not your code, that seems to be the code that is working. There is no switch in the code you provided.
Please post your code.

Banansplitt:
Sorry, I'm new to this. Added a github link to the code

Please make it easy to help you by posting the code here. See How to use the Forum

...R

Dermaptera:
Thank you for the link, but that is not your code, that seems to be the code that is working. There is no switch in the code you provided.
Please post your code.

Added it at the bottom

Robin2:
Please make it easy to help you by posting the code here. See How to use the Forum

...R

I tried but it went over the character limit, so GitHub was the easiest way to add it

I tried your code, because I could not see anything obviously wrong with it, and it seems to work fine for me. How is your button wired? Are you using a hardware pullup resistor? Is it between the buzzer and the pin? That would explain, why you have to hold it down to play.
And with your code it plays all the songs (several times) once the button is pressed. Is that, what you want?

Banansplitt:
I tried but it went over the character limit, so GitHub was the easiest way to add it

Then just post your .ino file as an attachment.

...R

Dermaptera:
I tried your code, because I could not see anything obviously wrong with it, and it seems to work fine for me. How is your button wired? Are you using a hardware pullup resistor? Is it between the buzzer and the pin? That would explain, why you have to hold it down to play.
And with your code it plays all the songs (several times) once the button is pressed. Is that, what you want?

My button is connected to a resistor that connecting it to the buzzer, and it’s connected to pin 2 and pin 3. There is a resistor between the button and the buzzer. The resistor and pin 2 are on the same side of the button. After the buzzer there is a resistor and then the ground if that matters.

I only want to play the songs once, not in a loop

Robin2:
Then just post your .ino file as an attachment.

...R

I used the online editor, will do next time

Why would you connect it like this?
Connect your button to pin 2 and ground and use INPUT_PULLUP instead of INPUT for pinmode.
And I think it is really hard to understand a circuit if you just describe it. It is much easier if you draw it.
Yes, you are right, it plays the song once. I didn't notice that were parts of one song.

That worked, I see now that I was thinking the wrong way. It makes much more sense to do it like this. I will draw it next time. Thank you so much for the help :slight_smile: