code of <Watering the Flower>

March 11, 2003

' connecting Servo Motor with FSR

' 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.
' a FSR value ranges from 0 to 1023
' when I use 10k register, I get a FSR range of value with a cup of water, from 0 to 340(430)
' if the cup is 0mm far from the FSR upper limit line, the maxmum value is 350(430).
' if the cup is 5mm far from the FSR upper limit line, the maxmum value is 470 ~ 550(560).

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
dim pulseRange as single ' the range of possible pulses
dim FSRvalue as integer ' dim FSR value as integer

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

minPulse = 0.0008
maxPulse = 0.0025
pulseWidth = minPulse
refreshPeriod = 0.02

' set initial values for variables:

pulseRange = maxPulse - minPulse

' 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:


' the main do loop:
do
' read a variable resistor sensor on pin 13:
FSRvalue = getADC(13)
debug.print "FSRvalue = " ; cstr(FSRvalue)


if pulseWidth <= maxPulse then ' if pulseWidth <= maxPulse then
if ( FSRvalue = 0 ) then
pulseWidth = minPulse
end if

if ( FSRvalue > 0 ) and ( FSRvalue < 450 ) then

'pulseWidth = minPulse + ((pulseRange * cSng(FSRvalue)) / 1023.0)
pulseWidth = minPulse + ((pulseRange * cSng(FSRvalue)) / 450.0)
end if

if (FSRvalue > 450) then
pulseWidth = maxPulse
end if

else
pulseWidth = minPulse
'end if
end if

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

call sleep(refreshPeriod)

loop


End Sub

back

Journal Index