Apparcane

Labyrinth Quant

May 28th, 2023
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. // определяем пины для двигателей
  2. // 0 - скорость
  3. const int MOTOR_LEFT_0_PIN = 5;
  4. const int MOTOR_LEFT_1_PIN = 6;
  5. const int MOTOR_LEFT_2_PIN = 7;
  6. const int MOTOR_RIGHT_0_PIN = 10;
  7. const int MOTOR_RIGHT_1_PIN = 8;
  8. const int MOTOR_RIGHT_2_PIN = 9;
  9. const int BUZZER_PIN = 4;
  10.  
  11. const int MOTOR_SPEED = 80;
  12. const float MOTOR_SPEED_RATE = 0.6;
  13. const int MOTOR_SPEED_LIGHT_TURN = MOTOR_SPEED - 5;
  14. const int MOTOR_SPEED_HARD_TURN_HIGH = MOTOR_SPEED + 5;
  15. const int MOTOR_SPEED_HARD_TURN_LOW = 35;
  16.  
  17. const int pinSensorA5 = A5; //Left
  18. const int pinSensorA6 = A6; //Center
  19. const int pinSensorA7 = A7; //Right
  20.  
  21. //Пороговые значения для лабиринта
  22. const int thresholdLeft = 100;
  23. const int thresholdCenter = 110;
  24. const int thresholdRight = 100;
  25.  
  26. //Значения при которых нужно отехать назад
  27. const int STUCK_IN_WALL = 74;
  28.  
  29. int sensorA5;
  30. int sensorA6;
  31. int sensorA7;
  32.  
  33. void Forward() {
  34. //Left
  35. analogWrite(MOTOR_LEFT_0_PIN, MOTOR_SPEED);
  36. digitalWrite(MOTOR_LEFT_1_PIN, LOW);
  37. digitalWrite(MOTOR_LEFT_2_PIN, HIGH);
  38.  
  39. //Right
  40. analogWrite(MOTOR_RIGHT_0_PIN, MOTOR_SPEED);
  41. digitalWrite(MOTOR_RIGHT_1_PIN, HIGH);
  42. digitalWrite(MOTOR_RIGHT_2_PIN, LOW);
  43. Serial.println("Forward");
  44. }
  45.  
  46. void Stop() {
  47. //Left
  48. analogWrite(MOTOR_LEFT_0_PIN, 0);
  49.  
  50. //Right
  51. analogWrite(MOTOR_RIGHT_0_PIN, 0);
  52. Serial.println("Stop");
  53. }
  54.  
  55. void Backward() {
  56. //Left
  57. analogWrite(MOTOR_LEFT_0_PIN, MOTOR_SPEED);
  58. digitalWrite(MOTOR_LEFT_1_PIN, HIGH);
  59. digitalWrite(MOTOR_LEFT_2_PIN, LOW);
  60.  
  61. //Right
  62. analogWrite(MOTOR_RIGHT_0_PIN, MOTOR_SPEED);
  63. digitalWrite(MOTOR_RIGHT_1_PIN, LOW);
  64. digitalWrite(MOTOR_RIGHT_2_PIN, HIGH);
  65. Serial.println("Forward");
  66. }
  67.  
  68. int RightCircle() {
  69. //Left
  70. analogWrite(MOTOR_LEFT_0_PIN, MOTOR_SPEED_LIGHT_TURN);
  71. digitalWrite(MOTOR_LEFT_1_PIN, LOW);
  72. digitalWrite(MOTOR_LEFT_2_PIN, HIGH);
  73.  
  74. //Right
  75. analogWrite(MOTOR_RIGHT_0_PIN, MOTOR_SPEED * MOTOR_SPEED_RATE);
  76. digitalWrite(MOTOR_RIGHT_1_PIN, HIGH);
  77. digitalWrite(MOTOR_RIGHT_2_PIN, LOW);
  78.  
  79. Serial.println("RightCircle");
  80. }
  81.  
  82. int LeftCircle() {
  83. //Left
  84. analogWrite(MOTOR_LEFT_0_PIN, MOTOR_SPEED * MOTOR_SPEED_RATE);
  85. digitalWrite(MOTOR_LEFT_1_PIN, LOW);
  86. digitalWrite(MOTOR_LEFT_2_PIN, HIGH);
  87.  
  88. //Right
  89. analogWrite(MOTOR_RIGHT_0_PIN, MOTOR_SPEED_LIGHT_TURN);
  90. digitalWrite(MOTOR_RIGHT_1_PIN, HIGH);
  91. digitalWrite(MOTOR_RIGHT_2_PIN, LOW);
  92.  
  93. Serial.println("LeftCircle");
  94. }
  95.  
  96. int Right() {
  97. //Left
  98. analogWrite(MOTOR_LEFT_0_PIN, MOTOR_SPEED_HARD_TURN_HIGH);
  99. digitalWrite(MOTOR_LEFT_1_PIN, LOW);
  100. digitalWrite(MOTOR_LEFT_2_PIN, HIGH);
  101.  
  102. //Right
  103. analogWrite(MOTOR_RIGHT_0_PIN, MOTOR_SPEED_HARD_TURN_LOW);
  104. digitalWrite(MOTOR_RIGHT_1_PIN, HIGH);
  105. digitalWrite(MOTOR_RIGHT_2_PIN, LOW);
  106.  
  107. Serial.println("Right");
  108. }
  109.  
  110. int Left() {
  111. //Left
  112. analogWrite(MOTOR_LEFT_0_PIN, MOTOR_SPEED_HARD_TURN_LOW);
  113. digitalWrite(MOTOR_LEFT_1_PIN, LOW);
  114. digitalWrite(MOTOR_LEFT_2_PIN, HIGH);
  115.  
  116. //Right
  117. analogWrite(MOTOR_RIGHT_0_PIN, MOTOR_SPEED_HARD_TURN_HIGH);
  118. digitalWrite(MOTOR_RIGHT_1_PIN, HIGH);
  119. digitalWrite(MOTOR_RIGHT_2_PIN, LOW);
  120.  
  121. Serial.println("Left");
  122. }
  123.  
  124. void sensorRead() {
  125. sensorA5 = analogRead(pinSensorA5);
  126. sensorA6 = analogRead(pinSensorA6);
  127. sensorA7 = analogRead(pinSensorA7);
  128.  
  129. Serial.print("Left: "); Serial.print(sensorA5); Serial.println(" ");
  130. Serial.print("Center: "); Serial.print(sensorA6); Serial.println(" ");
  131. Serial.print("RIght: "); Serial.print(sensorA7); Serial.println(" ");
  132. }
  133.  
  134. int config(int Left, int Right)
  135. {
  136. Left = Left * -1;
  137. return Right - Left;
  138. }
  139.  
  140. void setup() {
  141.  
  142. //Left
  143. digitalWrite(MOTOR_LEFT_1_PIN, LOW);
  144. digitalWrite(MOTOR_LEFT_2_PIN, HIGH);
  145.  
  146. //Right
  147. digitalWrite(MOTOR_RIGHT_1_PIN, HIGH);
  148. digitalWrite(MOTOR_RIGHT_2_PIN, LOW);
  149.  
  150. tone(BUZZER_PIN, 1000);
  151. digitalWrite(BUZZER_PIN, HIGH);
  152. delay(500);
  153. noTone(BUZZER_PIN);
  154. digitalWrite(BUZZER_PIN, LOW);
  155. delay(500);
  156. sensorRead();
  157. delay(1000);
  158. tone(BUZZER_PIN, 500);
  159. digitalWrite(BUZZER_PIN, HIGH);
  160. delay(500);
  161. noTone(BUZZER_PIN);
  162. digitalWrite(BUZZER_PIN, LOW);
  163.  
  164. Serial.begin(9600);
  165. }
  166.  
  167. void loop() {
  168. sensorRead();
  169.  
  170. // delay(1000);
  171.  
  172. if (sensorA5 < sensorA7 && sensorA6 < thresholdCenter)
  173. {
  174. LeftCircle();
  175. }
  176. else if (sensorA5 < sensorA7 && sensorA6 > thresholdCenter)
  177. {
  178. Left();
  179. }
  180. else if (sensorA5 > sensorA7 && sensorA6 < thresholdCenter)
  181. {
  182. RightCircle();
  183. }
  184. else if (sensorA5 > sensorA7 && sensorA6 > thresholdCenter)
  185. {
  186. Right();
  187. }
  188. else
  189. {
  190. Forward();
  191. }
  192.  
  193. if (sensorA5 > STUCK_IN_WALL || sensorA6 > STUCK_IN_WALL || sensorA7 > STUCK_IN_WALL)
  194. {
  195. Backward();
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment