Advertisement
safwan092

Untitled

May 29th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #define ENA 3 //must be PWM (~)
  2. #define IN1 4
  3. #define IN2 5
  4. #define IN3 6
  5. #define IN4 7
  6. #define ENB 9 //must be PWM (~)
  7. #define tr1 10
  8. #define ec1 11
  9. #define tr2 12
  10. #define ec2 13
  11.  
  12. byte com = 0; //reply from voice recognition
  13. long duration1, duration2;
  14. int distance1, distance2;
  15. int f = 0;
  16. int b = 0;
  17.  
  18. void setup() {
  19. Serial.begin(9600);
  20. pinMode(ENA, OUTPUT);
  21. pinMode(IN1, OUTPUT);
  22. pinMode(IN2, OUTPUT);
  23. pinMode(IN3, OUTPUT);
  24. pinMode(IN4, OUTPUT);
  25. pinMode(ENB, OUTPUT);
  26. pinMode(tr1, OUTPUT);
  27. pinMode(ec1, INPUT);
  28. pinMode(tr2, OUTPUT);
  29. pinMode(ec2, INPUT);
  30. analogWrite(ENA, 100); //0-255 80
  31. analogWrite(ENB, 100); //0-255 80
  32. stopp();
  33. delay(2000);
  34. Serial.write(0xAA);
  35. Serial.write(0x37);//Switch to Compact Mode
  36. delay(1000);
  37. Serial.write(0xAA);
  38. Serial.write(0x23);//Import group 1 and be ready for voice instruction
  39. }
  40.  
  41. void loop() {
  42. /*
  43. US1();
  44. US2();
  45. front();
  46. delay(1000);
  47. back();
  48. delay(1000);
  49. left();
  50. delay(1000);
  51. right();
  52. delay(1000);
  53. stopp();
  54. delay(1000);
  55. */
  56. US1();
  57. US2();
  58. if (f == 1 && distance1 < 10) {
  59. stopp();
  60. delay(1000);
  61. back();
  62. delay(1000);
  63. stopp();
  64. f = 0;
  65. }
  66. if (b == 1 && distance2 < 10) {
  67. stopp();
  68. delay(1000);
  69. front();
  70. delay(1000);
  71. stopp();
  72. b = 0;
  73. }
  74.  
  75. while (Serial.available())
  76. {
  77. com = Serial.read();
  78.  
  79. switch (com)
  80. {
  81. case 0x11:
  82. //front
  83. f = 1;
  84. if (f == 1 && distance1 < 10) {
  85. stopp();
  86. delay(1000);
  87. back();
  88. delay(1000);
  89. stopp();
  90. f = 0;
  91. }
  92. else if (f == 1 && distance1 > 10) {
  93. front();
  94. //delay(2000);
  95. }
  96. break;
  97. case 0x12:
  98. //back
  99. b = 1;
  100. if (b == 1 && distance2 < 10) {
  101. stopp();
  102. delay(1000);
  103. front();
  104. delay(1000);
  105. stopp();
  106. b = 0;
  107. }
  108. else if (b == 1 && distance2 > 10) {
  109. back();
  110. //delay(2000);
  111. }
  112. break;
  113. case 0x13:
  114. // right
  115. right();
  116. delay(1000);
  117. stopp();
  118. break;
  119. case 0x14:
  120. // left
  121. left();
  122. delay(1000);
  123. stopp();
  124. break;
  125. case 0x15:
  126. // stop
  127. stopp();
  128. break;
  129. }// end of SWITCH
  130. }
  131.  
  132. }// end of LOOP
  133.  
  134. void front() {
  135. digitalWrite(IN1, 1);
  136. digitalWrite(IN2, 0);
  137. digitalWrite(IN3, 1);
  138. digitalWrite(IN4, 0);
  139. }
  140.  
  141. void back() {
  142. digitalWrite(IN1, 0);
  143. digitalWrite(IN2, 1);
  144. digitalWrite(IN3, 0);
  145. digitalWrite(IN4, 1);
  146. }
  147.  
  148. void left() {
  149. digitalWrite(IN1, 1);
  150. digitalWrite(IN2, 0);
  151. digitalWrite(IN3, 0);
  152. digitalWrite(IN4, 1);
  153. }
  154.  
  155. void right() {
  156. digitalWrite(IN1, 0);
  157. digitalWrite(IN2, 1);
  158. digitalWrite(IN3, 1);
  159. digitalWrite(IN4, 0);
  160. }
  161.  
  162. void stopp() {
  163. digitalWrite(IN1, 0);
  164. digitalWrite(IN2, 0);
  165. digitalWrite(IN3, 0);
  166. digitalWrite(IN4, 0);
  167. }
  168.  
  169. void US1() {
  170. digitalWrite(tr1, LOW);
  171. delayMicroseconds(5);
  172. digitalWrite(tr1, HIGH);
  173. delayMicroseconds(10);
  174. digitalWrite(tr1, LOW);
  175. duration1 = pulseIn(ec1, HIGH);
  176. distance1 = duration1 * 0.034 / 2;
  177. //Serial.print("Distance1 = ");
  178. //Serial.print(distance1);
  179. //Serial.println(" cm\t\t");
  180. }
  181.  
  182. void US2() {
  183. digitalWrite(tr2, LOW);
  184. delayMicroseconds(5);
  185. digitalWrite(tr2, HIGH);
  186. delayMicroseconds(10);
  187. digitalWrite(tr2, LOW);
  188. duration2 = pulseIn(ec2, HIGH);
  189. distance2 = duration2 * 0.034 / 2;
  190. // Serial.print("Distance2 = ");
  191. // Serial.print(distance2);
  192. // Serial.println(" cm");
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement