Advertisement
Apparcane

Quant 3.0 BlackLine

May 21st, 2024
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.06 KB | Source Code | 0 0
  1. static int LeftSpeed = 5;
  2. static int RightSpeed = 10;
  3. static int LeftDir = 6;
  4. static int RightDir = 9;
  5.  
  6. static int LowSpeed = 50;
  7. static int MediumSpeed = 100;
  8. static int HighSpeed = 150;
  9.  
  10. const int Sensor_0_Pin = 0;
  11. const int Sensor_1_Pin = 1;
  12. const int Sensor_2_Pin = 2;
  13. const int Sensor_3_Pin = 3;
  14. const int Sensor_4_Pin = 4;
  15. const int Sensor_5_Pin = 5;
  16. const int Sensor_6_Pin = 6;
  17. const int Sensor_7_Pin = 7;
  18.  
  19. int sensor0 = 0;
  20. int sensor1 = 0;
  21. int sensor2 = 0;
  22. int sensor3 = 0;
  23. int sensor4 = 0;
  24. int sensor5 = 0;
  25. int sensor6 = 0;
  26. int sensor7 = 0;
  27.  
  28. void ReadSensors() {
  29.   // считываем состояние датчиков
  30.   sensor0 = digitalRead(Sensor_0_Pin);
  31.   sensor1 = digitalRead(Sensor_1_Pin);
  32.   sensor2 = digitalRead(Sensor_2_Pin);
  33.   sensor3 = digitalRead(Sensor_3_Pin);
  34.   sensor4 = digitalRead(Sensor_4_Pin);
  35.   sensor5 = digitalRead(Sensor_5_Pin);
  36.   sensor6 = digitalRead(Sensor_6_Pin);
  37.   sensor7 = digitalRead(Sensor_7_Pin);
  38. }
  39.  
  40.  
  41. int config() {
  42.  
  43.   ReadSensors();
  44.  
  45.   sensor0 = sensor0 * -1;
  46.   sensor1 = sensor1 * -1;
  47.   sensor2 = sensor2 * -1;
  48.   sensor3 = sensor3 * -1;
  49.  
  50.   int position = sensor0 + sensor1 + sensor2 + sensor4 + sensor5 + sensor6 + sensor7;
  51.  
  52.   Serial.println(position);
  53.  
  54.   return position;
  55. }
  56.  
  57. void Backward() {
  58.   //  Left Wheel
  59.   analogWrite(LeftSpeed, MediumSpeed);
  60.   digitalWrite(LeftDir, HIGH);
  61.   //  Right Wheel
  62.   analogWrite(RightSpeed, MediumSpeed);
  63.   digitalWrite(RightDir, HIGH);
  64. }
  65.  
  66. void Forward() {
  67.   //  Left Wheel
  68.   analogWrite(LeftSpeed, MediumSpeed);
  69.   digitalWrite(LeftDir, LOW);
  70.   //  Right Wheel
  71.   analogWrite(RightSpeed, MediumSpeed);
  72.   digitalWrite(RightDir, LOW);
  73. }
  74.  
  75. void Stop() {
  76.   //  Left Wheel
  77.   analogWrite(LeftSpeed, 0);
  78.   //  Right Wheel
  79.   analogWrite(RightSpeed, 0);
  80. }
  81.  
  82. void Right() {
  83.   //  Left Wheel
  84.   analogWrite(LeftSpeed, MediumSpeed);
  85.   digitalWrite(LeftDir, HIGH);
  86.   //  Right Wheel
  87.   analogWrite(RightSpeed, MediumSpeed);
  88.   digitalWrite(RightDir, LOW);
  89. }
  90.  
  91. void Left() {
  92.   //  Left Wheel
  93.   analogWrite(LeftSpeed, MediumSpeed);
  94.   digitalWrite(LeftDir, LOW);
  95.   //  Right Wheel
  96.   analogWrite(RightSpeed, MediumSpeed);
  97.   digitalWrite(RightDir, HIGH);
  98. }
  99.  
  100. void RightCircle() {
  101.   //  Left Wheel
  102.   analogWrite(LeftSpeed, HighSpeed);
  103.   digitalWrite(LeftDir, LOW);
  104.   //  Right Wheel
  105.   analogWrite(RightSpeed, LowSpeed);
  106.   digitalWrite(RightDir, LOW);
  107. }
  108.  
  109. void LeftCircle() {
  110.   //  Left Wheel
  111.   analogWrite(LeftSpeed, LowSpeed);
  112.   digitalWrite(LeftDir, LOW);
  113.   //  Right Wheel
  114.   analogWrite(RightSpeed, HighSpeed);
  115.   digitalWrite(RightDir, LOW);
  116. }
  117.  
  118. void setup() {
  119.   pinMode(LeftSpeed, OUTPUT);
  120.   pinMode(LeftDir, OUTPUT);
  121.   pinMode(RightDir, OUTPUT);
  122.   pinMode(RightSpeed, OUTPUT);
  123. }
  124.  
  125. void loop() {
  126.   int position = config()
  127.  
  128.   if (position == 0) {
  129.     Forward();
  130.   }
  131.   else if (position >= 1 && position <= 2) {
  132.     LeftCircle();
  133.   }
  134.   else if (position >= 3) {
  135.     Left();
  136.   }
  137.   else if (position <= -1 && position >= -2) {
  138.     RightCircle();
  139.   }
  140.   else if (position <= -3) {
  141.     Right();
  142.   }
  143.   else {
  144.     Forward();
  145.   }
  146. }
Tags: Arduino quant
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement