Hi Sai, You need to maintain similar logic to this (need to wait variable time for duty cycle and you should not worry about how many cycles you are doing this -- you need to maintain servo pulses all times anyway, so it is better just limiting movements). BTW: You also can have one potmeter to active speed and direction: if you have a number from 0..1023 then subtract 512 from it and you will get +-512 so the upper half position will cause servo go upwards, lower position to go downwards... Anyway, here is the pseudo code with the button + potmeter version: // loop forever while (1) { // put your potmeter acquisition here .... // change servo position by speed and direction if ( GPIO & (1<<3) ) { pw +=3D quotient ); } else { pw -=3D quotient ); } // limit servo movements if ( pw < 1000 ) { pw =3D 1000; } if ( pw > 2000 ) { pw =3D 2000; } // wait for variable time for ( i =3D pw; i > 0; i-- ) { __delay_us(1); } __delay_ms(18); } Tamas On 15 February 2012 02:55, yamanoor sairam wrote: > Hello All, > > I guess this is my third mail on the same topic. I am trying to control a > servo motor using PIC 12F675. I was successful in driving the servo as we= ll > as achieving the speed control for the servo. Thanks for the help from th= e > members of this list. > > Now, my speed control is quite flaky. I tried tweaking my code here and > there but I have had no success so far. > > My main objective is that the servo should spin 0 to 90 and back based on > the data from toggle switch. I achieved this goal. When I turn a > potentiometer, the speed at which the servo rotates should change. I > achieved this too. However, the servo is not rotating 90 degrees at all t= he > time. > > It rotates 90 ish -120 degrees at high speeds and rotates only 30-45 > degrees on low speeds (or some weird behavior is happening). I want my > servo to rotate 90 degrees but just my speed should vary. > > Can any one point me to the error in my code? > > All this code is inside a while loop. > > GO_DONE=3D1; > while(GO_DONE); > V_in=3D(ADRESH<<8)|ADRESL; > quotient=3DV_in/200; // I am dividing this because I will have integers > between 1 to 5 > if(quotient<=3D1) > { > quotient=3D1; > } > if(GPIO&(1<<3)) > { > while(counter>1000) > { > counter=3Dcounter-(20*quotient); > GPIO|=3D(1<<2); > cnt_1=3Dcounter; > while(cnt_1>0) > { > __delay_us(1); > cnt_1--; > } > GPIO&=3D~(1<<2); > __delay_ms(18); > } > cnt=3D1000;//reset of counter for reverse direction > } > elseif(!(GPIO&(1<<3))) > { > while(cnt<1500) > { > cnt=3Dcnt+(20*quotient); > GPIO|=3D(1<<2); > cnt_2=3Dcnt; > while(cnt_2>0) > { > __delay_us(1); > cnt_2--; > } > GPIO&=3D~(1<<2); > __delay_ms(18); > } > counter=3D1500;//reset of counter for forward direction > } > } > > > Any help is appreciated > > Sai > } > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > --=20 int main() { char *a,*s,*q; printf(s=3D"int main() { char *a,*s,*q; printf(s=3D%s%s%s, q=3D%s%s%s%s,s,q,q,a=3D%s%s%s%s,q,q,q,a,a,q); }", q=3D"\"",s,q,q,a=3D"\\",q,q,q,a,a,q); } --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .