Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1.  
  2.  
  3.  
  4. //the right motor will be controlled by the motor A pins on the motor driver
  5. const int AIN1 = 13; //control pin 1 on the motor driver for the right motor
  6. const int AIN2 = 12; //control pin 2 on the motor driver for the right motor
  7. const int PWMA = 11; //speed control pin on the motor driver for the right motor
  8.  
  9. //the left motor will be controlled by the motor B pins on the motor driver
  10. const int PWMB = 8; //speed control pin on the motor driver for the left motor
  11. const int BIN2 = 9; //control pin 2 on the motor driver for the left motor
  12. const int BIN1 = 10; //control pin 1 on the motor driver for the left motor
  13.  
  14.  
  15. //distance variables
  16. const int trigPin = 6;
  17. const int echoPin = 5;
  18.  
  19. //int switchPin = 7; //switch to turn the robot on and off
  20.  
  21. float distance = 0; //variable to store the distance measured by the distance sensor
  22.  
  23. //robot behaviour variables
  24. int backupTime = 300; //amount of time that the robot will back up when it senses an object
  25. int turnTime = 400; //amount that the robot will turn once it has backed up
  26.  
  27. /********************************************************************************/
  28. void setup()
  29. {
  30. pinMode(trigPin, OUTPUT); //this pin will send ultrasonic pulses out from the distance sensor
  31. pinMode(echoPin, INPUT); //this pin will sense when the pulses reflect back to the distance sensor
  32.  
  33. //pinMode(switchPin, INPUT_PULLUP); //set this as a pullup to sense whether the switch is flipped
  34.  
  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. Serial.print("To infinity and beyond!"); //test the serial connection
  47. }
  48.  
  49. /********************************************************************************/
  50. void loop()
  51. {
  52. //DETECT THE DISTANCE READ BY THE DISTANCE SENSOR
  53. distance = getDistance();
  54.  
  55. Serial.print("Distance: ");
  56. Serial.print(distance);
  57. Serial.println(" in"); // print the units
  58.  
  59. // if(digitalRead(switchPin) == LOW){ //if the on switch is flipped
  60.  
  61. if(distance < 15){ //if an object is detected
  62. //back up and turn
  63. Serial.print(" ");
  64. Serial.print("BACK!");
  65.  
  66. //stop for a moment
  67. rightMotor(0);
  68. leftMotor(0);
  69. // delay(200);
  70.  
  71. //back up
  72. rightMotor(-255);
  73. leftMotor(-255);
  74. delay(backupTime);
  75.  
  76. //turn away from obstacle
  77. rightMotor(255);
  78. leftMotor(-255);
  79. delay(turnTime);
  80.  
  81. }else{ //if no obstacle is detected drive forward
  82. Serial.print(" ");
  83. Serial.print("Moving...");
  84.  
  85.  
  86. rightMotor(255);
  87. leftMotor(255);
  88. }
  89. /*} else{ //if the switch is off then stop
  90.  
  91. //stop the motors
  92. rightMotor(0);
  93. leftMotor(0);
  94. }*/
  95.  
  96. delay(50); //wait 50 milliseconds between readings
  97. }
  98.  
  99. /********************************************************************************/
  100. void rightMotor(int motorSpeed) //function for driving the right motor
  101. {
  102. if (motorSpeed > 0) //if the motor should drive forward (positive speed)
  103. {
  104. digitalWrite(AIN1, HIGH); //set pin 1 to high
  105. digitalWrite(AIN2, LOW); //set pin 2 to low
  106. }
  107. else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
  108. {
  109. digitalWrite(AIN1, LOW); //set pin 1 to low
  110. digitalWrite(AIN2, HIGH); //set pin 2 to high
  111. }
  112. else //if the motor should stop
  113. {
  114. digitalWrite(AIN1, LOW); //set pin 1 to low
  115. digitalWrite(AIN2, LOW); //set pin 2 to low
  116. }
  117. analogWrite(PWMA, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
  118. }
  119.  
  120. /********************************************************************************/
  121. void leftMotor(int motorSpeed) //function for driving the left motor
  122. {
  123. if (motorSpeed > 0) //if the motor should drive forward (positive speed)
  124. {
  125. digitalWrite(BIN1, HIGH); //set pin 1 to high
  126. digitalWrite(BIN2, LOW); //set pin 2 to low
  127. }
  128. else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
  129. {
  130. digitalWrite(BIN1, LOW); //set pin 1 to low
  131. digitalWrite(BIN2, HIGH); //set pin 2 to high
  132. }
  133. else //if the motor should stop
  134. {
  135. digitalWrite(BIN1, LOW); //set pin 1 to low
  136. digitalWrite(BIN2, LOW); //set pin 2 to low
  137. }
  138. analogWrite(PWMB, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
  139. }
  140.  
  141. /********************************************************************************/
  142. //RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
  143. float getDistance()
  144. {
  145. float echoTime; //variable to store the time it takes for a ping to bounce off an object
  146. float calculatedDistance; //variable to store the distance calculated from the echo time
  147.  
  148. //send out an ultrasonic pulse that's 10ms long
  149. digitalWrite(trigPin, HIGH);
  150. delayMicroseconds(10);
  151. digitalWrite(trigPin, LOW);
  152.  
  153. echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the
  154. //pulse to bounce back to the sensor
  155.  
  156. calculatedDistance = echoTime / 148.0; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)
  157.  
  158. return calculatedDistance; //send back the distance that was calculated
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement