Canada
Asked — Edited
Resolved Resolved by Rich!

How Do You Get Two Ezb'S To Work Together?

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?


$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)



ARC Pro

Upgrade to ARC Pro

Harnessing the power of ARC Pro, your robot can be more than just a simple automated machine.

United Kingdom
#1  

$trigport and $echoport need to be in quotes.


$trigport = "1.D0"
$echoport = "1.D1"

However, and I know that I am guilty of this but ports shouldn't really be defined by variables. If they aren't going to change in your project you might as well hard code them in.

Also, I'm not sure if it's the forums or if it's the way you intended but it doesn't show the less than sign in the if statement.


$proxclose = 50 # Change for minimum distance

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

IF($proxsense < $proxclose)
goto(sensor check)
sleep(3000)
endif

Goto(start)

And finally, where's the code to move the servo? It looks like it's missing the line

Servo(D0, 90)

Presuming the servo is on board 0 and needs to move to position 90.

note: if board 0 is used you needn't use the preceding 0. as part of the port number, it will always assume the board is board 0 unless otherwise stated.

#2  

Sorry, my first post didn't show all of my code, it's all there now. I'll try what you suggested, Rich.

#4  

@Rich, I tried this but the servo still doesn't respond.


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

United Kingdom
#5  

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;


$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;


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

PRO
Synthiam
#6  

There's a lot of unnecessary logic in the code. This is a much simplier version...



# 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)

#7  

Thanks Rich and DJ, got it working now