Controlling multiple servos using multiple sensors MikroC on easypic v7

Thread Starter

rocker123uk

Joined Dec 6, 2015
33
Hello all,

I am now trying to control multiple servos each by a flex sensor. i have successfully got the first flex with one servo working, however, i cant seem to get it working when i try it with two servos. any idea or suggestion as to how i could achieve this? im trying to control 5 servos using 5 flex sensor. also i dont believe there is anything wrong with the circuit or hardware as it all worked fine. im assuming its a firmware/software problem.

here is my code:

C:
unsigned int Adcval;
unsigned int Adcval2;
unsigned int i,a;

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}


void servoRotate0() //0 Degree
{
  //unsigned int i;
  PORTB.F0 = 1;
  Delay_us(500);
  for(i=0;i<Adcval;i++)
  {
    Delay_us(1);
  }
  PORTB.F0 = 0;
   Delay_us(19500);
}
void servoRotate1() //0 Degree
{
PORTB.F1 = 1;
  Delay_us(500);
  for(a=0;a<Adcval2;a++)
  {
    Delay_us(1);
  }
  PORTB.F1 = 0;
   Delay_us(19500);
}


void main()
{
  int test1, test2;
  TRISA.F2 = 1; //analogue IP servo1
  TRISA.F3 = 1; //analogue IP  servo2
  TRISB = 0; // PORTB as Ouput Port
  ADC_init();
  do
  {
    Adcval = ADC_Read(2);
    Adcval = map(Adcval, 455, 588, 0, 255);
    servoRotate0();
  
    Adcval2 = ADC_Read(3);
    Adcval2 = map(Adcval2, 455, 588, 0, 255);
    servoRotate1();

  }while(1);
}]\code] Mod edit: code tags
 
Last edited by a moderator:

jjw

Joined Dec 24, 2013
594
What is the power supply for the servos?
What will be the minimum value from the map function?
You could make a test by halving the 19.5 ms delays.
 
Last edited:

Thread Starter

rocker123uk

Joined Dec 6, 2015
33
i have checked all of that. im pretty sure the circuit is fine. i think i need to do a interrupt but i dont know how to implement it
 
Top