Welcome to Synthiam!

Program robots using technologies created from industry experts. ARC is our free-to-use robot programming software that makes features like vision recognition, navigation and artificial intelligence easy.

Get Started
United Kingdom
Asked — Edited
Resolved Resolved by Rich!

Help With An "If" Script For Motor Control

I need a little help with a script. Originally I had a script on the mobile interface where if I pressed a button, the motors would drive forward, and pressed again, the motors would stop. Here's what I had using image buttons...

Code:


if (!$pressed)
Set(D1, Off)
Set(D3, Off)
PWM(D0, 70)
PWM(D2, 70)
waitforchange($Direction)

$pressed = 1
ELSE
Set(D1, Off)
Set(D3, Off)
PWM(D0, 0)
PWM(D2, 0)
waitforchange($Direction)
$pressed = 0
endif


But I have now changed the mobile image buttons to direction button's (along with a custom Movement Panel which is fully scripted with PWM commands) so they can interface with ping sensors, so changed the above script to the following...

Code:

if (!$pressed)
Forward()
waitforchange($Direction)
$pressed = 1

ELSE
Stop()
waitforchange($Direction)
$pressed = 0
endif


so pressing the button once moves the motors forward, but pressing it again no longer stops the motors. What do I need to change in the second script to get what I'm after?

Thanks.

AI Support Bot
Related Content
Synthiam
United Kingdom
#2  
I never had a stop button. I'm trying not to use a stop button to limit the amount of buttons on the mobile control. I have my control buttons laid out a certain way and would like to keep them that way if possible.
PRO
Synthiam
#3  
Here you go...

1) Make sure you have initiated the variable in your On Connection script or somewhere before attempting to use it in an IF condition (see video demonstration)

2) You can always test a script using the RUN command while editing (See video demonstration)

United Kingdom
#4  
Thanks DJ.

I already have a variable in my connection script which I used with the first script I posted, so no worries there. I was already aware of, and use the "Run" option when writing a script. I tried to play the video you posted, but it's not working, saying " This is a duplicate video that has already been uploaded".
United Kingdom
#6  
Lol, now it's asking me to sign in to watch the video. Do you have it set to private?
PRO
Synthiam
#7  
I'll check. YouTube really messed this video upload up! Lol they must be have some technical issues.
United Kingdom
#9  
Just a side note that perhaps I should have mentioned in regards to using "Test", when I run the first script with PWM using "Test", the option to "Stop" the script doesn't show up (which is correct in this example) and the first half of the script executes. When I press "Run" again the second part of the script executes which replaces the first half... and so on.

But doing the same on the second script using Forward() and Stop(), pressing "Run" executes the first half of the script, then "Stop" shows up. And when I press that, the script stops running and says "Done #####". So the second half of the script doesn't seem to be recognised. *confused*
United Kingdom
#10  
Looks like YouTube really messed up that video upload. The video still won't play. What was it going to show anyway, as all I'm really asking is what in my script (the second one in post #1) needs changing for it to work the way it should?

If it's going to show how to place a variable as an INIT script in a connection script, I already have that (using $pressed = 0). And as mentioned I am fully aware of what the "Run" function does in a script config menu, and I have been using it for months now.

As mentioned, the first script runs fine and all I've really done is replace the GET() and PWM() codes with FORWARD() and STOP() codes.
United Kingdom
#11  
Why not simplify it using the $direction variable in ARC?

Code:


IF($direction = "stop")
Forward()
ElseIf($direction = "forward")
Stop()
Else
# Robot is turning or moving in reverse. Do nothing?
EndIf


NB. Check the $direction variable exists to begin with. Also double check the directions ARC Movement Panel sets it to.
United Kingdom
#12  
Thanks Rich.

Sorry for the delayed response as I've only just got around to testing it. Just to note that the $direction variable was already set as I already had it working in the first script (post#1). I had already checked to see if the Movement Panel buttons were scripted and mapped correctly (mentioned in post#1 also), but triple checked it again anyway and everything was as it should have been. I'm curious to why the second script in post#1 didn't work, as its essentually the same as the first one.

Thanks for the advice though, and for your help.:)