Ping Sensor With Servos
NWCCTV
Posts: 3,629
I understand that Servo motors need a "refresh" about every 20 seconds or so. So, in the below code, how would I go about doing this so that they are not interrupted when using my Ping sensors? The code works fine for roughly 20 seconds and then just stops.
'' ***************************************
'' * Ping))) Demo with PST & LED's *
'' * Author: Parallax Staff *
'' * Copyright (c) 2006 Parallax, Inc. *
'' * See end of file for terms of use. *
'' * Started: 06-03-2010 *
'' ***************************************
{{
Code Description : In this example there are two LED's to indicate a distance. If the distance is further
than 6 inches than LED 1 will turn on, and if the distance is closer than 6 inches than LED 2 turns on; while
either LED 1 or 2 is on, the alternate LED will be off. There is a numerical display of the values in the
Parallax Serial Terminal (PST) at 9600 baud (true).
}}
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
PING_Pin = 13 ' I/O Pin For PING)))
LED1 = 0 ' I/O PIN for LED 1
LED2 = 9 ' I/O PIN for LED 2
ON = 1
OFF = 0
Distlimit = 6 ' In inches
VAR
long range
OBJ
ping : "ping"
servo : "PropBoe Servos"
PUB Start
dira[LED1..LED2]~~
outa[LED1..LED2]~
waitcnt(clkfreq + cnt)
repeat ' Repeat Forever
range := ping.Inches(PING_Pin) ' Get Range In Inches
'
if range > Distlimit ' Comparing range to a set value of 6 inches
outa[LED1] := ON ' P1 is on
outa[LED2] := OFF ' P2 is off
servo.Set(14,250)
servo.Set(15,-250)
elseif range < Distlimit ' If range is further than 6 inches
outa[LED1] := OFF ' P1 is off
outa[LED2] := ON ' P2 is on
servo.Set(14,-10)
servo.Set(15,-10)
{{

Comments
What happens when range is equal to DistLimit?
I would change this:
elseif range < Distlimit
to this:
else
"ping" is written in Spin and I don't believe it uses a separate cog for timing.