四博智联产品售后

标题: 2路电机控制Arduino代码示例 [打印本页]

作者: ycf    时间: 2015-12-10 10:56
标题: 2路电机控制Arduino代码示例
  1. // By DOIT,http://www.doit.am
  2. /*
  3. For Channel A and Chanel B,
  4. Motor rotates forward, then backward,.....
  5. Pins:

  6. PWMB --- 12
  7. DIRB --- 13
  8. PWMA --- 3
  9. DIRA --- 1

  10. */

  11. int pwm1 = 3;
  12. int dir1 = 1;
  13. int pwm2 = 12;                        
  14. int dir2 = 13;                           

  15. void setup()
  16. {
  17.     pinMode(dir1, OUTPUT);   
  18.     pinMode(dir2, OUTPUT);
  19. }

  20. void loop()
  21. {
  22.   int value;
  23.     for(value = 0 ; value<=255; value+=5) // forward,speed up
  24.   {
  25.     digitalWrite(dir1,HIGH);   
  26.     digitalWrite(dir2,HIGH);      
  27.     analogWrite(pwm1, value);   //PWM Speed Control
  28.     analogWrite(pwm2 , value);   //PWM Speed Control
  29.     delay(30);
  30.   }  
  31.   for(value = 255 ; value >=0; value-=5) // forward,speed down
  32.   {
  33.     digitalWrite(dir1,HIGH);   
  34.     digitalWrite(dir2,HIGH);      
  35.     analogWrite(pwm1, value);   //PWM Speed Control
  36.     analogWrite(pwm2 , value);   //PWM Speed Control
  37.     delay(30);
  38.   }  
  39.   delay(2000);
  40.     for(value = 0 ; value<=255; value+=5) // backward,speed up
  41.   {
  42.     digitalWrite(dir1,LOW);   
  43.     digitalWrite(dir2,LOW);   
  44.     analogWrite(pwm1, value);   //PWM Speed Control
  45.     analogWrite(pwm2 , value);   //PWM Speed Control
  46.     delay(30);
  47.   }   
  48. for(value = 255 ; value >=0; value-=5) // backward,speed down
  49.   {
  50.     digitalWrite(dir1,LOW);   
  51.     digitalWrite(dir2,LOW);      
  52.     analogWrite(pwm1, value);   //PWM Speed Control
  53.     analogWrite(pwm2 , value);   //PWM Speed Control
  54.     delay(30);
  55.   }  
  56.    delay(3000);
  57.   
  58. }

  59. MotorControlSample.zip (773 Bytes, 下载次数: 8382)
复制代码







欢迎光临 四博智联产品售后 (http://bbs.doit.am/) Powered by Discuz! X3.2