Servo Motors

Started by servostep May 13, 2010
Hello,

I could make 2 servo motors work using 2 switches. However after about 45 seconds, it doesn't work anymore. It seems frozen. Please help.

Tony

Here is my code:

const servoPin as byte = 10 ' the pin the servo is on
const servoPin2 as byte = 14 ' the pin the servo is on

const minPulse as single = 0.0004 ' the minimum pulseWidth
const maxPulse as single = 0.0024 ' the maximum pulseWidth

dim pulseWidth as single ' the servo's pulsewidth
dim pulseWidth2 as single ' the servo's pulsewidth

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

dim switchON as byte 'create a variable called switchON that is a byte
dim switchON2 as byte 'create a variable called switchON that is a byte

Sub main()

switchON = 0
switchON2 = 0
'''''''''''''' CONFIGURE SERVO MOTOR '''''''''''''''''''''''''''
' set initial values for variables:
pulseWidth = minPulse

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

'for button=======' This program reads the state of a switch and toggles the
' state of an output pin whenever the switch is pressed.

Const InputPin As Byte = 16
Const InputPin2 As Byte = 15
' Configure pins.
Call PutPin(InputPin, 1)
Call PutPin(InputPin2, 1)

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

' the main loop:
Do
' Toggle State if switch is pressed.
If GetPin(InputPin) = 1 Then
if switchON = 1 then

'do nothing

else

switchON = 1 'set switchON to be 1

pulseWidth = maxPulse
call Delay(1.0)
pulseWidth = minPulse

end if

else 'Switch is not on
switchON = 0 'is if the switch is not on, set the value of SwitchONg to 0 (old
end if

'''for servo #2

' Toggle State if switch is pressed.

If GetPin(InputPin2) = 1 Then
if switchON2 = 1 then

'do nothing

else
'servo======== switchON2 = 1 'set switchON2 to be 1

pulseWidth2 = maxPulse
call Delay(1.0)
pulseWidth2 = minPulse

end if

else 'Switch is not on
switchON2 = 0 'is if the switch is not on, set the value of SwitchONg to 0 (old
end if

Loop
End Sub
'''''''''''''' SERVOTASK CALLED AT LAUNCH '''''''''''''''''
sub servoTask()
do
call pulseOut(servoPin, pulseWidth, 1)
' wait 20 milliseconds before pulsing again
call sleep(0.02)

call pulseOut(servoPin2, pulseWidth2, 1)
'wait 20 milliseconds before pulsing again
call sleep(0.03)
loop
End sub

That would be my guess too. Take it to 50, and if that fixes the problem, back it down a few bytes at a time until it breaks - then finish by going to the last value that was solid.
Gene

----- Original Message -----
From: Tom Becker
To: b...
Sent: Saturday, May 15, 2010 4:20 PM
Subject: Re: [BasicX] Servo Motors

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

Try a larger stack?

Tom



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

Try a larger stack?
Tom
Is there an alternative way to make it work? I tried different stack numbers and it would keep freezing after about 40 seconds. Is there a sample code I could test for 2 servo motors?

Thanks,

Tony

--- In b..., "Gene HAM" wrote:
>
> That would be my guess too. Take it to 50, and if that fixes the problem, back it down a few bytes at a time until it breaks - then finish by going to the last value that was solid.
> Gene
>
> ----- Original Message -----
> From: Tom Becker
> To: b...
> Sent: Saturday, May 15, 2010 4:20 PM
> Subject: Re: [BasicX] Servo Motors
>
> > ... dim servoStack(1 to 30) as byte ' memory for the servoTask
>
> Try a larger stack?
>
> Tom
>
>
>
>

Replace "Delay" with sleep and see what happens. Delay is a PIG and should
NEVER be used.

The best way to do it would be to use the integer version of pulseout.
It requires less overhead since there would be no floating point math
involved in your task.
Chris
> Is there an alternative way to make it work? I tried different stack
> numbers and it would keep freezing after about 40 seconds. Is there a
> sample code I could test for 2 servo motors?
>
> Thanks,
>
> Tony
>
> --- In b..., "Gene HAM" wrote:
>>
>> That would be my guess too. Take it to 50, and if that fixes the
>> problem, back it down a few bytes at a time until it breaks - then
>> finish by going to the last value that was solid.
>> Gene
>>
>> ----- Original Message -----
>> From: Tom Becker
>> To: b...
>> Sent: Saturday, May 15, 2010 4:20 PM
>> Subject: Re: [BasicX] Servo Motors
>>
>> > ... dim servoStack(1 to 30) as byte ' memory for the servoTask
>>
>> Try a larger stack?
>>
>> Tom
>>
>

Regards,

Chris Harriman
Engineer
NetMedia, Inc.
1+(520)544-4567 Ext. 192