Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1.  
  2. #include <Servo.h>
  3.  
  4.  
  5.  
  6. #define echoPin 9 // Echo Pin
  7. #define trigPin 10 // Trigger Pin
  8. #define echoPinleft 12 // Echo Pin
  9. #define trigPinleft 13 // Trigger Pin
  10. #define echoPinright 7 // Echo Pin
  11. #define trigPinright 8 // Trigger Pin
  12. #define L2 5
  13. #define L1 11
  14. #define L3 3
  15. #define L4 6
  16. #define M 30
  17. #define minDist 3
  18.  
  19. Servo myservo;
  20.  
  21. void setup() {
  22. Serial.begin(9600);
  23. pinMode(trigPin, OUTPUT);
  24. pinMode(echoPin, INPUT);
  25. pinMode(trigPinleft, OUTPUT);
  26. pinMode(echoPinleft, INPUT);
  27. pinMode(trigPinright, OUTPUT);
  28. pinMode(echoPinright, INPUT);
  29. pinMode (L1,OUTPUT);
  30. pinMode (L2,OUTPUT);
  31. pinMode (L3,OUTPUT);
  32. pinMode (L4,OUTPUT);
  33.  
  34. myservo.attach(4);
  35. myservo.write(90);
  36. }
  37.  
  38. double get_f(){
  39. digitalWrite(trigPin, LOW);
  40. delayMicroseconds(2);
  41.  
  42. digitalWrite(trigPin, HIGH);
  43. delayMicroseconds(10);
  44.  
  45. digitalWrite(trigPin, LOW);
  46. int duration = pulseIn(echoPin, HIGH);
  47.  
  48. //Calculate the distance (in cm) based on the speed of sound.
  49. int distance = duration/58.2;
  50.  
  51. return distance;
  52. }
  53.  
  54. double get_r(){
  55. digitalWrite(trigPinright, LOW);
  56. delayMicroseconds(2);
  57.  
  58. digitalWrite(trigPinright, HIGH);
  59. delayMicroseconds(10);
  60.  
  61. digitalWrite(trigPinright, LOW);
  62. int durationr = pulseIn(echoPinright, HIGH);
  63.  
  64. //Calculate the distance (in cm) based on the speed of sound.
  65. int distancer = durationr/58.2;
  66.  
  67. return distancer;
  68. }
  69.  
  70. double get_l(){
  71. digitalWrite(trigPinleft, LOW);
  72. delayMicroseconds(2);
  73.  
  74. digitalWrite(trigPinleft, HIGH);
  75. delayMicroseconds(10);
  76.  
  77. digitalWrite(trigPinleft, LOW);
  78. int durationl = pulseIn(echoPinleft, HIGH);
  79.  
  80. //Calculate the distance (in cm) based on the speed of sound.
  81. int distancel = durationl/58.2;
  82.  
  83. return distancel;
  84. }
  85.  
  86. int generate(int row1 , int col1){
  87. if(row1>0x08){
  88. int nr = row1 - 0x09;
  89. row1 = 0x08 - nr;
  90. }
  91.  
  92. if(col1>0x08){
  93. int ncc = col1 - 0x09;
  94. col1 = 0x08 - ncc;
  95. }
  96.  
  97.  
  98.  
  99. return (0x0f - (col1-0x01) - (row1 - 0x01));
  100. }
  101.  
  102. void robMove(int l1, int l2, int r1, int r2)
  103. {
  104. digitalWrite(L1, l1);
  105. digitalWrite(L3, r1);
  106. digitalWrite(L2, l2);
  107. digitalWrite(L4, r2);
  108. }
  109.  
  110. void reverse(){
  111. robMove(LOW , LOW , LOW , LOW);
  112. delay(1000);
  113.  
  114. robMove(HIGH , LOW , LOW , HIGH);
  115. delay(800);
  116.  
  117. robMove(HIGH , LOW , HIGH , LOW);
  118. }
  119.  
  120. void lturn(){
  121. robMove(LOW , LOW , LOW , LOW);
  122. delay(1000);
  123.  
  124. robMove(LOW , LOW , HIGH , LOW);
  125. delay(700);
  126. }
  127.  
  128. void rturn(){
  129. robMove(LOW , LOW , LOW , LOW);
  130. delay(1000);
  131.  
  132. robMove(HIGH , LOW , LOW , LOW);
  133. delay(700);
  134. }
  135.  
  136. void loop() {
  137.  
  138.  
  139. Serial.print("right--> ");Serial.println(get_r());
  140. Serial.print("left--> "); Serial.println(get_l());
  141. Serial.print("front--> "); Serial.println(get_f());
  142.  
  143. //robMove(HIGH, LOW, HIGH, LOW);
  144. /*robMove(HIGH, LOW, HIGH, LOW);
  145. while(get_l() - get_r() > (double)minDist) robMove(LOW, LOW, HIGH, LOW);
  146. robMove(HIGH, LOW, HIGH, LOW);
  147. while(get_r() - get_l() > (double)minDist) robMove(HIGH, LOW, LOW, LOW);
  148. */
  149.  
  150. analogWrite(L3 , 50);
  151. analogWrite(L1 , 55);
  152. delay(2000);
  153. analogWrite(L1 , 0);
  154. analogWrite(L3 , 0);
  155. delay(2000);
  156. //return;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement