Advertisement
Guest User

Untitled

a guest
Aug 16th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. // L298N - Version: Latest
  2. #include <L298N.h>
  3. const unsigned int IN1 = 22;
  4. const unsigned int IN2 = 23;
  5. const unsigned int ENA = 13;
  6.  
  7. const unsigned int IN3 = 24;
  8. const unsigned int IN4 = 25;
  9. const unsigned int ENB = 12;
  10.  
  11. // Create one motor instance
  12. L298N motor1(ENA, IN1, IN2);
  13. L298N motor2(ENB, IN3, IN4);
  14. #define trigPin 3
  15. #define echoPin 2
  16.  
  17.  
  18. void setup() {
  19. Serial.begin(9600); //Begin serial communication
  20. Serial.println("Motor test!");
  21. pinMode(trigPin, OUTPUT);
  22. pinMode(echoPin, INPUT);
  23. motor1.setSpeed(105);
  24. motor2.setSpeed(105);
  25.  
  26.  
  27. }
  28.  
  29. // left motor is in1 and in2
  30. // right motor is in3 and in4
  31.  
  32. void loop() {
  33.  
  34. digitalWrite (trigPin, HIGH);
  35. delay(50);
  36. digitalWrite (trigPin, LOW);
  37. int duration = pulseIn(echoPin, HIGH);
  38. int distance=(duration/2)/29.1;
  39. Serial.println(distance);
  40.  
  41. if (distance < 25) {
  42. Serial.println("Obstacle detected");
  43. Serial.println("Commencing manuever");
  44. motor1.stop();
  45. motor2.stop();
  46. delay(750);
  47.  
  48. motor1.backward();
  49. motor2.backward();
  50. delay(500);
  51.  
  52. motor1.setSpeed(140);
  53. motor2.setSpeed(140);
  54. motor1.forward();
  55. delay(450);
  56.  
  57. }
  58.  
  59. else {
  60. Serial.println("No obstacles ahead. Proceeding current route");
  61. delay(15);
  62. motor1.setSpeed(200);
  63. motor2.setSpeed(200);
  64. motor1.forward();
  65. motor2.forward();
  66.  
  67. }
  68.  
  69.  
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement