Advertisement
safwan092

Untitled

Apr 6th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include <Servo.h> //Servo motor library. This is standard library
  2. #include <NewPing.h> //Ultrasonic sensor function library. You must install this library
  3. //our L298N control pins
  4. const int LeftMotorForward = 4;//5
  5. const int LeftMotorBackward = 5;//4
  6. const int RightMotorForward = 2;//3
  7. const int RightMotorBackward = 3;//2
  8. //sensor pins
  9. #define trig_pin A3 //analog input 1
  10. #define echo_pin A2 //analog input 2
  11.  
  12. #define maximum_distance 200
  13. boolean goesForward = false;
  14. int distance = 100;
  15. NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
  16. Servo servo_motor; //our servo name
  17. void setup() {
  18. pinMode(RightMotorForward, OUTPUT);
  19. pinMode(LeftMotorForward, OUTPUT);
  20. pinMode(LeftMotorBackward, OUTPUT);
  21. pinMode(RightMotorBackward, OUTPUT);
  22. servo_motor.attach(11); //our servo pin
  23. servo_motor.write(90); delay(2000);
  24. distance = readPing(); delay(100);
  25. distance = readPing(); delay(100);
  26.  
  27. distance = readPing(); delay(100);
  28. distance = readPing(); delay(100);
  29. }
  30. void loop() {
  31. int distanceRight = 0; int distanceLeft = 0; delay(50);
  32. if (distance <= 20) {
  33. moveStop();
  34. delay(300); moveBackward(); delay(400);
  35. moveStop();
  36. delay(300);
  37. distanceRight = lookRight(); delay(300);
  38. distanceLeft = lookLeft(); delay(300);
  39. if (distance >= distanceLeft) {
  40. turnRight();
  41. moveStop();
  42. } else {
  43. turnLeft();
  44. moveStop();
  45. }
  46. } else {
  47. moveForward();
  48. }
  49. distance = readPing();
  50. }
  51. int lookRight() {
  52. servo_motor.write(10); delay(500);
  53. int distance = readPing(); delay(100);
  54.  
  55. servo_motor.write(90);
  56. return distance;
  57. }
  58. int lookLeft() {
  59. servo_motor.write(170); delay(500);
  60. int distance = readPing();
  61. delay(100);
  62. servo_motor.write(90);
  63. return distance;
  64. delay(100);
  65. }
  66. int readPing() {
  67. delay(70);
  68. int cm = sonar.ping_cm(); if (cm == 0) {
  69. cm = 250;
  70. }
  71. return cm;
  72. }
  73. void moveStop() {
  74. digitalWrite(RightMotorForward, LOW);
  75. digitalWrite(LeftMotorForward, LOW);
  76. digitalWrite(RightMotorBackward, LOW);
  77. digitalWrite(LeftMotorBackward, LOW);
  78. }
  79. void moveForward() {
  80. if (!goesForward) {
  81. goesForward = true;
  82. digitalWrite(LeftMotorForward, HIGH);
  83. digitalWrite(RightMotorForward, HIGH);
  84. digitalWrite(LeftMotorBackward, LOW);
  85. digitalWrite(RightMotorBackward, LOW);
  86. }
  87. }
  88.  
  89. void moveBackward() {
  90. goesForward = false;
  91. digitalWrite(LeftMotorBackward, HIGH);
  92. digitalWrite(RightMotorBackward, HIGH);
  93. digitalWrite(LeftMotorForward, LOW);
  94. digitalWrite(RightMotorForward, LOW);
  95. }
  96. void turnRight() {
  97. digitalWrite(LeftMotorForward, HIGH);
  98. digitalWrite(RightMotorBackward, HIGH);
  99. digitalWrite(LeftMotorBackward, LOW);
  100. digitalWrite(RightMotorForward, LOW);
  101. delay(500);
  102. digitalWrite(LeftMotorForward, HIGH);
  103. digitalWrite(RightMotorForward, HIGH);
  104. digitalWrite(LeftMotorBackward, LOW);
  105. digitalWrite(RightMotorBackward, LOW);
  106. }
  107. void turnLeft() {
  108. digitalWrite(LeftMotorBackward, HIGH);
  109. digitalWrite(RightMotorForward, HIGH);
  110. digitalWrite(LeftMotorForward, LOW);
  111. digitalWrite(RightMotorBackward, LOW);
  112. delay(500);
  113. digitalWrite(LeftMotorForward, HIGH);
  114.  
  115. digitalWrite(RightMotorForward, HIGH);
  116. digitalWrite(LeftMotorBackward, LOW);
  117. digitalWrite(RightMotorBackward, LOW);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement