Guest User

Untitled

a guest
Dec 16th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. //Distance
  2. int d=20;
  3.  
  4. //Ultrasonic Pins Defined
  5. //L
  6. const int trigPin1 = 12;
  7. const int echoPin1 = 13;
  8. //F
  9. const int trigPin2 = 8;
  10. const int echoPin2 = 9;
  11. //R
  12. const int trigPin3 = 4;
  13. const int echoPin3 = 5;
  14.  
  15. //Vibration Motors Pins Defined
  16. //L
  17. const int g1 = 11;
  18. const int v1 = 10;
  19. //R
  20. const int v2 = 6;
  21. const int g2 = 7;
  22. //F
  23. const int v3 = 3;
  24. const int g3 = 2;
  25.  
  26. //Function for returning Distance in cms.
  27. long microsecondsToCentimeters(long microseconds)
  28. {
  29. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  30. // The ping travels out and back, so to find the distance of the
  31. // object we take half of the distance travelled.
  32. return microseconds / 29 / 2;
  33. }
  34.  
  35. //Function for Activating vibration motors
  36. void activatemotors(int cm1,int cm2,int cm3)
  37. {
  38. if(cm1<d)
  39. {
  40. analogWrite(v1, 150);
  41. }
  42. if(cm2<d)
  43. {
  44. analogWrite(v2, 150);
  45. }
  46. if(cm3<d)
  47. {
  48. analogWrite(v3, 150);
  49. }
  50. delay(250);
  51. analogWrite(v1, 0);
  52. analogWrite(v2, 0);
  53. analogWrite(v3, 0);
  54.  
  55. }
  56.  
  57.  
  58.  
  59. void setup() {
  60. // initialize serial communication:
  61. Serial.begin(9600);
  62.  
  63. //Defining Pins for Ultrasonic sensors
  64. pinMode(trigPin1, OUTPUT);
  65. pinMode(trigPin2, OUTPUT);
  66. pinMode(trigPin3, OUTPUT);
  67. pinMode(echoPin1, INPUT);
  68. pinMode(echoPin2, INPUT);
  69. pinMode(echoPin3, INPUT);
  70.  
  71. //Defining pins for Vibration motors
  72. pinMode(v1, OUTPUT);
  73. pinMode(g1, OUTPUT);
  74. pinMode(v2, OUTPUT);
  75. pinMode(g2, OUTPUT);
  76. pinMode(v3, OUTPUT);
  77. pinMode(g3, OUTPUT);
  78.  
  79. //Turning vibration Motors OFF initially
  80. analogWrite(v1, 0);
  81. digitalWrite(g1, LOW);
  82. analogWrite(v2, 0);
  83. digitalWrite(g2, LOW);
  84. analogWrite(v3, 0);
  85. digitalWrite(g3, LOW);
  86. }
  87.  
  88. void loop()
  89. {
  90. // establish variables for duration of the ping,
  91. // and the distance result in inches and centimeters:
  92. long duration1,duration2,duration3, cm1,cm2,cm3;
  93.  
  94. // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  95. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  96.  
  97. digitalWrite(trigPin1, LOW);
  98. delayMicroseconds(2);
  99. digitalWrite(trigPin1, HIGH);
  100. delayMicroseconds(10);
  101. duration1 = pulseIn(echoPin1, HIGH);
  102. cm1 = microsecondsToCentimeters(duration1);
  103.  
  104.  
  105. digitalWrite(trigPin2, LOW);
  106. delayMicroseconds(2);
  107. digitalWrite(trigPin2, HIGH);
  108. delayMicroseconds(10);
  109. duration2 = pulseIn(echoPin2, HIGH);
  110. cm2 = microsecondsToCentimeters(duration2);
  111.  
  112.  
  113.  
  114. digitalWrite(trigPin3, LOW);
  115. delayMicroseconds(2);
  116. digitalWrite(trigPin3, HIGH);
  117. delayMicroseconds(10);
  118. duration3 = pulseIn(echoPin3, HIGH);
  119. cm3 = microsecondsToCentimeters(duration3);
  120.  
  121.  
  122. /*digitalWrite(trigPin1, LOW);
  123. digitalWrite(trigPin2, LOW);
  124. digitalWrite(trigPin3, LOW);
  125.  
  126. // Read the signal from the sensor: a HIGH pulse whose
  127. // duration is the time (in microseconds) from the sending
  128. // of the ping to the reception of its echo off of an object.
  129.  
  130.  
  131.  
  132.  
  133. // convert the time into a distance
  134.  
  135. */
  136.  
  137. Serial.print(cm1);
  138. Serial.print("cm\t");
  139. Serial.print(cm2);
  140. Serial.print("cm\t");
  141. Serial.print(cm3);
  142. Serial.print("cm\t");
  143. Serial.println();
  144.  
  145. activatemotors(cm1,cm2,cm3);
  146. }
Add Comment
Please, Sign In to add comment