Here are the Servo Code example which I referred to

1 2

Example 1

' Servo testing example

const servoPin as byte = 12 ' the pin the servo is on
const refreshPeriod as single = 0.02 ' the time between servo pulses


' you may need to change the minimum and maximum pulseWidths for
' your servo to get the full range of motion. The ideal servo
' has a minimum of 1 milliseconds (0.001) and a maximum of 2 milliseconds (0.002).
' In practice, however, this varies.
' connect your servo and try different values for the constants
' below:


const minPulse as single = 0.0003 ' the minimum pulseWidth
const maxPulse as single = 0.0022 ' the maximum pulseWidth

dim pulseWidth as single ' the servo's pulsewidth


dim servoStack(1 to 30) as byte ' memory for the servoTask


Sub main()
call delay(0.5) ' start program with a half-second delay

debug.print "start"

' set initial values for variables:
pulseWidth = minPulse

' start the servoTask loop running:
callTask "servoTask", servoStack

' the main loop:
do
pulsewidth = minPulse
debug.print "minimum angle"
call sleep(2.0)

pulseWidth = ((maxPulse - minPulse) / 2.0) + minPulse
debug.print "middle"
call sleep(2.0)

pulseWidth = maxPulse
debug.print "maximum angle"
call sleep(2.0)

' this sleep makes sure your main loop runs
' as much as possible.
call sleep(0.0)
loop
End Sub

sub servoTask()
do
call pulseOut(servoPin, pulseWidth, 1)

' wait 20 milliseconds before pulsing again
call sleep(refreshPeriod)
loop
end sub

 

Example 2

this code is on the Tom's site

' Servo testing example
' in this example, a servo attached to pin 12
' should travel slowly from one side to the other,
' then quickly back to the beginning.
' The program starts pulsing the servo at its
' minimum pulsewidth, then increments the pulsewidth
' a small amount with each new pulse until it reaches
' the maximum pulsewidth. Then it sets the
' pulsewidth back to minimum.

dim minPulse as single ' the minimum pulseWidth
dim maxPulse as single ' the maximum pulseWidth
dim pulseWidth as single ' the servo's pulsewidth
dim refreshPeriod as single ' the time between pulses

Sub main()
call delay(0.5) ' start program with a half-second delay

' set initial values for variables:

' you may need to change the minimum and maximum
' pulseWidths for your servo to get the full
' range of motion. The ideal servo
' has a minimum of 1 milliseconds (0.001) and
' a maximum of 2 milliseconds (0.002). In practice,
' however, this varies. Connect your servo and try
' different values for the max and min below:

minPulse = 0.0008
maxPulse = 0.0022
pulseWidth = minPulse
refreshPeriod = 0.02

' the main loop:
do

' pulse the servo:
call pulseOut(12, pulseWidth, 1)

' increase the pulsewidth for the next pulse:
if pulseWidth <= maxPulse then
pulseWidth = pulseWidth + 0.00001
else
pulseWidth = minPulse
end if

' wait 20 milliseconds before pulsing again
call sleep(refreshPeriod)
loop
End Sub

back

Journal Index