I can not work with the same codes that I am used with Arduino UNO engines to control my robot.
When connecting the motor controller board (AF_DCMotor) with Arduino Mega 2560 has left me to run the same codes as used with the Arduino UNO board.
The upload to the Mega board smoothly, but then do not respond, do nothing, neither with a simple code with direct commands for it move directly the motors nor by sending commands Serial Port with a code a little bit more complex.
There are only two things that I have changed when moving from one plate to the other and is to 'Board' the board that touches ie Arduino Mega 2560 and I had to also change the port COM3 (I used Arduino UNO) to COM6 COM3 because with COM3 not even load the program.
You can help me?
Here's the code:
#include <AFMotor.h>
int input;
void setup(){
Serial.begin(9600);
}
void loop() {
AF_DCMotor motor4(4, MOTOR34_64KHZ); // define el motor en el puerto 4 a 64KHz
AF_DCMotor motor3(3, MOTOR34_64KHZ); // define el motor en el puerto 3 a 64KHz
AF_DCMotor motor2(2, MOTOR12_64KHZ); // define el motor en el puerto 2 a 64KHz
AF_DCMotor motor1(1, MOTOR12_64KHZ); // define el motor en el puerto 1 a 64KHz
input=Serial.read();
int pindigital = 2;
pinMode(pindigital, OUTPUT);
if (input=='1'){
digitalWrite(pindigital, LOW); // activaremos el relé en posición motor 1
delay(500);
motor1.setSpeed(255); // define la velocidad del motor (max.255)
motor1.run(FORWARD);
delay(500); // hace un forward durante medio segundo
}
if (input=='2'){
motor2.setSpeed(255); // define la velocidad del motor (max.255)
motor2.run(FORWARD);
delay(500); // hace un forward durante medio segundo
}
if (input=='3'){
motor3.setSpeed(255); // define la velocidad del motor (max.255)
motor3.run(FORWARD);
delay(500); // hace un forward durante medio segundo
}
if (input=='4'){
motor4.setSpeed(255); // define la velocidad del motor (max.255)
motor4.run(FORWARD);
delay(500); // hace un forward durante medio segundo
}
if (input=='5'){
digitalWrite(pindigital, HIGH); // activaremos el relé en posición motor 5
delay(500);
motor1.setSpeed(255); // define la velocidad del motor (max.255)
motor1.run(FORWARD);
delay(500); // hace un forward durante medio segundo
}
if (input=='6'){
digitalWrite(pindigital, LOW); // activaremos el relé en posición motor 1
delay(500);
motor1.setSpeed(255); // define la velocidad del motor (max.255)
motor1.run(BACKWARD);
delay(500); // hace un backward durante medio segundo
}
if (input=='7'){
motor2.setSpeed(255); // define la velocidad del motor (max.255)
motor2.run(BACKWARD);
delay(500); // hace un backward durante medio segundo
}
if (input=='8'){
motor3.setSpeed(255); // define la velocidad del motor (max.255)
motor3.run(BACKWARD);
delay(500); // hace un backward durante medio segundo
}
if (input=='9'){
motor4.setSpeed(255); // define la velocidad del motor (max.255)
motor4.run(BACKWARD);
delay(500); // hace un backward durante medio segundo
}
if (input=='0'){
digitalWrite(pindigital, HIGH); // activaremos el relé en posición motor 5
delay(500);
motor1.setSpeed(255); // define la velocidad del motor (max.255)
motor1.run(BACKWARD);
delay(500); // hace un forward durante medio segundo
}
}