Set, Record and Drive of Servo to 2 positions

Hi there,
I'd greatly appreciate any help with this project.
I fear I may have placed this as a post incorrectly before, but have now created a fresh post, as this is starting from scratch really.
A point of thanks to member 120thingsin20years for the structure of a mode selection program (posted Nov 1, 2011), that I'm now using.

I've had a good time trying to get certain things to work in software and hardware, but I must admit to getting tied up in things I don't yet understand entirely.
My frustration then leading to disheartenment.
I think taking some steps forward one by one is perhaps the best approach here, as I really want to understand the details.
In a nut shell, the project is to allow the user to Set, Record and Drive a servo to 2 positions, but with some additional features.

The final program design I'm hoping to achieve will perform with the following hopefully:
  • Be on the smallest chip possible, with the minimum number of exterior components, as physical size needs to be small.
    Prototyping can easily use through-hole components, but the final design will hopefully use surface mount components.
  • Have a mode selection button to cycle through the required different function modes.
  • Mode1 will be the default operation mode on power-up. The PICaxe will have to power-cycle at times, and always needs to default to this mode.
    This mode will use an exterior 'control' input of up to 30v DC (likely to be 22 - 28v), to switch a servo between 2 user defined positions.
    With the control input Low, the servo is already at, or driven to position 1.
    With the control input High, the servo is driven to position 2.
  • Additional mode(s) allow the user to use a potentiometer to sweep the servo arm to a position(s), and record the position(s) in non-volatile memory.
    The two positions recorded will be used by Mode1, when Mode1 is active.
  • An additional mode to cycle the servo between the two positions set (with a slight pause between),
    and for the user to use the potentiometer to set the servo transition speed.
    The speed setting will also be recorded in non-volatile memory, and used by Mode1 when active.
  • A final mode allows the user to toggle the servo between the two positions recorded, using an additional input button.
    The servo stays in position until the input button is pressed again, and is driven to the other position.
    This mode may have a choice of Slow and Fast for the transition speed between positions.
    Exit of the final mode with cycle back to Mode1.
  • Have the mode status indicated using 1 LED.
    Mode1 - LED Off
    Additional function Mode(s) - Different flash rates
    Highest numbered function mode - LED On

I am currently using a Hi-TEC HS-65HB servo which has a very useful maximum 189.5' arm rotation, using 690ms and 2510ms for 180' min and max positioning.
The function modes will allow servo positioning and recording anywhere between these min and max limits.
Pulsout would be my favoured servo drive command, as it seems to avoid the problems of using Servopos.

The following code hopefully starts to build the structure of the program, and currently just very, very simply allows Mode2 to sweep the servo using a 10K pot.
Well that's something anyway!
The mode switching is very stable, and I like the fact the user can Press... Hold... and Release to activate mode changes. It has a very definite and solid feel, with no accidental skipping of modes.
Needless to say that beyond this, when adding additional functionality, I quickly get into issues with timing, servo stutter, etc.

Without delving too far too quickly, I thought I'd check/ask for some info regarding the potentiometer, and the reading of its value to sweep the servo arm.
Will the chosen overall pot resistance have any advantage for the servo setting? i.e. Use of a 5K, 10K, or 20K pot?
I assume it's best to choose this before programming, rather than trying to fix it in software.

When using readadc to read the pot value, would it be advantageous to use greater definition, i.e. readadc10?
A better resolution for more accurate servo positions? and accurately recorded for better repeatability when controlled by Mode1?

Thanks in advance for reading, and any help is much appreciated.

Code:
' Cyclic mode change via 1 button
' Code edited from program by 120thingsin20years
'
' Uses a momentary push button to select through 4 modes of operation
' Currently the mode status is currently displayed on two LEDs
' Mode1 - LED1 off LED2 off
' Mode2 - LED1 on LED2 off
' Mode3 - LED1 off LED2 on
' Mode4 - LED1 on LED2 on
'
' Mode1 is the power-on default mode and will have operation code developed further
' Mode2 currently allows the manual sweep of a servo using a 10K potentiometer
' Mode3 will have operation code developed further
' Mode4 will have operation code developed further
'
#Picaxe 08M2
#No_Data            ' temporary use to speed-up transfer of programming chip

symbol PotIn = c.1    ' 10K pot input pin
symbol Servo_Out = c.2    ' pulse out to servo
symbol ModeLED1 = c.0    ' mode status indication
symbol ModeLED2 = c.4    ' mode status indication

symbol MaxModes = b4    ' max number of operation modes
symbol CurrentMode = b5    ' current operation mode
symbol PotValue = b6    ' limited pot value

pause 2000


Main:                ' power-on default mode
    gosub Init        ' populate some variables
    Continue:
    ' Mode1 operation code to be developed further
    gosub TightLoop    ' sense and change mode
goto Continue
'
end


Init: ' populate some variables
    let MaxModes = 4        ' set max number of modes
    let CurrentMode = 1    ' default operation mode to run under Continue: in Main:
    low c.0            ' currently a temporary display of mode status on LEDs
    low c.4            ' currently a temporary display of mode status on LEDs
'    
return


TightLoop:    ' sense and change mode
    if pin3 = 1 then            ' mode button pressed and held
        gosub WaitForRelease    ' wait for mode button release to change mode
        gosub ChangeMode        ' change mode
    end if
'
return


ChangeMode:
    if CurrentMode = 1 then
        low c.0    ' currently a temporary display of mode status on LEDs
        low c.4
        return    ' return to default Mode1 in Main:
    end if

    if CurrentMode = 2 then
        high c.0    ' currently a temporary display of mode status on LEDs
        low c.4
        gosub Mode2 ' call Mode2 operation
    end if

    if CurrentMode = 3 then
        low c.0    ' currently a temporary display of mode status on LEDs
        high c.4
        gosub Mode3    ' call Mode3 operation
    end if

    if CurrentMode = 4 then
        high c.0    ' currently a temporary display of mode status on LEDs
        high c.4
        gosub Mode4    ' call Mode4 operation
    end if

if CurrentMode > MaxModes then
    let CurrentMode = 1    ' cycle back around to Mode1 after highest numbered mode
end if
'
return


Mode2:
    do until pin3 = 1
        ' Mode2 operation code to be developed further
        readadc PotIn, PotValue
        PotValue = PotValue MIN 69 MAX 251        ' set limits on PotValue, 690ms and 2510ms
        
        pause 10                        ' pause 10ms min value to avoid any servo buzz when
                                    ' using Pot to position servo arm
        pulsout Servo_Out, PotValue
    loop
    gosub WaitForRelease
'
return


Mode3:
    do until pin3 = 1
        ' Mode3 operation code to be developed further
    loop
    gosub WaitForRelease
'
return


Mode4:
    do until pin3 = 1
        ' Mode4 operation code to be developed further
    loop
    gosub WaitForRelease
'
return


WaitForRelease:    ' wait for mode button release
    CurrentMode = CurrentMode + 1    ' increment mode number
    ContinueWaiting:
        do while pinc.3 = 1    ' while mode button is held on
            high c.0        ' use both mode LEDs on to indicate mode is changing
            high c.4
        loop
    low c.0    ' mode LEDs off once mode button is released
    low c.4
'
return
 
Top