2. I can not add potentiometer and servo motor.
You connect a second pot in the same way as the first, e.g. on AN1.
You connect a second servo in the same way as motor 1, e.g. on RB1.
I cannot help much with the coding of a PIC. However, You probably need to change the program structure. Currently you sare generating the PWM waveform in a subroutine using delay statements. This kind of delay is created by having the CPU running in a loop for a defined number of iterations to "burn" time. The CPU is not able to do usefil work during this time.
It is preferable to haev the PIC hardware generate the PWM signals autonomously as
described e.g. here. You'll have to connect the servos to PC.1 and PC.2 for this to work.
The idea is that you set up the registers of the hardware PWM channels such that these hardware module generate the signals without further intervention. You'll have to change only the register settings for changing the ducty cycle.
As for the analog input you'll have to read both input channels to get the settings of the two potentiometers.
A rather general code could look like this (you'll have to work out the detailed code as I'm not familiar with PICs):
Code:
setup ();
while (1)
{
ADC1 = read_ADC_channel_1();
ADC2 = read_ADC_channel_2();
set_PWM_channel_1(ADC1);
set_PWM_channel_2(ADC2);
}
Alternatively, using parameterized routines for reading the ADC and setting the PWM:
Code:
setup ();
while (1)
{
ADC1 = read_ADC_channel(1);
ADC2 = read_ADC_channel(2);
set_PWM_channel(ADC1, 1);
set_PWM_channel(ADC2, 2);
}