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

Help With An "if" Script For Motor Control

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

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.

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

Don't you have a stop button now?

#3

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.

#4

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)

#5

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

#6

try again

#7

Lol, now it's asking me to sign in to watch the video. Do you have it set to private?

#8

I'll check. YouTube really messed this video upload up! Lol they must be have some technical issues.

#9

No worries. Smile

#10

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*