Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. const int AIN1 = 13;
  2. const int AIN2 = 12;
  3. const int PWMA = 11;
  4.  
  5.  
  6. const int PWMB = 8;
  7. const int BIN2 = 9;
  8. const int BIN1 = 10;
  9.  
  10. const int trigPinLeft=4;
  11. const int echoPinLeft=3;
  12. const int trigPinRight=2;
  13. const int echoPinRight=1;
  14. const int trigPin = 6;
  15. const int echoPin = 5;
  16. int switchPin = 7;
  17.  
  18. float distance = 0;
  19. float distanceRight=0;
  20. float distanceLeft=0;
  21.  
  22. int backupTime = 300;
  23. int turnTime = 400;
  24.  
  25. /********************************************************************************/
  26. void setup()
  27. {
  28. pinMode(trigPin, OUTPUT);
  29. pinMode(echoPin, INPUT);
  30.  
  31. pinMode(AIN1, OUTPUT);
  32. pinMode(AIN2, OUTPUT);
  33. pinMode(PWMA, OUTPUT);
  34.  
  35. pinMode(BIN1, OUTPUT);
  36. pinMode(BIN2, OUTPUT);
  37. pinMode(PWMB, OUTPUT);
  38.  
  39. Serial.begin(9600);
  40. }
  41.  
  42. /********************************************************************************/
  43. void loop()
  44. {
  45. distance = getDistance();
  46. if(distance < 8){
  47. stop();
  48. delay(25);
  49. goLeft();
  50. delay(turnTime);
  51. distanceLeft=getDistanceLeft();
  52. distanceRight=getDistanceRight();
  53. while(distanceLeft<1||distanceRight<1){
  54. distanceLeft=getDistanceLeft();
  55. distanceRight=getDistanceRight();
  56. if(distanceRight<1&&distanceLeft<1){
  57. goForwards(255);
  58. break;
  59. }
  60. else if(distanceLeft<1){
  61. goRight();
  62. }
  63. else if(distanceRight<1){
  64. goLeft();
  65. }
  66. delay(50);
  67. }
  68. }
  69. else{
  70. goForwards(255);
  71. }
  72. delay(50);
  73. }
  74.  
  75. /********************************************************************************/
  76. void rightMotor(int motorSpeed)
  77. {
  78. if (motorSpeed > 0)
  79. {
  80. digitalWrite(AIN1, HIGH);
  81. digitalWrite(AIN2, LOW);
  82. }
  83. else if (motorSpeed < 0)
  84. {
  85. digitalWrite(AIN1, LOW);
  86. digitalWrite(AIN2, HIGH);
  87. }
  88. else
  89. {
  90. digitalWrite(AIN1, LOW);
  91. digitalWrite(AIN2, LOW);
  92. }
  93. analogWrite(PWMA, abs(motorSpeed));
  94. }
  95.  
  96. /********************************************************************************/
  97. void leftMotor(int motorSpeed)
  98. {
  99. if (motorSpeed > 0)
  100. {
  101. digitalWrite(BIN1, HIGH);
  102. digitalWrite(BIN2, LOW);
  103. }
  104. else if (motorSpeed < 0)
  105. {
  106. digitalWrite(BIN1, LOW);
  107. digitalWrite(BIN2, HIGH);
  108. }
  109. else
  110. {
  111. digitalWrite(BIN1, LOW);
  112. digitalWrite(BIN2, LOW);
  113. }
  114. analogWrite(PWMB, abs(motorSpeed));
  115. }
  116. /********************************************************************************/
  117. void goForwards(int speed){
  118. rightMotor(speed);
  119. leftMotor(speed);
  120. }
  121. /********************************************************************************/
  122. void goBackwards(int speed){
  123. rightMotor(speed*-1);
  124. leftMotor(speed*-1);
  125. }
  126. /********************************************************************************/
  127. void goRight(){
  128. rightMotor(-255);
  129. leftMotor(255);
  130. }
  131. /********************************************************************************/
  132. void goLeft(){
  133. rightMotor(255);
  134. leftMotor(-255);
  135. }
  136. /********************************************************************************/
  137. void stop(){
  138. rightMotor(0);
  139. leftMotor(0);
  140. }
  141. /********************************************************************************/
  142. float getDistance()
  143. {
  144. float echoTime;
  145. float calculatedDistance;
  146. digitalWrite(trigPin, HIGH);
  147. delayMicroseconds(10);
  148. digitalWrite(trigPin, LOW);
  149.  
  150. echoTime = pulseIn(echoPin, HIGH);
  151. calculatedDistance = echoTime / 148.0;
  152. return calculatedDistance;
  153. }
  154. /********************************************************************************/
  155. float getDistanceRight()
  156. {
  157. float echoTimeRight;
  158. float calculatedDistanceRight;
  159.  
  160. digitalWrite(trigPinRight, HIGH);
  161. delayMicroseconds(10);
  162. digitalWrite(trigPinRight, LOW);
  163.  
  164. echoTimeRight = pulseIn(echoPinRight, HIGH);
  165. calculatedDistanceRight = echoTimeRight / 148.0;
  166.  
  167. return calculatedDistanceRight;
  168. }
  169. /********************************************************************************/
  170. float getDistanceLeft()
  171. {
  172. float echoTimeLeft;
  173. float calculatedDistanceLeft;
  174.  
  175. digitalWrite(trigPinLeft, HIGH);
  176. delayMicroseconds(10);
  177. digitalWrite(trigPinLeft, LOW);
  178.  
  179. echoTimeLeft = pulseIn(echoPinLeft, HIGH);
  180. calculatedDistanceLeft = echoTimeLeft/ 148.0;
  181.  
  182. return calculatedDistanceLeft;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement