Данный проект — это пример изготовления простого Arduino-робота.
Для повторения данной конструкции понадобятся:
* Arduino — 1,
* мотор-редуктор — 1,
* сервомашинка -1,
* мотор-шилд — 1,
* УЗ-датчик — 3,
* аккумулятор, колёса и другая мелочёвка.
Схема соединения компонентов робота:
Программа робота (Arduino-cкетч):
//Libraries #include <AFMotor.h> #include <Servo.h> #include <NewPing.h> //Setup Servo servo1; NewPing sonar_center(16,17,250); NewPing sonar_right(14,15,250); NewPing sonar_left(18,19,250); AF_DCMotor drive(2,MOTOR12_1KHZ); //Created DC main drive motor 1, 8 kHz /* You can set the speed of the motor using setSpeed(speed) where the speed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want. To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE. Of course, the Arduino doesn't actually know if the motor is 'forward' or 'backward', so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield. Be friendly to the motor: stop it before reversing by using: motor(1, RELEASE, 0); delay(500);*/ //Below we are creating unsigned integer variables which we will use later on in the code. They are unsigned as they will only have postive values unsigned int distance_front; unsigned int distance_left; unsigned int distance_right; void setup() { Serial.begin(9600); // set up Serial library at 38400 bps servo1.attach(10); } void loop() //This block repeats itself while the Arduino is turned on { delay(500); servo1.write(86); //Rotate the servo to face the front distance_front = sonar_center.ping_cm (); //Send a ping, returns the distance in centimeters or 0 (zero) if no ping echo within set distance limit if(distance_front > 40 || distance_front == 0) //If there is nothing infront of the robot within 40cm or the distance value is 0 (which for the newping libary means no ping was returned) then... { drive.setSpeed(255); drive.run(FORWARD); //go forward } else { drive.run(RELEASE); //break servo1.write(30); //turn front wheel delay(250); //slight delay drive.setSpeed(250); drive.run(BACKWARD); //reverse motor... delay(3000); //for 3 second drive.run(RELEASE); //break delay(500); //time for breaking servo1.write(86); //turn front wheel } distance_left = sonar_left.ping_cm (); //Send a ping, returns the distance in centimeters or 0 (zero) if no ping echo within set distance limit distance_right = sonar_right.ping_cm (); //Send a ping, returns the distance in centimeters or 0 (zero) if no ping echo within set distance limit if(distance_left != 0 && distance_left < 40) //If there is nothing infront of the robot within 40cm or the distance value is 0 (which for the newping libary means no ping was returned) then... { servo1.write(140); //turn front wheel } else if (distance_right != 0 && distance_right < 40) //If there is nothing infront of the robot within 40cm or the distance value is 0 (which for the newping libary means no ping was returned) then... { servo1.write(30); //turn front wheel }else { servo1.write(86); } }
Ссылки
R3: Rolling Red Robot
По теме
Ардуино что это и зачем?
Arduino, термины, начало работы
Разновидности плат Arduino, а также про клоны, оригиналы и совместимость
КМБ для начинающих ардуинщиков
Состав стартера (точка входа для начинающих ардуинщиков)