Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. // Pins used
  2. const int Ain1Pin = 7;
  3. const int Ain2Pin = 8;
  4. const int PwmAPin = 9;
  5. const int Bin1Pin = 4;
  6. const int Bin2Pin = 5;
  7. const int PwmBPin = 6;
  8.  
  9.  
  10. void setup(){
  11.  
  12. // Configure the pins
  13. // For the motor to move forward: pin 1 = HIGH and pin 2 = LOW
  14. // For the motor to move backward: pin 1 = LOW and pin 2 = HIGH
  15. // For the motor to stop or break: pin 1 = LOW and pin 2 = LOW
  16. // The PWM pin determines the speed of the motor
  17. pinMode(Ain1Pin, OUTPUT);
  18. pinMode(Ain2Pin, OUTPUT);
  19. pinMode(PwmAPin, OUTPUT);
  20. pinMode(Bin1Pin, OUTPUT);
  21. pinMode(Bin2Pin, OUTPUT);
  22. pinMode(PwmBPin, OUTPUT);
  23. }
  24.  
  25.  
  26.  
  27. void loop() {
  28.  
  29. delay(2000);
  30.  
  31. // Move motor A forward at full speed
  32. digitalWrite(Ain1Pin, LOW);
  33. digitalWrite(Ain2Pin, HIGH);
  34. analogWrite(PwmAPin, 255);
  35.  
  36. // Move motor B forward at full speed
  37. digitalWrite(Bin1Pin, LOW);
  38. digitalWrite(Bin2Pin, HIGH);
  39. analogWrite(PwmBPin, 255);
  40.  
  41. delay(2000);
  42.  
  43. // Move motor A forward at half speed
  44. digitalWrite(Ain1Pin, LOW);
  45. digitalWrite(Ain2Pin, HIGH);
  46. analogWrite(PwmAPin, 123);
  47.  
  48. // Move motor B backward at full speed
  49. digitalWrite(Bin1Pin, HIGH);
  50. digitalWrite(Bin2Pin, LOW);
  51. analogWrite(PwmBPin, 255);
  52.  
  53. delay(2000);
  54.  
  55. // Move motor A backward at half speed
  56. digitalWrite(Ain1Pin, HIGH);
  57. digitalWrite(Ain2Pin, LOW);
  58. analogWrite(PwmAPin, 123);
  59.  
  60. // Stop motor B
  61. digitalWrite(Bin1Pin, LOW);
  62. digitalWrite(Bin2Pin, LOW);
  63. analogWrite(PwmBPin, 0);
  64.  
  65. delay(2000);
  66.  
  67. // Stop motor A
  68. digitalWrite(Ain1Pin, LOW);
  69. digitalWrite(Ain2Pin, LOW);
  70. analogWrite(PwmAPin, 0);
  71.  
  72. // Move motor B forward at half speed
  73. digitalWrite(Bin1Pin, LOW);
  74. digitalWrite(Bin2Pin, HIGH);
  75. analogWrite(PwmBPin, 123);
  76.  
  77. }
  78.  
  79. void brake () {
  80. // Stop motor A
  81. digitalWrite(Ain1Pin, LOW);
  82. digitalWrite(Ain2Pin, LOW);
  83. analogWrite(PwmAPin, 0);
  84.  
  85. // Stop motor B
  86. digitalWrite(Ain1Pin, LOW);
  87. digitalWrite(Ain2Pin, LOW);
  88. analogWrite(PwmBPin, 0);
  89.  
  90. }
  91.  
  92. void forward () {
  93. // Go motor A
  94. digitalWrite(Ain1Pin, LOW);
  95. digitalWrite(Ain2Pin, HIGH);
  96. analogWrite(PwmAPin, 255);
  97.  
  98. // Go motor B
  99. digitalWrite(Ain1Pin, LOW);
  100. digitalWrite(Ain2Pin, HIGH);
  101. analogWrite(PwmBPin, 255);
  102. }
  103.  
  104. void back () {
  105. // back motor A
  106. digitalWrite(Ain1Pin, HIGH);
  107. digitalWrite(Ain2Pin, LOW);
  108. analogWrite(PwmAPin, 255);
  109.  
  110. // back motor B
  111. digitalWrite(Ain1Pin, HIGH);
  112. digitalWrite(Ain2Pin, LOW);
  113. analogWrite(PwmBPin, 255);
  114.  
  115. char Incoming_value = 0;
  116.  
  117. Incoming_value = Serial.read(); //Read the incoming data and store it into variable Incoming_value
  118. Serial.print(Incoming_value); //Print Value of Incoming_value in Serial monitor
  119. Serial.print("\n"); //New line
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement