Want more features on Pastebin? Sign Up, it's FREE!
Guest

Arduino mega motor control

By: a guest on Dec 18th, 2013  |  syntax: C  |  size: 2.80 KB  |  views: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. void motor_control(){
  2.  
  3.   float x_value,y_value;
  4.   float x1,x2,y1,y2,motor_left,motor_right;
  5.   float left_pwm,right_pwm;
  6.   int dont_move = 0;
  7.  
  8.   //References from transmitter
  9.   /*XbeeTxBuffer[9] = (map(analogRead(joy_2_x),0,1023,100,0))/4;  //Byte 2
  10.   XbeeTxBuffer[10] = (map(analogRead(joy_2_y),0,1023,100,0))/4; //Byte 3
  11.   XbeeTxBuffer[11] = (map(analogRead(joy_1_x),0,1023,100,0))/4; //Byte 4
  12.   XbeeTxBuffer[12] = (map(analogRead(joy_1_y),0,1023,100,0))/4; //Byte 5*/
  13.  
  14.   if(good_comm == 1){
  15.    
  16.     x_value = RxBuffer[9];
  17.     y_value = RxBuffer[10];
  18.     //left_pwm = ((y_value - 128) * 2) + ((x_value - 128) * 2);
  19.     //right_pwm = ((y_value - 128) * 2) + ((128 - x_value) * 2);
  20.    
  21.     if(x_value >= 124){
  22.       if(x_value <= 132){
  23.         if(y_value >= 124){
  24.           if(y_value <= 132){
  25.              digitalWrite(pwm1, LOW);
  26.              digitalWrite(pwm2, LOW);
  27.              digitalWrite(pwm3, LOW);
  28.              digitalWrite(pwm4, LOW);
  29.              digitalWrite(dir1, LOW);
  30.              digitalWrite(dir2, LOW);
  31.              digitalWrite(dir3, LOW);
  32.              digitalWrite(dir4, LOW);
  33.              dont_move = 1;
  34.           }
  35.         }
  36.       }
  37.     }
  38.  
  39.     if(dont_move == 0){
  40.       left_pwm = ((y_value - 128) * 2) + ((128 - x_value) * 2);
  41.       right_pwm = ((y_value - 128) * 2) + ((x_value - 128) * 2);
  42.      
  43.      
  44.       //FRONT
  45.       if(left_pwm >= 0){
  46.         if(left_pwm >= 255){
  47.           left_pwm = 255;
  48.         }
  49.         digitalWrite(dir1, LOW);
  50.         digitalWrite(dir3, LOW);
  51.         analogWrite(pwm1, left_pwm);
  52.         analogWrite(pwm3, left_pwm);
  53.       }
  54.      
  55.       //FRONT
  56.       if(right_pwm >= 0){
  57.         if(right_pwm >= 255){
  58.           right_pwm = 255;
  59.         }
  60.         digitalWrite(dir2, LOW);
  61.         digitalWrite(dir4, LOW);
  62.         analogWrite(pwm2, right_pwm);
  63.         analogWrite(pwm4, right_pwm);
  64.       }
  65.      
  66.       //REVERSE
  67.       if(left_pwm < 0){
  68.         if(left_pwm <= -255){
  69.           left_pwm = -255;
  70.         }
  71.         left_pwm = -left_pwm;
  72.         digitalWrite(pwm1, LOW);
  73.         digitalWrite(pwm3, LOW);
  74.         analogWrite(dir1, left_pwm);
  75.         analogWrite(dir3, left_pwm);
  76.       }
  77.      
  78.       //REVERSE
  79.       if(right_pwm < 0){
  80.         if(right_pwm <= -255){
  81.           right_pwm = -255;
  82.         }
  83.         right_pwm = -right_pwm;
  84.         digitalWrite(pwm2, LOW);
  85.         digitalWrite(pwm4, LOW);
  86.         analogWrite(dir2, right_pwm);
  87.         analogWrite(dir4, right_pwm);
  88.       }
  89.    
  90.      
  91.     }
  92.   }else{
  93.      digitalWrite(pwm1, LOW);
  94.      digitalWrite(pwm2, LOW);
  95.      digitalWrite(pwm3, LOW);
  96.      digitalWrite(pwm4, LOW);
  97.      digitalWrite(dir1, LOW);
  98.      digitalWrite(dir2, LOW);
  99.      digitalWrite(dir3, LOW);
  100.      digitalWrite(dir4, LOW);
  101.   }//END ELSE
  102.  
  103.   dont_move = 0;
  104. }//END FUNCTION
clone this paste RAW Paste Data