Apparcane

Quant BT controll for BT RC Controller

May 14th, 2023 (edited)
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. //Режим работы
  4. // 1 - Bleutooth
  5. // 2 - Labyrinth
  6. // 3 - BlackLine
  7. int mode = 2;
  8.  
  9. // определяем пины для двигателей
  10. // 0 - скорость
  11. const int MOTOR_LEFT_0_PIN = 5;
  12. const int MOTOR_LEFT_1_PIN = 6;
  13. const int MOTOR_LEFT_2_PIN = 7;
  14. const int MOTOR_RIGHT_0_PIN = 10;
  15. const int MOTOR_RIGHT_1_PIN = 8;
  16. const int MOTOR_RIGHT_2_PIN = 9;
  17.  
  18. int MOTOR_SPEED = 50;
  19. const int MOTOR_SPEED_KOF = 4;
  20.  
  21. const int pinSensorA5 = A5; //Left
  22. const int pinSensorA6 = A6; //Center
  23. const int pinSensorA7 = A7; //Right
  24.  
  25. //Пороговые значения для лабиринта
  26. const int threshold1 = 25;
  27. const int threshold2 = 40;
  28. const int threshold3 = 25;
  29.  
  30. int sensorA5;
  31. int sensorA6;
  32. int sensorA7;
  33.  
  34. SoftwareSerial bt(2, 3); //rx, tx
  35.  
  36. void Forward() {
  37. //Left
  38. analogWrite(MOTOR_LEFT_0_PIN, MOTOR_SPEED);
  39. digitalWrite(MOTOR_LEFT_1_PIN, LOW);
  40. digitalWrite(MOTOR_LEFT_2_PIN, HIGH);
  41.  
  42. //Right
  43. analogWrite(MOTOR_RIGHT_0_PIN, MOTOR_SPEED);
  44. digitalWrite(MOTOR_RIGHT_1_PIN, HIGH);
  45. digitalWrite(MOTOR_RIGHT_2_PIN, LOW);
  46. Serial.println("Forward");
  47. }
  48.  
  49. void Stop() {
  50. //Left
  51. analogWrite(MOTOR_LEFT_0_PIN, 0);
  52.  
  53. //Right
  54. analogWrite(MOTOR_RIGHT_0_PIN, 0);
  55. Serial.println("Stop");
  56. }
  57.  
  58. void Backward() {
  59. //Left
  60. analogWrite(MOTOR_LEFT_0_PIN, MOTOR_SPEED);
  61. digitalWrite(MOTOR_LEFT_1_PIN, HIGH);
  62. digitalWrite(MOTOR_LEFT_2_PIN, LOW);
  63.  
  64. //Right
  65. analogWrite(MOTOR_RIGHT_0_PIN, MOTOR_SPEED);
  66. digitalWrite(MOTOR_RIGHT_1_PIN, LOW);
  67. digitalWrite(MOTOR_RIGHT_2_PIN, HIGH);
  68. Serial.println("Forward");
  69. }
  70.  
  71. void Right() {
  72. //Left
  73. analogWrite(MOTOR_LEFT_0_PIN, MOTOR_SPEED);
  74.  
  75. //Right
  76. analogWrite(MOTOR_RIGHT_0_PIN, MOTOR_SPEED / MOTOR_SPEED_KOF);
  77.  
  78. Serial.println("Right");
  79. }
  80.  
  81. void Left() {
  82. //Left
  83. analogWrite(MOTOR_LEFT_0_PIN, MOTOR_SPEED / MOTOR_SPEED_KOF );
  84.  
  85. //Right
  86. analogWrite(MOTOR_RIGHT_0_PIN, MOTOR_SPEED);
  87.  
  88. Serial.println("Left");
  89. }
  90.  
  91. void sensorRead() {
  92. sensorA5 = analogRead(pinSensorA5);
  93. sensorA6 = analogRead(pinSensorA6);
  94. sensorA7 = analogRead(pinSensorA7);
  95.  
  96. Serial.println("Left: "); Serial.print(sensorA5);
  97. Serial.println("Center: "); Serial.print(sensorA6);
  98. Serial.println("RIght: "); Serial.print(sensorA7);
  99. }
  100.  
  101. void setup() {
  102.  
  103. bt.begin(9600);
  104. Serial.begin(9600);
  105. }
  106.  
  107. char answer;
  108.  
  109. void loop() {
  110. sensorRead();
  111. // delay(1000);
  112. if (bt.available())
  113. {
  114. answer = bt.read();
  115. Serial.println(answer);
  116.  
  117. if (answer == 'X') // set mode = 1
  118. {
  119. mode = 2;
  120. }
  121. else if (answer == 'x') // set mode = 2
  122. {
  123. mode = 1;
  124. }
  125.  
  126. if (mode == 1)
  127. {
  128. if (answer == 'F') {
  129. Forward();
  130. }
  131. else if (answer == 'B') {
  132. Backward();
  133. }
  134. else if (answer == 'L') {
  135. Left();
  136. }
  137. else if (answer == 'R') {
  138. Right();
  139. }
  140. else if (answer == 'V') {
  141. tone(4, 500);
  142. digitalWrite(4, HIGH);
  143. }
  144. else if (answer == 'v') {
  145. noTone(4);
  146. digitalWrite(4, LOW);
  147. }
  148. else if (answer == 'S') {
  149. Stop();
  150. }
  151. else if (isDigit(answer)) {
  152. MOTOR_SPEED = map(atoi(&answer), 0, 10, 0, 255);
  153. }
  154.  
  155. if (sensorA5 >= 100 || sensorA6 >= 90 || sensorA7 >= 100) {
  156. Backward();
  157. delay(300);
  158. Forward();
  159. }
  160. }
  161. }
  162. if (mode == 2)
  163. { // sensorA5 sensorA6 sensorA7
  164. if (sensorA5 < threshold1 && sensorA6 < threshold2 && sensorA7 < threshold3) {
  165. // Нет препятствий, двигаемся вперед
  166. Forward();
  167. } else if (sensorA5 > threshold1 && sensorA6 < threshold2 && sensorA7 < threshold3) {
  168. // Препятствие слева, поворачиваем вправо
  169. Left();
  170. } else if (sensorA5 < threshold1 && sensorA6 < threshold2 && sensorA7 > threshold3) {
  171. // Препятствие справа, поворачиваем влево
  172. Right();
  173. } else if (sensorA5 > threshold1 && sensorA6 > threshold2 && sensorA7 < threshold3) {
  174. // Препятствие спереди, поворачиваем влево
  175. //Left();
  176. } else {
  177. // Препятствия с обеих сторон или со всех сторон, поворачиваем вправо
  178. Right();
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment