- Код: Выделить всё
#include <AFMotor.h> //Link library for working with shield
// Plug motors to the terminal blocks M1, M2, M3, M4
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
unsigned long duration;
void setup(){
// Set the maximum speed of the motor (similar work PWM)
motor1.setSpeed(255);
motor1.run(RELEASE);
motor2.setSpeed(255);
motor2.run(RELEASE);
motor3.setSpeed(255);
motor3.run(RELEASE);
motor4.setSpeed(255);
motor4.run(RELEASE);
pinMode(12, INPUT);
pinMode(13, INPUT);
}
void loop(){
if (duration = pulseIn(12, HIGH))
{
//moving forward
motor1.run(FORWARD); // Ask moving forward
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
motor1.setSpeed(100); // Set the speed
motor2.setSpeed(100);
motor3.setSpeed(100);
motor4.setSpeed(100);
}
else
{
// move left
motor1.run(BACKWARD); // Ask moving forward 2 motors
motor4.run(BACKWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor1.setSpeed(100); // Set the speed
motor4.setSpeed(100);
motor2.setSpeed(100);
motor3.setSpeed(100);
}
if (duration = pulseIn(13, HIGH))
{
//moving forward
motor1.run(FORWARD); // Ask moving forward
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
motor1.setSpeed(100); // Set the speed
motor2.setSpeed(100);
motor3.setSpeed(100);
motor4.setSpeed(100);
}
else
{
// move right
motor1.run(FORWARD); // Ask moving forward 2 motors
motor4.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor1.setSpeed(100); // Set the speed
motor4.setSpeed(100);
motor2.setSpeed(100);
motor3.setSpeed(100);
}
}