Adafruit-Motor-Shield

Hi everyone,

I have this code, but the motors stop frequently, almost every 2 seconds. I'm not a programmer, and I just started using Arduino Uno a week ago for this project.
This is the code:

#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <AFMotor.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Crear objeto de Adafruit Motor Shield

AF_DCMotor motor1(1); // Definir motor 1 en el canal 1
AF_DCMotor motor2(2); // Definir motor 2 en el canal 2

void setup() {
  Serial.begin(9600); // Iniciar comunicación serial a 9600 bps
  
  AFMS.begin(); // Iniciar la Adafruit Motor Shield
  
  motor1.setSpeed(90); // Establecer velocidad del motor 1 al 90%
  motor1.run(FORWARD); // Hacer girar el motor 1 hacia adelante
  
  motor2.setSpeed(90); // Establecer velocidad del motor 2 al 90%
  motor2.run(BACKWARD); // Hacer girar el motor 2 hacia atrás
}

void loop() {
  // El loop puede permanecer vacío si no hay ninguna otra acción necesaria
}

Thank you for your help

Your motor function calls are inside setup() and only run one time before exiting. Move your motor function calls into loop() and the motor calls will be called continuously.

Thank you for your reply.
I did it again with this code, and I got the same result.

#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <AFMotor.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Crear objeto de Adafruit Motor Shield

AF_DCMotor motor1(1); // Definir motor 1 en el canal 1
AF_DCMotor motor2(2); // Definir motor 2 en el canal 2

void setup() {
  Serial.begin(9600); // Iniciar comunicación serial a 9600 bps
  
  AFMS.begin(); // Iniciar la Adafruit Motor Shield
  
  motor1.setSpeed(90); // Establecer velocidad del motor 1 al 90%
  motor2.setSpeed(90); // Establecer velocidad del motor 2 al 90%
}

void loop() {
  // Hacer girar el motor 1 hacia adelante
  motor1.run(FORWARD);
  
  // Hacer girar el motor 2 hacia atrás
  motor2.run(BACKWARD);
}

Any Idea?

Your code commands the motors to run forward then backward rapidly, but no time to move.

Try an example in the examples folder of the AFmotor library.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.