United Kingdom
Asked — Edited
Resolved Resolved by DJ Sures!

Playing Sound Effects Using Joystick 2

Hi guys.

I have some servos set up with joystick buttons and using control command, 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

Cheers, Steve.


ARC Pro

Upgrade to ARC Pro

Stay at the forefront of robot programming innovation with ARC Pro, ensuring your robot is always equipped with the latest advancements.

PRO
Synthiam
#1  

Make sure you have the Set Variables checked in ARC's Joystick control...

User-inserted image

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


:loop

IF ($JoystickX2 = 1)
  
  CODE TO PLAY SOUND HERE

  # Wait until the sound has played before continuing and making a mess
  sleep(5000)
  
ELSEIF ($joystickX2 = -1)
  
  CODE TO PLAY SOUND HERE

  # Wait until the sound has played before continuing and making a mess
  sleep(5000)
  
ENDIF 
  
  
sleep(1000)

goto(loop)

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.

United Kingdom
#2  

There are a few ways to accomplish this...

One, a loop with WaitForChange...


:loop
WaitForChange(GetServo(D5))
# Code to run when servo moves
Sleep(5000)
Goto(loop)

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


:loop
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
EndIf
Sleep(5000)
Goto(loop)

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


:loop
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
EndIf
Sleep (5000)
Goto(loop)

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.

United Kingdom
#3  

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

United Kingdom
#4  

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

United Kingdom
#5  

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. :D

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. :)

Steve.

PRO
Synthiam
#6  

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.

United Kingdom
#7  

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. ;)