Advertisement
Semior001

Autom

Oct 20th, 2016
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.59 KB | None | 0 0
  1. const int motorPin_left = 4;
  2. const int reversePin_left= 3;
  3. const int motorPin_right = 5;
  4. const int reversePin_right = 6;
  5. const int leftFront_sensor = A0;
  6. const int rightFront_sensor = A1;
  7. const int leftSide_sensor = A2;
  8. const int rightSide_sensor = A3;
  9. const int sensorMin = 0;     // sensor minimum
  10. const int sensorMax = 1024;  // sensor maximum
  11. const int reverseTiming = 3400;
  12.  
  13. enum direction {
  14.     NORTH,
  15.     EAST,
  16.     SOUTH,
  17.     WEST
  18. };
  19.  
  20. // Хранят координаты робота
  21. int coord[2] = {0, 0};
  22. int coord_direction = SOUTH;
  23. int i;
  24.  
  25. // Маршрут робота
  26. const int route[][3] = {
  27.     {0, 1},
  28.     {1, 1},
  29.     {1, 0},
  30.     {2, 0},
  31.     {2, 1},
  32.     {3, 1},
  33.     {3, 0},
  34.     {4, 0},
  35.     {4, 1},
  36.     {5, 1},
  37.     {5, 2},
  38.     {0, 2},
  39.     {0, 0}
  40. };
  41.  
  42. void setup(){
  43.     pinMode(motorPin_left, OUTPUT);
  44.     pinMode(reversePin_left, OUTPUT);
  45.     pinMode(motorPin_right, OUTPUT);
  46.     pinMode(reversePin_right, OUTPUT);
  47.     motor_stop(motorPin_left, reversePin_left);
  48.     motor_stop(motorPin_right, reversePin_right);
  49.     Serial.begin(115200);
  50.     // turn_left();
  51.     // turn_right();
  52.     move_forward();
  53. }
  54.  
  55. void loop(){
  56.     // int range_left = analogRead(leftSide_sensor);
  57.     // int range_right = analogRead(rightSide_sensor);
  58.     // Serial.println("----------------");
  59.     // Serial.print(range_left);
  60.     // Serial.print(" ");
  61.     // Serial.print(range_right);
  62.     // Serial.println();
  63.  
  64.     // onLine(leftFront_sensor)?Serial.print("T"):Serial.print("F");
  65.     // Serial.print(" ");
  66.     // onLine(rightFront_sensor)?Serial.print("T"):Serial.print("F");
  67.     // Serial.println();
  68.  
  69.     // onLine(leftSide_sensor)?Serial.print("T"):Serial.print("F");
  70.     // Serial.print(" ");
  71.     // onLine(rightSide_sensor)?Serial.print("T"):Serial.print("F");
  72.     // Serial.println();
  73.     // delay(500);
  74. }
  75.  
  76. // Функции для работы с моторами
  77. void motor_forward(int motorPin, int reversePin, int speed){
  78.   digitalWrite(reversePin, HIGH);
  79.   analogWrite(motorPin, 255-speed);
  80. }
  81.  
  82. void motor_back(int motorPin, int reversePin, int speed){
  83.   digitalWrite(reversePin, LOW);
  84.   analogWrite(motorPin, speed);
  85. }
  86.  
  87. void motor_stop(int motorPin, int reversePin){
  88.   digitalWrite(reversePin, LOW);
  89.   analogWrite(motorPin, 0);
  90. }
  91.  
  92. // Функция для проверки на линии ли датчик
  93. bool onLine(int pin){
  94.     int sensorReading = analogRead(pin);
  95.     if(sensorReading < 700){
  96.         return false;
  97.     }else{
  98.         return true;
  99.     }
  100. }
  101.  
  102. // Функции для работы с автоматическим роботом
  103. void move_forward(){
  104.     motor_forward(motorPin_left, reversePin_left, 250);
  105.     motor_forward(motorPin_right, reversePin_right, 250);
  106.     // delay(500);
  107.     while(!onLine(leftSide_sensor) && !onLine(rightSide_sensor)){
  108.         Serial.println("SEKU");
  109.         motor_forward(motorPin_left, reversePin_left, 250);
  110.         motor_forward(motorPin_right, reversePin_right, 250);
  111.     }
  112.     delay(1000);
  113.     while(!onLine(leftSide_sensor) && !onLine(rightSide_sensor)){
  114.         Serial.println("SEKU");
  115.         motor_forward(motorPin_left, reversePin_left, 250);
  116.         motor_forward(motorPin_right, reversePin_right, 250);
  117.     }
  118.     motor_stop(motorPin_left, reversePin_left);
  119.     motor_stop(motorPin_right, reversePin_right);
  120. }
  121.  
  122. void turn_left(){
  123.     motor_back(motorPin_left, reversePin_left, 250);
  124.     motor_forward(motorPin_right, reversePin_right, 250);
  125.     int timing = 20;
  126.     delay(500);
  127.     while(!onLine(leftSide_sensor)){
  128.         motor_back(motorPin_left, reversePin_left, 250);
  129.         motor_forward(motorPin_right, reversePin_right, 250);
  130.     }
  131.     delay(timing);
  132.     while(!onLine(leftFront_sensor)){
  133.         motor_back(motorPin_left, reversePin_left, 250);
  134.         motor_forward(motorPin_right, reversePin_right, 250);
  135.     }
  136.     delay(timing);
  137.     while(!onLine(rightFront_sensor)){
  138.         motor_back(motorPin_left, reversePin_left, 250);
  139.         motor_forward(motorPin_right, reversePin_right, 250);
  140.     }
  141.     motor_stop(motorPin_left, reversePin_left);
  142.     motor_stop(motorPin_right, reversePin_right);
  143. }
  144.  
  145. void turn_right(){
  146.     motor_forward(motorPin_left, reversePin_left, 250);
  147.     motor_back(motorPin_right, reversePin_right, 250);
  148.     int timing = 20;
  149.     delay(500);
  150.     while(!onLine(rightSide_sensor)){
  151.         motor_forward(motorPin_left, reversePin_left, 250);
  152.         motor_back(motorPin_right, reversePin_right, 250);
  153.     }
  154.     delay(timing);
  155.     while(!onLine(rightFront_sensor)){
  156.         motor_forward(motorPin_left, reversePin_left, 250);
  157.         motor_back(motorPin_right, reversePin_right, 250);
  158.     }
  159.     delay(timing);
  160.     while(!onLine(leftFront_sensor)){
  161.         motor_forward(motorPin_left, reversePin_left, 250);
  162.         motor_back(motorPin_right, reversePin_right, 250);
  163.     }
  164.     motor_stop(motorPin_left, reversePin_left);
  165.     motor_stop(motorPin_right, reversePin_right);
  166. }
  167.  
  168. void throw_balls(int balls){
  169.     //
  170.     //
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement