Community Question

I have got two EZBv4's connected to my project. As you can see in the code below, I have an Ultrasonic sensor connected to controller #1 and I am trying to get it to move a servo one controller #0. When I run it, the servo does not move. Am I missing something in communicating between the two controllers?

Code:


$trigport = 1.D0
$echoport = 1.D1
$proxclose = 50 # Change for minimum distance

:start
:sensor check
$proxsense = GetPing($trigport, $echoport)

IF($proxsense < $proxclose)
servo(0.d2,112)
sleep(2000)
servo(0.d2,130)
sleep(2000)
servo(0.d2,112)
sleep(9000)

elseif($proxsense > $proxclose)
goto(sensor check)
sleep(3000)
endif

Goto(start)

Luis Vazquez
Commented November 2014
@bhouston Ping me on skype
bhouston
Commented November 2014
@Rich, I tried this but the servo still doesn't respond.

Code:


$trigport = "1.D0"
$echoport = "1.D1"
$proxclose = 50 # Change for minimum distance

:start
:sensor check
$proxsense = GetPing($trigport, $echoport)

IF($proxsense < $proxclose)
servo(d2,112)
servospeed(d2,2)
sleep(2000)
servo(d2,130)
sleep(2000)
servo(d2,112)
sleep(9000)

elseif($proxsense > $proxclose)
goto(sensor check)
sleep(3000)
endif

Goto(start)


It does what it should when I run it on the console.

Rich
Commented November 2014
The code should move the servo on port D2 of EZ-B0 when the ping sensor reports a value of less than 50. If it's not doing that check your servo and check your sensor.

Also, the elseif isn't needed. If the first argument is false (i.e. ping less than 50) then just using the goto(start) will work fine. The elseif is practically doing the same thing. The sleep after the goto in the elseif is redundant as it'll never get to be executed.

Try this code, it should work;

Code:


$proxclose = 50 # Change for minimum distance

:start
$proxsense = GetPing(1.D0, 1.D1)

IF($proxsense < $proxclose)
servo(d2,112)
sleep(2000)
servo(d2,130)
sleep(2000)
servo(d2,112)
sleep(9000)
endif

sleep(100)

Goto(start)



If it doesn't work try running just the following piece of code;

Code:


servo(d2,112)
sleep(2000)
servo(d2,130)
sleep(2000)
servo(d2,112)

DJ Sures
Commented November 2014
There's a lot of unnecessary logic in the code. This is a much simplier version...

Code:



# Change for minimum distance
$proxclose = 50

:start

$proxsense = GetPing(1.d0, 1.d1)

IF ($proxsense < $proxclose)

servo(d2,112)

servospeed(d2,2)

sleep(2000)

servo(d2,130)

sleep(2000)

servo(d2,112)

sleep(9000)

ENDIF

sleep(3000)

Goto(start)

bhouston
Commented November 2014
Thanks Rich and DJ, got it working now
Question
Avatarby bhouston
Published Sunday, November 9, 2014