0% found this document useful (0 votes)
7 views3 pages

Codigo XD

This document contains an Arduino sketch for controlling four DC motors using the AFMotor library. It defines motor control functions for moving forward, backward, left, right, and stopping based on serial commands. The setup initializes serial communication, and the loop checks for commands to execute the corresponding motor movements.

Uploaded by

micafebrero427
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Codigo XD

This document contains an Arduino sketch for controlling four DC motors using the AFMotor library. It defines motor control functions for moving forward, backward, left, right, and stopping based on serial commands. The setup initializes serial communication, and the loop checks for commands to execute the corresponding motor movements.

Uploaded by

micafebrero427
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <AFMotor.

h>
//initial motors pin
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);

char command;
void setup()
{
Serial.begin(9600); //velocidad en baudios

void loop(){
if(Serial.available() > 0){
command = Serial.read();
Stop();

switch(command){
case 'F':
forward();
break;
case 'B':
back();
break;
case 'G':
izquierda();
break;
case 'I':
derecha();
break;
case 'L':
left();
break;
case 'R':
right();
break;
}
}
}

void forward()
{
motor1.setSpeed(125); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor clockwise
motor2.setSpeed(125); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor clockwise
motor3.setSpeed(125); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(125); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise

void back()
{
motor1.setSpeed(125); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor anti-clockwise
motor2.setSpeed(125); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor anti-clockwise
motor3.setSpeed(125); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
motor4.setSpeed(125); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor anti-clockwise

void left()
{
motor1.setSpeed(155); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor clockwise
motor2.setSpeed(155); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(155); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(155); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor clockwise

void right()
{
motor1.setSpeed(155); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(155); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor clockwise
motor3.setSpeed(155); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor clockwise
motor4.setSpeed(155); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise

}
void derecha()
{
motor1.setSpeed(125); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor clockwise
motor2.setSpeed(125); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(125); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor clockwise
motor4.setSpeed(125); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void izquierda()
{
motor1.setSpeed(125); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(125); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor clockwise
motor3.setSpeed(125); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(125); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor clockwise

}
void Stop()
{
motor1.setSpeed(0); //Define minimum velocity
motor1.run(RELEASE); //stop the motor when release the button
motor2.setSpeed(0); //Define minimum velocity
motor2.run(RELEASE); //rotate the motor clockwise
motor3.setSpeed(0); //Define minimum velocity
motor3.run(RELEASE); //stop the motor when release the button
motor4.setSpeed(0); //Define minimum velocity
motor4.run(RELEASE); //rotate the motor clockwise

You might also like