Award
Congratulations Richard R, 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.

Scripting Joystick Buttons

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:
 
#1

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

Steve.

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!


#2

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

Code:


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

#3

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

#4

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

#5

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

Code:

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

#6

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

#7

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

#8

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

#9

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 ez builder, 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...

#10

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

Code:


$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 ez builder 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...