PDA

View Full Version : What am I missing here? If..Then



elevenscorpions
- 21st May 2007, 23:08
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


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

mister_e
- 21st May 2007, 23:14
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...

Darrel Taylor
- 22nd May 2007, 02:26
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,

elevenscorpions
- 22nd May 2007, 03:06
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