
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.
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)
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".
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*
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.
Code:
NB. Check the $direction variable exists to begin with. Also double check the directions ARC Movement Panel sets it to.
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.