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 Dunning-Kruger!

Advice For Elseif Script

I need a little help with the following script. Basically what it is supposed to do is to get accelerometer information iPhone Sensor Stream to control a motor controller to go forwards when phone is tilted forwards, stop when tilted back to a resting position, and reverse when the phone is tilted backwards.

I have managed to get the motors going forwards at an accelerometer reading of 0.3000, and stop the motors at less than 0.2000. But when I add the code for going in reverse, the EZ-B disconnects. What I need is for the motors to stop between readings of less than 0.2000, and more than -0.2000. I suspect there is a conflict in the script below...

Code:

:loop

#Move forwards
if($AccelerometerY >0.3000)
Set(D1, Off)
Set(D3, Off)
PWM(D0, 40)
PWM(D2, 40)
sleep(1000)

#Move reverse
elseif($AccelerometerY <-0.3000)
Set(D1, On)
Set(D3, On)
PWM(D0, 40)
PWM(D2, 40)
sleep(1000)
endif

#Stop
elseif($AccelerometerY <0.2000 >-0.2000)
Set(D1, On)
Set(D3, On)
PWM(D0, 0)
PWM(D2, 0)
sleep(1000)
endif

goto(loop)


Any ideas what I have done wrong (i suspect it's something to do with #Stop), and an idea of what the correct script should look like?

Thanks.

#1  
One thing you forgot is the "or" statement in the last elseif.... and yes the extra endif needs to be removed as @WBS00001 observed...

Code:


elseif($AccelerometerY <0.2000 or $AccelerometerY >-0.2000)
#2  
The main problem is the "endif" statement at the end of the #Move reverse section. Needs to be removed.

EDIT
I should add Richard is also correct, but it should be an "and" and not an "or".
#3  
Heck, instead of just saying "Deleted" in this post I might as well use it for some purpose, no matter how insignificant. :)

Since only one of the possibilities can run at any one loop, you can factor out the common "Sleep" statement and remove them from all the If-ElseIf sections and place just the one sleep statement just before the Goto(loop) command.

There, saved this post from total uselessness.
United Kingdom
#4  
Thanks Richard. I tried what you said using the OR, and it worked fine. The EndIf cropped up by accident when copy/pasting the thread *confused*. It wasn't in he original script.

Anyway, thanks again, and to WBS as well. That was a great help.:)
United Kingdom
#5  
@WBS.

Just saw your post edit. Thanks for the additional info buddy. Very grateful.
United Kingdom
#6  
Just to note for anyone else reading this, the AND that WBS suggested worked too.

Thanks again guys. Much appreciated.:)
#7  
Being the obsessive-compulsive concerning technology I am, I just have to pick a nit here regarding the use of an "OR or "AND" conjunction in the final ElseIf evaluation. :D Sorry Richard.

With an OR in there, 0.3000 would be a valid number in that evaluation. Likewise -0.3000 is valid. But, you excluded anything greater than 0.3000 by the first If statement. and anything less than -0.3000 by the next (ElseIf) statement. So anything greater than +0.3000 or less than -0.3000 is excluded before the script gets to the last ElseIf statement. However, that leaves the range of -0.3000 to +0.3000 as getting to the last ElseIf statement.

Using an OR in there means anything less than 0.2000 is valid. -0.3000 is valid. In theory, anything between +0.1999 and minus infinity is valid. Also it means anything greater than -0.2000 is also valid. +0.3000 is valid, as is any number above -0.2000 right up to infinity. Again, the only reason it's not quite like that is because of the first two If-ElseIf evaluations.

So, the thing is, using an OR in there leaves a lot of room for possible errors down the road and does not do what you really wanted to do. That is, limit the evaluation to numbers between -0.1999 and above, and anything 0.1999 and below. Only using an AND will do that.