# Using Accelerometer to play sounds

**URL:** <https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215>\
**Category:** Questions about Thunkable X\
**Created:** [September 25, 2021, 9:48am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215 "2021-09-25T09:48:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlinmclellan](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/carlinmclellan/32/77831_2.png) [@carlinmclellan](https://community.thunkable.com/u/carlinmclellan)\
**Post date:** [September 25, 2021, 9:48am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/1 "2021-09-25T09:48:54Z")

</div>

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 🙏

 ![Screen Shot 2021-09-25 at 7.47.49 pm](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/3/4/34cd4d50851e7aea969478ffff788a6c475596a0.png)

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [September 25, 2021, 11:23am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/2 "2021-09-25T11:23:35Z")

</div>

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

it will leave a `json` like

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

```

sorry there was an error as

> [@carlinmclellan](#):
>
> The issue I’m having is the note retriggers super quickly and glitches out

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

---

<div class="post-metadata">

**Author:** ![carlinmclellan](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/carlinmclellan/32/77831_2.png) [@carlinmclellan](https://community.thunkable.com/u/carlinmclellan)\
**Post date:** [September 26, 2021, 12:56am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/3 "2021-09-26T00:56:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [September 26, 2021, 2:41am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/4 "2021-09-26T02:41:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![carlinmclellan](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/carlinmclellan/32/77831_2.png) [@carlinmclellan](https://community.thunkable.com/u/carlinmclellan)\
**Post date:** [September 26, 2021, 2:55am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/5 "2021-09-26T02:55:40Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [September 26, 2021, 3:32am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/6 "2021-09-26T03:32:43Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [September 26, 2021, 3:40am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/7 "2021-09-26T03:40:32Z")

</div>

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](https://x.thunkable.com/copy/4ad46dd67fc93a5fef16b78b0c76b8c3)

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/3/7/3787bc0fc1c5101ee2dbcf08ab1e851788c5e954.png)

What device are you testing this on?

---

<div class="post-metadata">

**Author:** ![carlinmclellan](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/carlinmclellan/32/77831_2.png) [@carlinmclellan](https://community.thunkable.com/u/carlinmclellan)\
**Post date:** [September 26, 2021, 4:01am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/8 "2021-09-26T04:01:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![carlinmclellan](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/carlinmclellan/32/77831_2.png) [@carlinmclellan](https://community.thunkable.com/u/carlinmclellan)\
**Post date:** [September 26, 2021, 4:08am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/9 "2021-09-26T04:08:08Z")

</div>

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 😅

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [September 26, 2021, 4:12am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/10 "2021-09-26T04:12:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [September 26, 2021, 6:05am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/11 "2021-09-26T06:05:45Z")

</div>

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

```auto
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`**

---

<div class="post-metadata">

**Author:** ![carlinmclellan](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/carlinmclellan/32/77831_2.png) [@carlinmclellan](https://community.thunkable.com/u/carlinmclellan)\
**Post date:** [September 26, 2021, 7:40am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/12 "2021-09-26T07:40:12Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [September 26, 2021, 8:07am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/13 "2021-09-26T08:07:32Z")

</div>

> [@carlinmclellan](#):
>
> JSON

the code what code that runs. if surely check;

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

```

OK.

---

<div class="post-metadata">

**Author:** ![carlinmclellan](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/carlinmclellan/32/77831_2.png) [@carlinmclellan](https://community.thunkable.com/u/carlinmclellan)\
**Post date:** [September 26, 2021, 8:20am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/14 "2021-09-26T08:20:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [September 26, 2021, 8:28am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/15 "2021-09-26T08:28:28Z")

</div>

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 app** and 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

---

<div class="post-metadata">

**Author:** ![carlinmclellan](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/carlinmclellan/32/77831_2.png) [@carlinmclellan](https://community.thunkable.com/u/carlinmclellan)\
**Post date:** [October 1, 2021, 10:30am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/16 "2021-10-01T10:30:23Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [October 1, 2021, 11:33am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/17 "2021-10-01T11:33:47Z")

</div>

> [@vishruth-ram](#):
>
> I experienced the same problem. and for the app same as you! set `sencivity` to high.

this worked for me.

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [October 1, 2021, 11:37am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/18 "2021-10-01T11:37:18Z")

</div>

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

> [@carlinmclellan](#):
>
> accelerometer

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

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/9/6/964edd49b1d670f9f2df9075ec7cc58ab82732f6.png)

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [October 1, 2021, 4:49pm UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/19 "2021-10-01T16:49:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![carlinmclellan](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/carlinmclellan/32/77831_2.png) [@carlinmclellan](https://community.thunkable.com/u/carlinmclellan)\
**Post date:** [October 2, 2021, 8:18am UTC](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215/20 "2021-10-02T08:18:37Z")

</div>

Thanks tatiang for the suggestion –  
This really stretched my 🧠 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](https://x.thunkable.com/copy/b2d932fe90e79fb5275ccab1e08117cc)

 ![Screen Shot 2021-10-02 at 6.16.21 pm](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/5/85d3bc7080908aebd8e3caae2d47a05719561df5.png)

[Next page](https://community.thunkable.com/t/using-accelerometer-to-play-sounds/1526215.md?page=2)
