Hey guys,
I want to build myself a sonic screwdriver from the scifi show Doctor Who. right now I am working on the sound effect. I do this with an SD Card and the TMRpcm library. I want the sound to play when I press the button and the sound should turm off when I am relesing the button. But it seems like whenever I press the button the sound effect plays 100ms (because of the delay) and the jumps to the beginning of the audio file wich results in an annoying sound. But when I do a longer delay, lets say 1 second, it jumps to the start after one secnd of playing. and when I relese the button it will go on for one second before stop playing. So I want the audio to play through when I press it and when I relese it it should stop immediately.
I dont think the mistake has something to do with the else statement in my code because when I delete it the outcome is the same.
Thank you for your help in advance.
I think you need to be detecting the "state change" of your button. When the state of the button changes from not pressed to pressed, call sonic.play(). When it changes from pressed to not pressed, call sonic.stopPlayback().
Your duplicate thread in Audio, which had no replies, has been deleted
Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting will result in a timeout from the forum.
In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.
I dont think it has to do with the button states because as I mentioned before when I delete the else statement the result is still the same. I think it has to do with the library. Maybe.
I attached a picture so you can see the button wirering. when I relese the button the sound stops. but when I am pressing it, the sound will run for how long I put in a delay. so when I say "delay(100);" the sound will run for 100ms and then start at the begining until I relese the button.
the sound itself is 3 - 4 second long.
I am useing an arduino Nano.
That looks ok (so long as those 5V and ground wires are attached to the Arduino on the other side).
If you press the button for 1 seconds and release, does the sound stop immediately, or 1 second later or does it continue to play until the end at 4 seconds? If you remove the else part, does the sound stop as you release the button, or 1 second later or does it play until the end?
PaulRB:
I think you need to be detecting the "state change" of your button. When the state of the button changes from not pressed to pressed, call sonic.play(). When it changes from pressed to not pressed, call sonic.stopPlayback().
Oh sorry now I understand what you meen by that. This will certainly help thank you