safwan092

Untitled

Mar 28th, 2022 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include<NewPing.h>
  2. #include<Servo.h>
  3. #include<AFMotor.h>
  4. #define RIGHT A3
  5. #define LEFT A2
  6. #define TRIGGER_PIN A1
  7. #define ECHO_PIN A0
  8. #define MAX_DISTANCE 100
  9.  
  10.  
  11. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
  12.  
  13.  
  14. AF_DCMotor Motor1(1, MOTOR12_1KHZ);
  15. AF_DCMotor Motor4(4, MOTOR34_1KHZ);
  16.  
  17. Servo myservo;
  18.  
  19. int pos = 0;
  20.  
  21. void setup() {
  22. // put your setup code here, to run once:
  23. Serial.begin(9600);
  24. myservo.attach(10);
  25. pinMode(RIGHT, INPUT);
  26. pinMode(LEFT, INPUT);
  27.  
  28. }
  29.  
  30. void loop() {
  31. delay(50);
  32. unsigned int distance = sonar.ping_cm();
  33. Serial.print("distance");
  34. Serial.println(distance);
  35.  
  36.  
  37. int Right_Value = digitalRead(RIGHT);
  38. int Left_Value = digitalRead(LEFT);
  39.  
  40. Serial.print("RIGHT");
  41. Serial.println(Right_Value);
  42. Serial.print("LEFT");
  43. Serial.println(Left_Value);
  44.  
  45. if ((Right_Value == 0) && (distance >= 5 && distance <= 30) && (Left_Value == 0)) {
  46. Motor1.setSpeed(150);
  47. Motor1.run(FORWARD);
  48.  
  49. Motor4.setSpeed(150);
  50. Motor4.run(FORWARD);
  51. } else if ((Right_Value == 1) && (Left_Value == 0)) {
  52. Motor1.setSpeed(150);
  53. Motor1.run(FORWARD);
  54.  
  55. Motor4.setSpeed(150);
  56. Motor4.run(BACKWARD);
  57. } else if ((Right_Value == 0) && (Left_Value == 1)) {
  58. Motor1.setSpeed(150);
  59. Motor1.run(BACKWARD);
  60.  
  61. Motor4.setSpeed(150);
  62. Motor4.run(FORWARD);
  63. } else if ((Right_Value == 0) && (Left_Value == 0)) {
  64. Motor1.setSpeed(0);
  65. Motor1.run(RELEASE);
  66.  
  67. Motor4.setSpeed(0);
  68. Motor4.run(RELEASE);
  69. } else if (distance > 1 && distance < 5) {
  70. Motor1.setSpeed(0);
  71. Motor1.run(RELEASE);
  72.  
  73. Motor4.setSpeed(0);
  74. Motor4.run(RELEASE);
  75. }
  76. }
Add Comment
Please, Sign In to add comment