Advertisement
Guest User

Untitled

a guest
Jul 13th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #include <SoftwareSerial.h> // Allows communication with Bluetooth
  2. #include <HCSR04.h>
  3. #include <L298N.h>
  4. #include <Servo.h>
  5.  
  6. SoftwareSerial mySerial(0, 1); // RX, TX
  7. int data;
  8.  
  9. const unsigned int IN1 = 7;
  10. const unsigned int IN2 = 8;
  11. const unsigned int EN = 6;
  12. const unsigned int IN3 = 5;
  13. const unsigned int IN4 = 4;
  14. const unsigned int ENB = 3;
  15. const unsigned int trigPin = 13;
  16. const unsigned int echoPin = 12;
  17.  
  18. int mode = 0; //mode 0 is bluetooth mode, mode 1 is obstacle-avoiding mode
  19.  
  20. UltraSonicDistanceSensor distanceSensor(13, 12); // Initialize sensor that uses digital pins 13 and 12.
  21.  
  22. L298N motor(EN, IN1, IN2);
  23. L298N motor2(ENB, IN3, IN4);
  24.  
  25. Servo servo1;
  26.  
  27. void setup() {
  28. // Open serial communications and wait for port to open:
  29. mySerial.begin(9600);
  30. Serial.begin(9600);
  31. while (!Serial) {
  32. ; // wait for serial port to connect. Needed for native USB port only
  33. }
  34. Serial.println("Initiated");
  35. motor.setSpeed(225); //set the speed of the motors, between 0-255
  36. motor2.setSpeed (225);
  37. pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  38. pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
  39. servo1.attach(11);
  40. servo1.write(115);
  41. }
  42. void loop() {
  43.  
  44. while (mode == 0) //changed if to while loop (RA)
  45. {
  46. if (mySerial.available()) {
  47. data = mySerial.read(); // define data as the num recieved from BT
  48. Serial.println(data);
  49.  
  50. }
  51. if (data == 1) {
  52. forward_car();
  53. }
  54. else if (data == 2) {
  55. back_car();
  56. }
  57. else if (data == 3) {
  58. left_car();
  59. }
  60. else if (data == 4) {
  61. right_car();
  62. }
  63. else if (data == 5) {
  64. stop_car();
  65. }
  66.  
  67. else if (data == 6) {
  68. mode = 1;
  69. break;
  70. }
  71. }
  72.  
  73. while (mode == 1) {
  74. // Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
  75. Serial.println(distanceSensor.measureDistanceCm());
  76. delay(500);
  77. long duration, distance; // start the scan
  78. digitalWrite(trigPin, LOW);
  79. delayMicroseconds(2); // delays are required for a succesful sensor operation.
  80. digitalWrite(trigPin, HIGH);
  81.  
  82. delayMicroseconds(10); //this delay is required as well!
  83. digitalWrite(trigPin, LOW);
  84. duration = pulseIn(echoPin, HIGH);
  85.  
  86. if (distanceSensor.measureDistanceCm() < 30) {
  87. digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
  88. motor.stop();
  89. motor2.stop();
  90. delay(500);
  91. motor.backward();
  92. motor2.backward(); //actually backward
  93. delay(1000);
  94. motor.stop();
  95. motor2.stop();
  96. delay(500);
  97.  
  98.  
  99. if (lookLeft() > lookRight()) {
  100. motor.forward();
  101. motor2.backward();
  102. delay(90);
  103. }
  104. else {
  105. motor.backward(); //this means turn
  106. motor2.forward();
  107. delay(90);
  108. }
  109.  
  110. }
  111. else
  112. { digitalWrite(2, LOW);
  113. Serial.println ("No obstacle detected. going forward");
  114. motor.forward(); //if there's no obstacle ahead, Go Forward!
  115. motor2.forward();
  116. delay (100);
  117. }
  118.  
  119.  
  120. if (mySerial.available()) {
  121. data = mySerial.read(); // define data as the num recieved from BT
  122. Serial.println(data);
  123.  
  124. }
  125.  
  126. if (data != 6) {
  127. mode = 0;
  128. break;
  129. }
  130.  
  131. }
  132. }
  133.  
  134.  
  135. void forward_car()
  136. { motor.forward();
  137. motor2.forward();
  138.  
  139. }
  140.  
  141. void back_car()
  142. { motor.backward();
  143. motor2.backward();
  144. }
  145.  
  146. void left_car()
  147. { motor.forward();
  148. motor2.backward();
  149. }
  150.  
  151. void right_car()
  152. { motor.backward();
  153. motor2.forward();
  154. }
  155.  
  156. void stop_car()
  157. { motor.stop();
  158. motor2.stop();
  159.  
  160. }
  161.  
  162. int lookRight()
  163. {
  164. servo1.write(50);
  165. delay(500);
  166. int distance = distanceSensor.measureDistanceCm();
  167. delay(100);
  168. servo1.write(115);
  169. return distance;
  170. }
  171.  
  172. int lookLeft()
  173. {
  174. servo1.write(170);
  175. delay(500);
  176. int distance = distanceSensor.measureDistanceCm();
  177. delay(100);
  178. servo1.write(115);
  179. return distance;
  180. delay(100);
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement