Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. /*
  2.  
  3. */
  4.  
  5. #include <Arduino.h>
  6.  
  7. //the right motor will be controlled by the motor A pins on the motor driver
  8. const int AIN1 = 13; //control pin 1 on the motor driver for the right motor
  9. const int AIN2 = 12; //control pin 2 on the motor driver for the right motor
  10. const int PWMA = 11; //speed control pin on the motor driver for the right motor
  11.  
  12. //the left motor will be controlled by the motor B pins on the motor driver
  13. const int PWMB = 8; //speed control pin on the motor driver for the left motor
  14. const int BIN2 = 9; //control pin 2 on the motor driver for the left motor
  15. const int BIN1 = 10; //control pin 1 on the motor driver for the left motor
  16.  
  17. int buttonPin = 7; //Emergency stop button to turn the robot off suddenly
  18.  
  19. const int driveTime = 20; //this is the number of milliseconds that it takes the robot to drive 1 inch
  20. //it is set so that if you tell the robot to drive forward 25 units, the robot drives about 25 inches
  21.  
  22. const int turnTime = 15; //this is the number of milliseconds that it takes to turn the robot 90 degree
  23. //it is set so that if you tell the robot to turn right 90 units, the robot turns about 90 degrees
  24.  
  25. //Note: these numbers will vary a little bit based on how you mount your motors, the friction of the
  26. //surface that your driving on, and fluctuations in the power to the motors.
  27. //You can change the driveTime and turnTime to make them more accurate
  28.  
  29. String botDirection; //the direction that the robot will drive in (this change which direction the two motors spin in)
  30. String distance="40"; //the distance to travel in each direction
  31.  
  32. /********************************************************************************/
  33. void setup(){
  34. pinMode(buttonPin, INPUT_PULLUP); //set this as a input to sense whether the button is pushed
  35.  
  36. //set the motor control pins as outputs
  37. pinMode(AIN1, OUTPUT);
  38. pinMode(AIN2, OUTPUT);
  39. pinMode(PWMA, OUTPUT);
  40.  
  41. pinMode(BIN1, OUTPUT);
  42. pinMode(BIN2, OUTPUT);
  43. pinMode(PWMB, OUTPUT);
  44.  
  45. Serial.begin(9600); //begin serial communication with the computer
  46.  
  47. }
  48.  
  49. /********************************************************************************/
  50. void loop(){
  51.  
  52. if (digitalRead(buttonPin)==HIGH){
  53.  
  54. } else {
  55. goForwards();
  56. delay(1000);
  57. turnLeft();
  58. delay(1000);
  59. goBackwards();
  60. delay(1000);
  61. turnRight();
  62. delay(1000);}
  63. else{
  64. while(1){
  65.  
  66. }
  67. }
  68.  
  69.  
  70.  
  71. }
  72.  
  73. /********************************************************************************/
  74. void rightMotor(int motorSpeed) //function for driving the right motor
  75. {
  76. if (motorSpeed > 0) //if the motor should drive forward (positive speed)
  77. {
  78. digitalWrite(AIN1, HIGH); //set pin 1 to high
  79. digitalWrite(AIN2, LOW); //set pin 2 to low
  80. }
  81. else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
  82. {
  83. digitalWrite(AIN1, LOW); //set pin 1 to low
  84. digitalWrite(AIN2, HIGH); //set pin 2 to high
  85. }
  86. else //if the motor should stop
  87. {
  88. digitalWrite(AIN1, LOW); //set pin 1 to low
  89. digitalWrite(AIN2, LOW); //set pin 2 to low
  90. }
  91. analogWrite(PWMA, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
  92. }
  93. /********************************************************************************/
  94. void leftMotor(int motorSpeed) //function for driving the left motor
  95. {
  96. if (motorSpeed > 0) //if the motor should drive forward (positive speed)
  97. {
  98. digitalWrite(BIN1, HIGH); //set pin 1 to high
  99. digitalWrite(BIN2, LOW); //set pin 2 to low
  100. }
  101. else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
  102. {
  103. digitalWrite(BIN1, LOW); //set pin 1 to low
  104. digitalWrite(BIN2, HIGH); //set pin 2 to high
  105. }
  106. else //if the motor should stop
  107. {
  108. digitalWrite(BIN1, LOW); //set pin 1 to low
  109. digitalWrite(BIN2, LOW); //set pin 2 to low
  110. }
  111. analogWrite(PWMB, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
  112. }
  113. /********************************************************************************/
  114. void turnLeft(){
  115. rightMotor(255); //drive the right wheel forward
  116. leftMotor(-200); //drive the left wheel forward
  117. delay(turnTime * distance.toInt()); //drive the motors long enough turn the entered distance
  118. rightMotor(0); //turn the right motor off
  119. leftMotor(0); //turn the left motor off
  120. }
  121. /********************************************************************************/
  122. void turnRight(){
  123. rightMotor(-200); //drive the right wheel forward
  124. leftMotor(255); //drive the left wheel forward
  125. delay(turnTime * distance.toInt()); //drive the motors long enough turn the entered distance
  126. rightMotor(0); //turn the right motor off
  127. leftMotor(0); //turn the left motor off
  128. }
  129. /********************************************************************************/
  130. void goForwards(){
  131. rightMotor(200); //drive the right wheel forward
  132. leftMotor(200); //drive the left wheel forward
  133. delay(driveTime * distance.toInt()); //drive the motors long enough travel the entered distance
  134. rightMotor(0); //turn the right motor off
  135. leftMotor(0); //turn the left motor off
  136. }
  137. /********************************************************************************/
  138. void goBackwards(){
  139. rightMotor(-200); //drive the right wheel forward
  140. leftMotor(-200); //drive the left wheel forward
  141. delay(driveTime * distance.toInt()); //drive the motors long enough travel the entered distance
  142. rightMotor(0); //turn the right motor off
  143. leftMotor(0); //turn the left motor off
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement