Advertisement
Guest User

Untitled

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