Using Accelerometer to play sounds

Hi Thunkers,

I am trying to play a musical note using the accelerometer to trigger the note.

The issue I’m having is the note retriggers super quickly and glitches out. I just want it to play once for each movement. I’ve tried experimenting with ‘wait’ and restricting the range without much luck.

Thank you :pray:

print the error block see what’s the error in beetwen.

it will leave a json like

}
error:loading failed. server cannot reach. try again
status:ERROR
type:ERR_LOADINGFAILED_SERVER_TRYAGAIN
}

sorry there was an error as

your project was great. i don’t know we could do THAT.

Hi Vishruth,

Thanks for your reply.

I’ve tried to print the error block but it’s not giving me any error message. I think the issue is more with the blocks than the component itself.

I guess what I want is a block that says “Play once” then I can do use a wait block so that it doesn’t retrigger 100 times per second.

Anyone know if that is possible?

I was wondering if I could create a variable to tell it to wait before retriggering but couldn’t quite get my head around it.

2 Likes

You’re right that the error block isn’t going to help here. Have you already tried placing a wait block inside the when Play is done block? What is the duration of the sound file you’re using?

2 Likes

Hi Tatiang,

Yep I have tried the wait block however it still has the issue of playing ~100 times per second. I’ve tried with the wait before and after play, same problem though unfortunately!

If I Live Test these blocks in Thunkable Live on an iPhone 11 and tap the button repeatedly, it only plays the sound once until completion and then repeats.

You can try it on the Sound bug? screen:

https://x.thunkable.com/copy/4cf84590e6208be576b1fa815591fcff

So that tells me that Thunkable is firing the when Play is done section only after the sound completes playing. But you didn’t mention… what is the duration of your sound file?

1 Like

I also tried blocks similar to yours and they only play the complete sound file once when the accelerometer changes.

https://x.thunkable.com/copy/4ad46dd67fc93a5fef16b78b0c76b8c3

What device are you testing this on?

1 Like

Hi tatiang,

Thanks for looking into this.

Sound files duration is 1.5 seconds,

I’ve been testing on Android (Oppo something something).
Just tested on my iPhone 6S which seems a slightly less glitchy. Since I restricted the range (between 2-3) its not tooooo bad. The problem is when the phone is held at the angle that triggers the note, it plays (super fast) repeatedly.

I’m also a bit confused because the accelerometer seems to be functioning as a gyroscope (as far as my limited understanding goes). That is, the values are changing based on the angle I hold the phone, rather than how fast I move the phone.

1 Like

PS totally off topic but how do you like the iPhone 11? I was thinking of getting once since they are not a bad price at the moment :sweat_smile:

I like it… I started with an iPhone 6, then 8, then 11. It’s been a nice upgrade.

now, I’ve never heard an android crash before( :angel: unless I dodn’t know what to do with iOS)
OK, here

android.shakeOmeter.mesaure(return.app.thunkable.musicshake);
get.shakeOmeter.value(value){[
  {value.x}=number:{value.y}=number_
     play(sound.mp3);
  ]
}

could please check the sound source or if it deosn’t contain .mp3

Hey Vishruth,

The sound file is .mp3.

For the code here, did you say before I should run something as a JSON? I’m not exactly sure what you’re asking me to do? If you could block it out that might enlighten me a bit :slight_smile:

the code what code that runs. if surely check;

app.android(getfunction(play sound  beat sence shakeOmeter);
-android code-

OK.

I’m really sorry but I’m not sure how to do that?

2 Likes

varies by your software. download the app and test it on your mobile device.

you’ll need to run the latest version. if you updated your computer. if not, try going to setting and
acess to unknown appand then <Thunkable live/your downloaded app> and then sound to true.

hmmm. I experienced the same problem. and for the app same as you! set sencivity to high.
now test it on your mobile device

@domhnallohanlon I wonder if you could help me out here?
Is there a block that will let me just play a sound once, when the accelerometer changes?
At the moment because the accelerometer is changing 1000 times a second, the sound is restarted many times per second and sounds glitchy.

this worked for me.

there’s NOTHING with the sound. just you guesed it!

it’s deafault sencivity is low. means it will sence AFTER the device is shaken. set this to high
image

One option would be to check for the accelerometer changing and then check to see if enough time has passed by comparing the “seconds since 1970” block to a variable storing that value when the accelerometer changed. If the difference is greater than some amount (say, 0.1 seconds or 0.5 seconds), then play a sound. So you’re essentially adding a timer to the accelerometer event.

2 Likes

Thanks tatiang for the suggestion –
This really stretched my :brain: but I was able to work it out in the end, and the result was as I hoped!

For anyone that would like to see how it works -
https://x.thunkable.com/copy/b2d932fe90e79fb5275ccab1e08117cc

2 Likes