moad
Newbie level 5
Hi all,
I am trying to control 5 servos using 5 flex sensors. I have succesffuly controlled one servo using 1 flex now when i try to control two m finding it very difficult. can any one suggest any ideas as to why im getting this problem. code runs fine without any errors and the sensors work perfectly (checked the ADC on LCD). i have used timer 0 for 1ms to set the servo at 0 degree and when an interrupt occurs a counter counts up to 20ms, once thats reached, the servo ports are turned off. but i dont seem to get any results. someone help please
here is my code!
unsigned int Adcval;
unsigned int Adcval2;
unsigned int i,a, count;
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 bitWrite(char *x, char n, char value) {
if (value)
*x |= (1 << n);
else
*x &= ~(1 << n);
}
//Timer0
//Prescaler 1:8; TMR0 Preload = 6; Actual Interrupt Time : 1 ms
void InitTimer0(){
OPTION_REG = 0x82;
TMR0 = 6;
INTCON = 0xA0;
}
void Interrupt(){
if (TMR0IF_bit){
TMR0IF_bit = 0;
TMR0 = 6;
count++;
PORTB.F0 = 1;
PORTB.F1 = 1;
for(i=0;i<Adcval;i++)
{
Delay_us(1);
}
for(a=0;a<Adcval2;a++)
{
Delay_us(1);
}
if (count == 20){
PORTB.F0 = 0;
PORTB.F1 = 0;
}
}
}
/*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();
InitTimer0();
}while(1);
}
I am trying to control 5 servos using 5 flex sensors. I have succesffuly controlled one servo using 1 flex now when i try to control two m finding it very difficult. can any one suggest any ideas as to why im getting this problem. code runs fine without any errors and the sensors work perfectly (checked the ADC on LCD). i have used timer 0 for 1ms to set the servo at 0 degree and when an interrupt occurs a counter counts up to 20ms, once thats reached, the servo ports are turned off. but i dont seem to get any results. someone help please
here is my code!
unsigned int Adcval;
unsigned int Adcval2;
unsigned int i,a, count;
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 bitWrite(char *x, char n, char value) {
if (value)
*x |= (1 << n);
else
*x &= ~(1 << n);
}
//Timer0
//Prescaler 1:8; TMR0 Preload = 6; Actual Interrupt Time : 1 ms
void InitTimer0(){
OPTION_REG = 0x82;
TMR0 = 6;
INTCON = 0xA0;
}
void Interrupt(){
if (TMR0IF_bit){
TMR0IF_bit = 0;
TMR0 = 6;
count++;
PORTB.F0 = 1;
PORTB.F1 = 1;
for(i=0;i<Adcval;i++)
{
Delay_us(1);
}
for(a=0;a<Adcval2;a++)
{
Delay_us(1);
}
if (count == 20){
PORTB.F0 = 0;
PORTB.F1 = 0;
}
}
}
/*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();
InitTimer0();
}while(1);
}