Congratulations DJ Sures, for answering the question and has received an EZ-Credit award! Share what you have learned and create a tutorial to help others by clicking here.

Playing Sound Effects Using Joystick 2

Assistance Requested

Help Steve G with their question and receive $10 of EZ-Credit to get more robots and parts from our store. The following information was provided about their previous efforts searching tutorials for a resolution.

Steve G claims to have checked these sources:

Hi guys.

I have some servos set up with joystick buttons and using ControlCommand(), I have set up some sound effects from a soundboard to play when the buttons are pressed and servos move. Works great.

Here's the thing. I have a servo set up and controlled by joystick 2. What I would also like to do is to play a sound effect I have on the soundboard when this servo is moved, but joystick 2 doesn't have a scripting option on it's configuration menu like the buttons do. Bare in mind I'm still really new and inexperience at using scripts, how do I accomplished this? *confused*


I have found a few conversations, tutorials and activities that may help with your question. Take a look at these links. I've sorted them by what I believe to be most relevant but that is not always the case as I'm still learning.

Also, consider reviewing the Learn section for informative lessons and activities. Check it out!


Make sure you have the Set Variables checked in EZ-Builder's Joystick control...

User-inserted image

Create new EZ-Script Control and use this code...



IF ($JoystickX2 = 1)


# Wait until the sound has played before continuing and making a mess

ELSEIF ($joystickX2 = -1)


# Wait until the sound has played before continuing and making a mess




When the new EZ-Script Control with the above code is running, it will loop for ever until STOPPED. The code will check for the joystick position and play a sound according to it's location. You can monitor the variable values for the different joystick positions by looking at the VARIABLE tab when editing scripts.


There are a few ways to accomplish this...

One, a loop with WaitForChange...


# Code to run when servo moves

Two, a loop with IF for specific position based control...


IF(GetServo(D5) = 90)
# Code to run at position 90
ElseIf(GetServo(D5) > 100 AND GetServo(D5) < 170)
# Code to run if the servo is between position 100 and position 170
ElseIf(GetServo(D5) < 60)
# Code to run if the servo is at position 1 to position 59

Or use the joystick variables (you must enable them in the joystick control)


IF($JoystickY2 > 0.9)
# Code to run when joystick 2 pushed forwards
ElseIf($JoystickY2 < -0.9)
# Code to run when joystick 2 pushed backwards
ElseIf($JoystickY2 = 0)
# Code to run when joystick returned to centre position
Sleep (5000)

If you are using the same joystick as the Movement Panel and you want sounds when moving forwards, reverse or turning you can use the custom Movement Panel and script the directions in there which can include any ControlCommands you need.


And DJ beat me (but I did give more options Winky)


Thanks guys. I will give this a try tommorow morning and let you know how I get on. Smile


Just a quick update. I followed your code DJ, and it worked like a charm. I did take out the sleep() commands as I wanted the sound effects to start instantly and it works great. Thanks so much for that. Grin

Thanks also to Rich for your reply. Yes DJ just picked you to the post, but your help will also come in useful and of course greatly appreciated. Smile



Great to hear ... Keep in mind that you will need a Sleep() in the loop, otherwise you will be looping at the speed of your PC and use all your processing power. A Sleep() pauses the CPU and let's other things happen during the Sleep() time

This is why you will always notice a Sleep() in every loop that anyone will share.


Ahh got it. Misunderstood what the sleep() command did in this instance *eyeroll*. I will put them back in. Thanks for putting me right. While your around, would you mind helping me out with an additional joystick question I have which I posted here in post 11?

Thanks again. Winky