United Kingdom
Asked — Edited
Resolved Resolved by Dunning-Kruger!

Scripting Joystick Buttons

Hello there.

I need a little help with something, but I'm not sure if it is even possible. Is there a way to use one joystick button to move a servo in to two positions with two presses. Here's what I mean...

Servo is at center (possition 90)

Press button 1 and servo turns left (possition 1).

No button release script so servo stays left (possition 1).

Press button 1 again, and servo moves back to center (possition 90).

No button release script, so servo stays center (possition 90).

Press button 1 and servo turns left (possition 1)

and so on.

I have had a look through the script menu but have not seen anything to help me accomplish this (unless I missed it of course) and I'm not sure if it is even possible. I would like to do this just so I can free up some buttons on my controller.

If anyone has had experience with this or know what script to use, then your help would be most appreciated. :)

Steve.


ARC Pro

Upgrade to ARC Pro

Join the ARC Pro community and gain access to a wealth of resources and support, ensuring your robot's success.

#1  

Of course... behind the button is scripts that will run when the button is pressed or released... You will have to set $button1=1 somewhere else in an init type of script prior to using the button the first time...

in the script of whatever button you want to use to run the servo..


if $button1=1
servo(D0,1) #when button is pressed servo will move to position 1
$button1=0
else
servo(D0,90) #when button is pressed again servo will move to position 90
$button1=1
endif

#2  

Here is where you enter the above script in the joystick control, (button tab)

User-inserted image

United Kingdom
#3  

@Richard.

Thanks for the script example. That was exactly what I was looking for and I had a feeling this would be possible. Still really new at scripting so I'm still finding my way around. I already knew where and how to add script as I have been using this already, but it's what script to use and what some of the scripts do is what I need to understand better. Some of the script examples in the menu for me are easy enough to understand and self explanitory, but some I am still trying to get my head around confused but I'll get there.

Will try your script a bit later and let you know how I got on.

United Kingdom
#4  

Ok, so i tried the script and ran in to a problem. What am i doing wrong?

if $button1=1
servo(D13,88)
$button1=0
else
servo(D13,62)
$button1=1
endif
#5  

Is the script mapped to the correct button? What is the script doing or not doing exactly?

#6  

@Steve G.... What do you mean by scripting buttons? So we're not talking about the joystick buttons at all then? Can you post a screen shot, maybe?

United Kingdom
#7  

Yeah, I am talking about joystick buttons. I click on to button 1 multi script and put the code in for when button 1 is pressed but no script for when button 1 is released, saved it then saved again. I go to try the script and the diagnostic list says "error in line 1". Battery is charging at the mo, so will do a screen shoot in a bit.

User-inserted image

User-inserted image

#8  

if you get an error in line one it might be because your program needs to set $button1 to equal 1 in another script before you use the joystick control...

Although you do not need to declare variables in ARC, you still need to set a preliminary value (sort of) before you use them for the first time...

I mentioned to use an init script and type or even in your servo speed script if you like $button1=1

then use the joystick control...

#9  

@Steve G... We should keep everything here as it all pertains to the same questions you are asking..

Just start a new script control and name it "Init" or whatever you want... type in the following...


$button1=1 #sets the initial value of $button1 for the joystick control

servo(D13,90) #set initial servo position to 90 or whatever
servo(D14,90) #set initial servo position to 90 or whatever
ServoSpeed(D13,3) # 0 is fastest, 10 is slowest here I set it to 3
ServoSpeed(D14,3) # 0 is fastest, 10 is slowest here I set it to 3

#etc. do this for as many servos as you need

Keep it on your desktop of ARC and just run it one at the beginning... that's it. You can run it anytime you like if you ever need to change the speed and then want to go back to a slower speed...

United Kingdom
#10  

@Richard.

Everything seems to be working so thanks for your help (and patience). I do have one additional question for you or anyone who knows.

I have a controller button scripted "when button is pressed" to move a servo from possition 90 to 160, and have nothing scripted on "when button is released" so servo moves from 90 to 160 even if I release the button. What peace of script do I need to write in "when button is released" to stop the servo in its current possition when I do release the button between positions 90 to 160?

I have looked through the script manual and the only thing I thought would work was the STOP() command, but as you probably know that's for a modified servo and doesn't work on a standard one.

United Kingdom
#11  

A few questions...

Is the position always 90 before it moves to 160? What moves it back to 90? Is servo speed set at all?

By using the command Servo() the servo will jump to position rather than move to position. Meaning that stopping the script wont effect the position, it's either 90 or 160.

Using ServoUp() or ServoDown() in a loop can stop mid position.

Using the Auto Position Movement control may also give better results.

United Kingdom
#12  

Hi Rich.

Yes the rest possition is 90 and maximum travel is 160. Button one on the joystick does this.

Servo speed is set to 2 in both directions.

Button 2 returns servo back to 90.

Basically the set up is for a head looking up (button 1 to 160) and looking down (button 2 back to 90) but I would like to stop the head looking up when I release button 1 (anywhere between 90 and 160), and then the same for looking down. Hope that all made sense;).

United Kingdom
#13  

Check out the ServoUp() and ServoDown() commands. Used with some Ifs should achieve what you want.

Have the servo move one position at a time (or more if it's too slow) until it hits the limit.

IF it hits the limit then do nothing. ElseIf it's less than the upper limit (or more than the lower limit depending on direction) then move the servo up or down by x number of positions. It's less code and more efficient to switch that around, so IF it's less than the upper limit move the servo up a position otherwise don't do anything.


IF(GetServo(D1) < 160)
  ServoUp(D1, 1)
EndIf
Sleep(100)


IF(GetServo(D1) > 90)
  ServoDown(D1, 1)
EndIf
Sleep(100)

The sleep and number of servo positions to move in the ServoUp/ServoDown command will determine the movement speed.

PRO
Synthiam
#14  

I think he is putting the code in both Button Pressed and Button Released. Which is why this experience is occurring.

The code that was provided to you for pushing the button only needs to be in one of the two locations.

Do you want the action to run when the button is pressed or do releases? Choose one and place the code in the respective area.

United Kingdom
#15  

Rich, you are my hero.

The little bit of script you supplied worked great :D. I played around with the servo up/down values and got it moving just how I wanted. And a couple of rapid pressed of the joystick button still produced a nice smooth motion. And an added bonus is if the servo buzzes, a quick pulse or 2 of the button stops the buzz.

Again thanks very much for that. Much appreciated :).

United Kingdom
#16  

@DJ.

You posted exactly the same time I just did lol. I've just got home and only just tried it out. No I deleated the script I originally had in the "button release" and now left it blank. I did want it to run on "button pressed". Works great now. :P