Advertisement
Guest User

arduino

a guest
Oct 17th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. #include <Servo.h>
  2. #define trig 3 //chân trig của HC-SR04
  3. #define echo 4//chân echo của HC-SR04
  4.  
  5. Servo srf05; // create servo object to control a servo
  6. #define inA1 6 //Định nghĩa chân in1 của động cơ A
  7. #define inA2 7 //Định nghĩa chân in2 của động cơ A
  8. #define inB1 8 //Định nghĩa chân in1 của động cơ B
  9. #define inB2 9 //Định nghĩa chân in2 của động cơ B
  10.  
  11. #define SERVO_PIN 10
  12.  
  13. Servo gServo;
  14.  
  15. void setup()
  16. {
  17. gServo.attach(SERVO_PIN);
  18. }
  19.  
  20. void loop()
  21. {
  22. gServo.write(0);
  23. delay(1000);
  24.  
  25. gServo.write(90);
  26. delay(1000);
  27.  
  28. gServo.write(180);
  29. delay(1000);
  30.  
  31.  
  32.  
  33. pinMode(inA1, OUTPUT);//Set chân in1 của dc A là output
  34. pinMode(inA2, OUTPUT);//Set chân in2 của dc A là output
  35. pinMode(inB1, OUTPUT);//Set chân in1 của dc B là output
  36. pinMode(inB2, OUTPUT);//Set chân in2 của dc B là output
  37. pinMode(trig,OUTPUT);//chân trig sẽ phát tín hiệu
  38. pinMode(echo,INPUT);//chân echo sẽ nhận tín hiệu
  39. Serial.begin(9600);
  40. srf05.attach(5); // attaches the servo on pin 9 to the servo object
  41. }
  42.  
  43. void loop()
  44. {
  45. objectAvoiderChooseWay ( inA1 , inA2, inB1, inB2, 50, 700);
  46.  
  47. digitalWrite(6, HIGH);
  48. digitalWrite(7, LOW);
  49. digitalWrite(8, HIGH);
  50. digitalWrite(9,LOW);
  51. }
  52.  
  53. int objectDistance_cm (byte angle)
  54. {
  55. srf05.write(angle);
  56. delay(500);
  57. unsigned long duration; //biến đo thời gian
  58. int distance; //biến lưu khoảng cách /* phát xung từ chân trig */
  59.  
  60. digitalWrite(trig,0); //tắt chân trig
  61. delayMicroseconds(2);
  62. digitalWrite(trig,1); // phát xung từ chân trig
  63. delayMicroseconds(5);// xung có độ dài 5 microSeconds
  64. digitalWrite(trig,0);//tắt chân trig /* tính toán thời gian */
  65.  
  66. duration = pulseIn(echo,HIGH);//đo độ rộng xung HIGH ở chân echo.
  67. distance = int(duration/2/29.412);//tính khoảng cách đến vật.
  68. return distance;
  69. }
  70.  
  71. void motorControlNoSpeed (byte in1,byte in2, byte direct)
  72. {
  73.  
  74. switch (direct)
  75. {
  76. case 0:// Dừng không quay
  77. digitalWrite(in1,LOW);
  78. digitalWrite(in2,LOW);
  79. break;
  80. case 1:// Quay chiều thứ 1
  81. digitalWrite(in1,HIGH);
  82. digitalWrite(in2,LOW);
  83. break;
  84. case 2:// Quay chiều thứ 2
  85. digitalWrite(in1,LOW);
  86. digitalWrite(in2,HIGH);
  87. break;
  88. //default:
  89. }
  90. }
  91.  
  92. void robotMover (byte inR1, byte inR2, byte inL1, byte inL2, byte action)
  93. {
  94.  
  95. switch (action)
  96. {
  97. case 0:// không di chuyển
  98. motorControlNoSpeed(inR1, inR2, 0);
  99. motorControlNoSpeed(inL1, inL2, 0);
  100. break;
  101. case 1://đi thẳng
  102. motorControlNoSpeed(inR1, inR2, 1);
  103. motorControlNoSpeed(inL1, inL2, 1);
  104. break;
  105. case 2:// lùi lại
  106. motorControlNoSpeed(inR1, inR2, 2);
  107. motorControlNoSpeed(inL1, inL2, 2);
  108. break;
  109. case 3:// quay trái
  110. motorControlNoSpeed(inR1, inR2, 1);
  111. motorControlNoSpeed(inL1, inL2, 2);
  112. break;
  113. case 4:// quay phải
  114. motorControlNoSpeed(inR1, inR2, 2);
  115. motorControlNoSpeed(inL1, inL2, 1);
  116. break;
  117. case 5:// rẽ trái
  118. motorControlNoSpeed(inR1, inR2, 1);
  119. motorControlNoSpeed(inL1, inL2, 0);
  120. break;
  121. case 6:// rẽ phải
  122. motorControlNoSpeed(inR1, inR2, 0);
  123. motorControlNoSpeed(inL1, inL2, 1);
  124. break;
  125. case 7:// rẽ lùi trái
  126. motorControlNoSpeed(inR1, inR2, 2);
  127. motorControlNoSpeed(inL1, inL2, 0);
  128. break;
  129. case 8:// rẽ lùi phải
  130. motorControlNoSpeed(inR1, inR2, 0);
  131. motorControlNoSpeed(inL1, inL2, 2);
  132. break;
  133. default:
  134. action = 0;
  135.  
  136. }
  137. }
  138.  
  139. void objectAvoiderChooseWay (byte inR1, byte inR2, byte inL1, byte inL2, byte allow_distance, int turn_back_time)
  140. {
  141.  
  142. robotMover(inR1,inR2,inL1,inL2,1);
  143.  
  144. int front_distance = objectDistance_cm (90);
  145. int left_distance;
  146. int right_distance;
  147. int max_distance;
  148. if (front_distance > allow_distance)
  149. {
  150. robotMover(inR1,inR2,inL1,inL2,1);
  151.  
  152. delay(10);
  153. }
  154. if (front_distance <= allow_distance)
  155. {
  156. robotMover(inR1,inR2,inL1,inL2,2);
  157.  
  158. delay(300);
  159. robotMover(inR1,inR2,inL1,inL2,0);
  160. left_distance = objectDistance_cm (180); //đo khoảng cách bên trái
  161.  
  162. right_distance = objectDistance_cm (0); //đo khoảng cách bên phải
  163.  
  164. max_distance = max(left_distance,right_distance);// so sánh giá trị lớn nhất với khoảng cách bên phải (gán bằng cái lớn nhất)
  165. if (max_distance==left_distance)
  166. {
  167. robotMover(inR1,inR2,inL1,inL2,3);
  168.  
  169. delay (turn_back_time/2); // Nếu bên trái là khoảng cách lớn nhất thì rẽ trái
  170. }
  171. else
  172. {
  173. if (max_distance==right_distance)
  174. {
  175. robotMover(inR1,inR2,inL1,inL2,4);// Nếu bên phải có khoảng cách lớn nhất thì rẽ phải
  176.  
  177. delay (turn_back_time/2);
  178. }
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement