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.

Advice For Elseif Script

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

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

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)

#3

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

#4

Heck, instead of just saying "Deleted" in this post I might as well use it for some purpose, no matter how insignificant. Smile

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.

#5

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

#6

@WBS.

Just saw your post edit. Thanks for the additional info buddy. Very grateful.

#7

Just to note for anyone else reading this, the AND that WBS suggested worked too.

Thanks again guys. Much appreciated. Smile

#8

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