Advertisement
Erfinator

pwm test 2

Dec 24th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. // Use this code to test your motor with the Arduino board:
  2.  
  3. // if you need PWM, just use the PWM outputs on the Arduino
  4. // and instead of digitalWrite, you should use the analogWrite command
  5.  
  6. // --------------------------------------------------------------------------- Motors
  7. int motor_left[] = {2, 3};
  8. int motor_right[] = {7, 8};
  9. int enable_pin_left = 3;
  10. int enable_pin_right = 5;
  11.  
  12. // --------------------------------------------------------------------------- Setup
  13. void setup() {
  14. Serial.begin(9600);
  15.  
  16. // Setup motors
  17. int i;
  18. for(i = 0; i < 2; i++){
  19. pinMode(motor_left[i], OUTPUT);
  20. pinMode(motor_right[i], OUTPUT);
  21. pinMode(enable_pin_left, OUTPUT);
  22. pinMode(enable_pin_right, OUTPUT);
  23. }
  24.  
  25. }
  26.  
  27. // --------------------------------------------------------------------------- Loop
  28. void loop() {
  29.  
  30. analogWrite(enable_pin_left, 80);
  31. analogWrite(enable_pin_right, 80);
  32. drive_forward();
  33. delay(5000);
  34. drive_backward();
  35. delay(5000);
  36.  
  37. }
  38.  
  39. // --------------------------------------------------------------------------- Drive
  40.  
  41. void motor_stop(){
  42. digitalWrite(motor_left[0], LOW);
  43. digitalWrite(motor_left[1], LOW);
  44.  
  45. digitalWrite(motor_right[0], LOW);
  46. digitalWrite(motor_right[1], LOW);
  47. delay(25);
  48. }
  49.  
  50. void drive_forward(){
  51. digitalWrite(motor_left[0], HIGH);
  52. digitalWrite(motor_left[1], LOW);
  53.  
  54. digitalWrite(motor_right[0], HIGH);
  55. digitalWrite(motor_right[1], LOW);
  56. }
  57.  
  58. void drive_backward(){
  59. digitalWrite(motor_left[0], LOW);
  60. digitalWrite(motor_left[1], HIGH);
  61.  
  62. digitalWrite(motor_right[0], LOW);
  63. digitalWrite(motor_right[1], HIGH);
  64. }
  65.  
  66. void turn_left(){
  67. digitalWrite(motor_left[0], LOW);
  68. digitalWrite(motor_left[1], HIGH);
  69.  
  70. digitalWrite(motor_right[0], HIGH);
  71. digitalWrite(motor_right[1], LOW);
  72. }
  73.  
  74. void turn_right(){
  75. digitalWrite(motor_left[0], HIGH);
  76. digitalWrite(motor_left[1], LOW);
  77.  
  78. digitalWrite(motor_right[0], LOW);
  79. digitalWrite(motor_right[1], HIGH);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement