Advertisement
Erfinator

How does this code work? It works, just not sure how it does

Dec 23rd, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 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.  
  10. // --------------------------------------------------------------------------- Setup
  11. void setup() {
  12. Serial.begin(9600);
  13.  
  14. // Setup motors
  15. int i;
  16. for(i = 0; i < 2; i++){
  17. pinMode(motor_left[i], OUTPUT);
  18. pinMode(motor_right[i], OUTPUT);
  19. }
  20.  
  21. }
  22.  
  23. // --------------------------------------------------------------------------- Loop
  24. void loop() {
  25.  
  26. drive_forward();
  27. delay(1000);
  28. motor_stop();
  29. Serial.println("1");
  30.  
  31. drive_backward();
  32. delay(1000);
  33. motor_stop();
  34. Serial.println("2");
  35.  
  36. turn_left();
  37. delay(1000);
  38. motor_stop();
  39. Serial.println("3");
  40.  
  41. turn_right();
  42. delay(1000);
  43. motor_stop();
  44. Serial.println("4");
  45.  
  46. motor_stop();
  47. delay(1000);
  48. motor_stop();
  49. Serial.println("5");
  50. }
  51.  
  52. // --------------------------------------------------------------------------- Drive
  53.  
  54. void motor_stop(){
  55. digitalWrite(motor_left[0], LOW);
  56. digitalWrite(motor_left[1], LOW);
  57.  
  58. digitalWrite(motor_right[0], LOW);
  59. digitalWrite(motor_right[1], LOW);
  60. delay(25);
  61. }
  62.  
  63. void drive_forward(){
  64. digitalWrite(motor_left[0], HIGH);
  65. digitalWrite(motor_left[1], LOW);
  66.  
  67. digitalWrite(motor_right[0], HIGH);
  68. digitalWrite(motor_right[1], LOW);
  69. }
  70.  
  71. void drive_backward(){
  72. digitalWrite(motor_left[0], LOW);
  73. digitalWrite(motor_left[1], HIGH);
  74.  
  75. digitalWrite(motor_right[0], LOW);
  76. digitalWrite(motor_right[1], HIGH);
  77. }
  78.  
  79. void turn_left(){
  80. digitalWrite(motor_left[0], LOW);
  81. digitalWrite(motor_left[1], HIGH);
  82.  
  83. digitalWrite(motor_right[0], HIGH);
  84. digitalWrite(motor_right[1], LOW);
  85. }
  86.  
  87. void turn_right(){
  88. digitalWrite(motor_left[0], HIGH);
  89. digitalWrite(motor_left[1], LOW);
  90.  
  91. digitalWrite(motor_right[0], LOW);
  92. digitalWrite(motor_right[1], HIGH);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement