Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. const int trigPin = A1;
  2. const int echoPin = A2;
  3. int Motor1 = 13;
  4. int Motor2 = 12;
  5. int en1 = 11;
  6. int en2 = 10;
  7. int Motor3 = 9;
  8. int Motor4 = 8;
  9.  
  10. void setup() {
  11. Serial.begin(9600);
  12. pinMode(Motor1,OUTPUT);
  13. pinMode(Motor2,OUTPUT);
  14. pinMode(Motor3,OUTPUT);
  15. pinMode(Motor4,OUTPUT);
  16. pinMode(en1,OUTPUT);
  17. pinMode(en2,OUTPUT);
  18. }
  19.  
  20. void loop() {
  21. long duration, cm;
  22.  
  23. pinMode(trigPin, OUTPUT);
  24. digitalWrite(trigPin, LOW);
  25. delayMicrosecond(2);
  26. digitalWrite(trigPin, HIGH);
  27. delayMicrosecond(5);
  28. digitalWrite(trigPin, LOW);
  29.  
  30. pinMode(echoPin, INPUT);
  31. duration = pulseIn(echoPin, HIGH);
  32.  
  33. cm = microsecondsToCentimeters(duration);
  34.  
  35. Serial.print(cm);
  36. Serial.print("cm");
  37. serial.println();
  38.  
  39. delay(100);
  40.  
  41. if (cm<-40){
  42. digitalWrite(Motor1, LOW);
  43. digitalWrite(Motor2, HIGH);
  44. digitalWrite(Motor3, HIGH);
  45. digitalWrite(Motor4, LOW);
  46. digitalWrite(en1, 255);
  47. digitalWrite(en2, 255);
  48. }
  49. else {
  50. digitalWrite(Motor1, LOW);
  51. digitalWrite(Motor2, HIGH);
  52. digitalWrite(Motor3, LOW);
  53. digitalWrite(Motor4, HIGH);
  54. digitalWrite(en1, 255);
  55. digitalWrite(en2, 255);
  56. }
  57. }
  58. long microsecondsToCentimeters(long microseconds) {
  59. return microsecond / 29 / 2;
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement