Control 4 Servos - 4 Pots - Same PIC
I am interested in controlling 4 Servos, sensing 4 Pots Connected to the 4 Analog Inputs of 16F873.
I tried with just one Pot and One Servo , works just fine , But with 4 Pots and four Servo's, it jerks.
I am sure , i am doing some thing wrong and need some help. I am running the PIC @ 4Mhz.
Mainloop:
LOW SERV1 : LOW SERV2 :LOW SERV3 :LOW SERV4 :
GOSUB READ_POTS
'-----------------------------------------------------------------
SERVO = 150*TEMP1 : SERVO = SERVO / 255 : SERVO = SERVO + 90
pulsout serv1 ,servo : PAUSE 15
'-----------------------------------------------------------------
'-----------------------------------------------------------------
SERVO = 150*TEMP2 : SERVO = SERVO / 255 : SERVO = SERVO + 90
pulsout serv2 ,servo : PAUSE 15
'-----------------------------------------------------------------
'-----------------------------------------------------------------
SERVO = 150*TEMP3 : SERVO = SERVO / 255 : SERVO = SERVO + 90
pulsout serv3 ,servo : PAUSE 15
'-----------------------------------------------------------------
'-----------------------------------------------------------------
SERVO = 150*TEMP4 : SERVO = SERVO / 255 : SERVO = SERVO + 90
pulsout serv4 ,servo : PAUSE 15
'-----------------------------------------------------------------
pause 18
GOTO Mainloop
END
READ_POTS:
Adcin 0, TEMP1 ' Read FIRST POT
Adcin 1, TEMP2 ' Read SECOND POT
Adcin 2, TEMP3 ' Read THIRD POT
Adcin 3, TEMP4 ' Read FOURTH POT
RETURN
regards
add a little more code so you only react to changes
Performing calculations and a series of reads in a row is your major prob. Here's an idea - (pseudo code):
#1:
define prev_value vars
Loop:
for i = 1 to 4
read one pot (based on i)
check to see if it changed (compared to a previously saved value)
if changed, calc new value
pulse_out each of your motors
next i
goto loop
This way you're only doing one A/D read per loop, and only doing the math when the value of the pot changes (multiplication and (even more so) division are clock cycle suckers!
------------------
#2: do away with the gosubs in speed-critical situations. In-line code is harder to read, but faster
Regards,
Does anyone have a full program (MPLAB) done on project list below
[QUOTE=charudatt;23362]I am interested in controlling 4 Servos, sensing 4 Pots Connected to the 4 Analog Inputs of 16F873.