What am I missing here? If..Then


Closed Thread
Results 1 to 4 of 4
  1. #1

    Default What am I missing here? If..Then

    What am I missing here? I'm trying to set a couple of software limit switches using the feedback potentiometer on a linear actuator. The extend and retract logic works fine, but the analog input variable in the IF..AND..THEN doesn't seem to do anything. The linear actuator will run full stroke, rather than stopping at about 20% and 80% of travel. I set up a LED o/p to come on at about 80% of stroke, and it works OK. I would appreciate any input.

    BrianB

    Code:
    define  OSC 4
    Define	ADC_BITS	8	' Set number of bits in result
    Define	ADC_CLOCK	3	' Set clock source (3=rc)
    Define	ADC_SAMPLEUS	50	' Set sampling time in uS
    
    ANSEL = %11110000 ' Set analog pins
    ANSELH = %00001111
    TRISA = %00001100 ' Set port A
    TRISB = %00110000 ' Set port B 
    TRISC = %11001111 ' Set port C 
    
    AN11 var byte   'Cylinder 1 Feedback Pot.
    SW1EXT var bit  'Extend Switch
    SW2RET var Bit  'Retract Switch
    J var byte
    
    'Initialize Variables and Ports
    PORTA=0
    PORTB=0
    PORTC=0
    
    MAIN:
    'Poll Switches
    If PortA.3=0 then
        pause 20
        if PORTA.3=0 then
            SW1EXT=1
        endif
    Else
        SW1EXT=0
    ENDIF
    If PortA.2=0 then
        pause 20
        if PORTA.2=0 then
            SW2RET=1
        endif
    else
        SW2RET=0
    endif
    adcin 11, AN11 'Cylinder 1 Pot
    IF AN11>200 then 'LED for Cylinder 1 Pot 80% Stroke Indicator
        PORTA.1=1
    else
        PORTA.1=0
    ENdif
    IF (SW1EXT=1) and (AN11<200) then 'Extend Cylinder 1
        PORTC.4=1
        PORTB.7=0
        PORTC.5=1
    endif
    IF (SW2RET=1) and (AN11>50) then 'Retract Cylinder 1
        PORTC.4=0
        PORTB.7=1
        PORTC.5=1
    endif
    if SW1EXT=0 and SW2RET=0 then 'Stop Cylinder 1
        PORTC.4=0
        PORTB.7=0
        PORTC.5=0
    endif
    if SW1EXT=1 and SW2RET=1 then 'Stop Cylinder 1
        PORTC.4=0
        PORTB.7=0
        PORTC.5=0
    endif
    for J=1 to 50 'Heartbeat
    pause 1
    NExt J
    toggle PORTA.0
    
    goto main
    Last edited by Darrel Taylor; - 21st May 2007 at 23:36. Reason: Added [code][/code] tags.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    It's a good practice to use the parenthesis in your code as you did for the first ones.. then you change your mind later...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Brian,

    It looks like you have to Hold the button down to extend or retract the actuator.

    But, the only way to stop the actuator from moving is to have both

    SW1EXT=0 and SW2RET=0

    -or-

    SW1EXT=1 and SW2RET=1

    It won't allow it to stop according to the A/D reading because you're still holding one button down.

    HTH,
    DT

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Thanks. I got fixated on the idea that I didn't understand some aspect of the if..then..else statement (which is also probably true), and forgot that I have to actively turn the actuator off.

    BrianB

Similar Threads

  1. Missing a bit
    By l_gaminde in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 31st August 2009, 18:54
  2. I am missing something, what is wrong with this code?
    By manta_z in forum mel PIC BASIC
    Replies: 3
    Last Post: - 26th April 2009, 23:15
  3. PIC18F4550 Error[128] Missing Arguments
    By damalu in forum mel PIC BASIC
    Replies: 9
    Last Post: - 17th November 2008, 21:47
  4. MCP23016 missing D7 on I2CRead
    By TWSK in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th June 2007, 18:37
  5. What am I missing
    By PaulB in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 12th November 2004, 16:15

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts