Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int motorPin_left = 4;
- const int reversePin_left= 3;
- const int motorPin_right = 5;
- const int reversePin_right = 6;
- const int leftFront_sensor = A0;
- const int rightFront_sensor = A1;
- const int leftSide_sensor = A2;
- const int rightSide_sensor = A3;
- const int sensorMin = 0; // sensor minimum
- const int sensorMax = 1024; // sensor maximum
- const int reverseTiming = 3400;
- enum direction {
- NORTH,
- EAST,
- SOUTH,
- WEST
- };
- // Хранят координаты робота
- int coord[2] = {0, 0};
- int coord_direction = SOUTH;
- int i;
- // Маршрут робота
- const int route[][3] = {
- {0, 1},
- {1, 1},
- {1, 0},
- {2, 0},
- {2, 1},
- {3, 1},
- {3, 0},
- {4, 0},
- {4, 1},
- {5, 1},
- {5, 2},
- {0, 2},
- {0, 0}
- };
- void setup(){
- pinMode(motorPin_left, OUTPUT);
- pinMode(reversePin_left, OUTPUT);
- pinMode(motorPin_right, OUTPUT);
- pinMode(reversePin_right, OUTPUT);
- motor_stop(motorPin_left, reversePin_left);
- motor_stop(motorPin_right, reversePin_right);
- Serial.begin(115200);
- // turn_left();
- // turn_right();
- move_forward();
- }
- void loop(){
- // int range_left = analogRead(leftSide_sensor);
- // int range_right = analogRead(rightSide_sensor);
- // Serial.println("----------------");
- // Serial.print(range_left);
- // Serial.print(" ");
- // Serial.print(range_right);
- // Serial.println();
- // onLine(leftFront_sensor)?Serial.print("T"):Serial.print("F");
- // Serial.print(" ");
- // onLine(rightFront_sensor)?Serial.print("T"):Serial.print("F");
- // Serial.println();
- // onLine(leftSide_sensor)?Serial.print("T"):Serial.print("F");
- // Serial.print(" ");
- // onLine(rightSide_sensor)?Serial.print("T"):Serial.print("F");
- // Serial.println();
- // delay(500);
- }
- // Функции для работы с моторами
- void motor_forward(int motorPin, int reversePin, int speed){
- digitalWrite(reversePin, HIGH);
- analogWrite(motorPin, 255-speed);
- }
- void motor_back(int motorPin, int reversePin, int speed){
- digitalWrite(reversePin, LOW);
- analogWrite(motorPin, speed);
- }
- void motor_stop(int motorPin, int reversePin){
- digitalWrite(reversePin, LOW);
- analogWrite(motorPin, 0);
- }
- // Функция для проверки на линии ли датчик
- bool onLine(int pin){
- int sensorReading = analogRead(pin);
- if(sensorReading < 700){
- return false;
- }else{
- return true;
- }
- }
- // Функции для работы с автоматическим роботом
- void move_forward(){
- motor_forward(motorPin_left, reversePin_left, 250);
- motor_forward(motorPin_right, reversePin_right, 250);
- // delay(500);
- while(!onLine(leftSide_sensor) && !onLine(rightSide_sensor)){
- Serial.println("SEKU");
- motor_forward(motorPin_left, reversePin_left, 250);
- motor_forward(motorPin_right, reversePin_right, 250);
- }
- delay(1000);
- while(!onLine(leftSide_sensor) && !onLine(rightSide_sensor)){
- Serial.println("SEKU");
- motor_forward(motorPin_left, reversePin_left, 250);
- motor_forward(motorPin_right, reversePin_right, 250);
- }
- motor_stop(motorPin_left, reversePin_left);
- motor_stop(motorPin_right, reversePin_right);
- }
- void turn_left(){
- motor_back(motorPin_left, reversePin_left, 250);
- motor_forward(motorPin_right, reversePin_right, 250);
- int timing = 20;
- delay(500);
- while(!onLine(leftSide_sensor)){
- motor_back(motorPin_left, reversePin_left, 250);
- motor_forward(motorPin_right, reversePin_right, 250);
- }
- delay(timing);
- while(!onLine(leftFront_sensor)){
- motor_back(motorPin_left, reversePin_left, 250);
- motor_forward(motorPin_right, reversePin_right, 250);
- }
- delay(timing);
- while(!onLine(rightFront_sensor)){
- motor_back(motorPin_left, reversePin_left, 250);
- motor_forward(motorPin_right, reversePin_right, 250);
- }
- motor_stop(motorPin_left, reversePin_left);
- motor_stop(motorPin_right, reversePin_right);
- }
- void turn_right(){
- motor_forward(motorPin_left, reversePin_left, 250);
- motor_back(motorPin_right, reversePin_right, 250);
- int timing = 20;
- delay(500);
- while(!onLine(rightSide_sensor)){
- motor_forward(motorPin_left, reversePin_left, 250);
- motor_back(motorPin_right, reversePin_right, 250);
- }
- delay(timing);
- while(!onLine(rightFront_sensor)){
- motor_forward(motorPin_left, reversePin_left, 250);
- motor_back(motorPin_right, reversePin_right, 250);
- }
- delay(timing);
- while(!onLine(leftFront_sensor)){
- motor_forward(motorPin_left, reversePin_left, 250);
- motor_back(motorPin_right, reversePin_right, 250);
- }
- motor_stop(motorPin_left, reversePin_left);
- motor_stop(motorPin_right, reversePin_right);
- }
- void throw_balls(int balls){
- //
- //
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement