Drive dc-motors with analog values

59 Views Asked by At

I have a problem with my project and I need your hep.
My project is to control a robotic arm with 4 dc motors with a PIC16F877A from a gui and "teach" them some moves. I have written the code for the movements with 8 buttons in the gui, up, down, left, right,etc.
I can read the values from the analog pins and send them to pc from serial and now I have problem by drive the motors in the specific positions by reading the analog values.
I have made it with while loop but it run only the 1 motor and then it stop and I cant do anything. At the beginning I can use all the other buttons but if I push 'b' or 'e' it stacks and I cant do anything.
Can you please help??
P.S. Sorry for my English.

unsigned int y0, y1, y2, y3;
char x;
char buffer[15];
void GetDelay(){
      Delay_ms(80);
      }
void main() {

      CMCON = 0x07;
      ADCON1 = 0x80;

      UART1_Init(9600);
      Delay_ms(100);
      ADC_Init();
      Delay_ms(100);
      TRISA0_bit = 1;
      PORTA.F0 = 1;
      TRISA1_bit = 1;
      PORTA.F1 = 1;
      TRISA2_bit = 1;
      PORTA.F2 = 1;
      TRISA3_bit = 1;
      PORTA.F3 = 1;
      TRISB = 0;
      PORTB = 0;
      TRISB0_bit = 1;
      PORTB.F0 = 1;
      TRISB1_bit = 1;
      PORTB.F1 = 1;
      TRISB4_bit = 1;
      PORTB.F4 = 0;
      TRISB5_bit = 1;
      PORTB.F5 = 0;
      TRISC1_bit = 0;
      PORTC.F1 = 0;
      TRISC2_bit = 0;
      PORTC.F2 = 0;
      TRISC3_bit = 0;
      PORTC.F3 = 0;
      TRISC4_bit = 0;
      PORTC.F4 = 0;
      TRISC5_bit = 0;
      PORTC.F5 = 0;
      TRISD0_bit = 1;
      PORTD.F0 = 1;
      TRISD1_bit = 1;
      PORTD.F1 = 1;
      TRISD2_bit = 1;
      PORTD.F2 = 1;
      TRISD3_bit = 1;
      PORTD.F3 = 1;
      TRISD4_bit = 1;
      PORTD.F4 = 1;
      TRISD5_bit = 1;
      PORTD.F5 = 1;
      TRISD6_bit = 0;
      PORTD.F6 = 0;
      TRISD7_bit = 0;
      PORTD.F7 = 0;

PWM2_Init(5000);  //Initialize PWM1
PWM2_Start();  //start PWM1
PWM2_Set_Duty(127); //Set current duty for PWM1

      while(1){
        if(RB4_bit==0){

               if (UART1_Data_Ready() == 1){
                  x = UART1_Read();
               }
               //ARPAGH OPEN
               if (x=='1' ){
                  RC4_bit=0;
                  RC5_bit=1;
                  GetDelay();
                  RC4_bit=0;
                  RC5_bit=0;

               }
               //ARPAGH CLOSE
               else if (x=='2'){
                  RC4_bit=1;
                  RC5_bit=0;
                  GetDelay();
                  RC4_bit=0;
                  RC5_bit=0;
               }
               //2h ARTHRWSH DOWN
               else if (x=='3'){
                  RB3_bit=1;
                  RB2_bit=0;
                  GetDelay();
                  RB3_bit=0;
                  RB2_bit=0;
               }
               //2h ARTHRWSH UP
               else if (x=='4'){
                  RB3_bit=0;
                  RB2_bit=1;
                  GetDelay();
                  RB3_bit=0;
                  RB2_bit=0;
               }
               //1h ARTHRWSH DOWN
               else if (x=='5'){
                  RD6_bit=0;
                  RD7_bit=1;
                  GetDelay();
                  RD6_bit=0;
                  RD7_bit=0;
               }
               //1h ARTHRWSH UP
               else if (x=='6'){
                  RD6_bit=1;
                  RD7_bit=0;
                  GetDelay();
                  RD6_bit=0;
                  RD7_bit=0;
               }
               //PERISTROFH RIGHT
               else if (x=='7'){
                  RC2_bit=1;
                  RC3_bit=0;
                  GetDelay();
                  RC2_bit=0;
                  RC3_bit=0;
               }
               //PERISTROFH LEFT
               else if (x=='8'){
                  RC2_bit=0;
                  RC3_bit=1;
                  GetDelay();
                  RC2_bit=0;
                  RC3_bit=0;
               }
               //ANALOG
               else if (x=='9'){

                   y0 = ADC_Get_Sample(0);
                   sprintl(buffer, "%d", y0);
                   UART1_Write_Text(buffer);
                   UART1_Write_Text(",");
                   y2 = ADC_Get_Sample(2);
                   sprintl(buffer, "%d", y2);
                   UART1_Write_Text(buffer);
                   UART1_Write_Text(",");
                   y1 = ADC_Get_Sample(1);
                   sprintl(buffer, "%d", y1);
                   UART1_Write_Text(buffer);
                   UART1_Write_Text(",");
                   y3 = ADC_Get_Sample(3);
                   sprintl(buffer, "%d", y3);
                   UART1_Write_Text(buffer);
                   //UART1_Write_Text("\n");
                   UART1_Write(13);
               }
               //START POSITION
               else if (x=='b'){
               //ARPAGH OPEN
               while (RD2_Bit==0){
                  RC4_bit=0;
                  RC5_bit=1;
                  GetDelay();
                  RC4_bit=0;
                  RC5_bit=0;
                }
               //2h ARTHRWSH UP
               while (y2>440){
                  RB3_bit=0;
                  RB2_bit=1;
                  GetDelay();
                  RB3_bit=0;
                  RB2_bit=0;
                  y2 = ADC_Get_Sample(2);
                  Delay_ms(100);
                }
               //1h ARTHRWSH DOWN
               while (y1>470){
                  RD6_bit=0;
                  RD7_bit=1;
                  GetDelay();
                  RD6_bit=0;
                  RD7_bit=0;
                  y1 = ADC_Get_Sample(1);
                  Delay_ms(100);
                  }
               //PERISTROFH RIGHT
               while (y3>120){
                  RC2_bit=1;
                  RC3_bit=0;
                  GetDelay();
                  RC2_bit=0;
                  RC3_bit=0;
                  y3 = ADC_Get_Sample(3);
                  Delay_ms(100);
                  }
               }
              //FINAL POSITION
               else if (x=='e'){
               //ARPAGH OPEN
               while (RD2_Bit==0){
                  RC4_bit=0;
                  RC5_bit=1;
                  GetDelay();
                  RC4_bit=0;
                  RC5_bit=0;
                }
               //2h ARTHRWSH DOWN
               while (y2<440){
                  y2 = ADC_Get_Sample(2);
                  Delay_ms(100);
                  RB3_bit=1;
                  RB2_bit=0;
                  GetDelay();
                  RB3_bit=0;
                  RB2_bit=0;

                }
                //2h ARTHRWSH UP
               while (y2>440){
                  y2 = ADC_Get_Sample(2);
                  Delay_ms(100);
                  RB3_bit=0;
                  RB2_bit=1;
                  GetDelay();
                  RB3_bit=0;
                  RB2_bit=0;

                }
               //1h ARTHRWSH UP
               while (y1<470){
                  y1 = ADC_Get_Sample(1);
                  Delay_ms(100);
                  RD6_bit=1;
                  RD7_bit=0;
                  GetDelay();
                  RD6_bit=0;
                  RD7_bit=0;

                  }
               //1h ARTHRWSH DOWN
               while (y1>470){
                  y1 = ADC_Get_Sample(1);
                  Delay_ms(100);
                  RD6_bit=0;
                  RD7_bit=1;
                  GetDelay();
                  RD6_bit=0;
                  RD7_bit=0;

                  }
               //ARPAGH CLOSE
               while (RD3_Bit==0){
                  RC4_bit=1;
                  RC5_bit=0;
                  GetDelay();
                  RC4_bit=0;
                  RC5_bit=0;
                }
               //PERISTROFH LEFT
               while (y3<120){
                  y3 = ADC_Get_Sample(3);
                  Delay_ms(100);
                  RC2_bit=0;
                  RC3_bit=1;
                  GetDelay();
                  RC2_bit=0;
                  RC3_bit=0;
               }
               //PERISTROFH RIGHT
               while (y3>120){
                  y3 = ADC_Get_Sample(3);
                  Delay_ms(100);
                  RC2_bit=1;
                  RC3_bit=0;
                  GetDelay();
                  RC2_bit=0;
                  RC3_bit=0;
               }

               }

               x = '\0';
   }

  }
}
0

There are 0 best solutions below