Advertisement
Erfinator

pwm?

Dec 24th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 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, 50);
  31. analogWrite(enable_pin_right, 50);
  32.  
  33. }
  34.  
  35. // --------------------------------------------------------------------------- Drive
  36.  
  37. void motor_stop(){
  38. digitalWrite(motor_left[0], LOW);
  39. digitalWrite(motor_left[1], LOW);
  40.  
  41. digitalWrite(motor_right[0], LOW);
  42. digitalWrite(motor_right[1], LOW);
  43. delay(25);
  44. }
  45.  
  46. void drive_forward(){
  47. digitalWrite(motor_left[0], HIGH);
  48. digitalWrite(motor_left[1], LOW);
  49.  
  50. digitalWrite(motor_right[0], HIGH);
  51. digitalWrite(motor_right[1], LOW);
  52. }
  53.  
  54. void drive_backward(){
  55. digitalWrite(motor_left[0], LOW);
  56. digitalWrite(motor_left[1], HIGH);
  57.  
  58. digitalWrite(motor_right[0], LOW);
  59. digitalWrite(motor_right[1], HIGH);
  60. }
  61.  
  62. void turn_left(){
  63. digitalWrite(motor_left[0], LOW);
  64. digitalWrite(motor_left[1], HIGH);
  65.  
  66. digitalWrite(motor_right[0], HIGH);
  67. digitalWrite(motor_right[1], LOW);
  68. }
  69.  
  70. void turn_right(){
  71. digitalWrite(motor_left[0], HIGH);
  72. digitalWrite(motor_left[1], LOW);
  73.  
  74. digitalWrite(motor_right[0], LOW);
  75. digitalWrite(motor_right[1], HIGH);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement