0

Motor won't run FORWARD, but follows the same code BACKWARD
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Motor won't run FORWARD, but follows the same code BACKWARD

by Aub on Sun Dec 10, 2017 9:38 pm

So I'm using the motor shield V2 and I was able to run the sample DC motor code without problem:
Code: Select all | TOGGLE FULL SIZE
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2
---->   http://www.adafruit.com/products/1438
*/

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

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
 
  // Set the speed to start, from 0 (off) to 255 (max speed)
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);
  // turn on motor
  myMotor->run(RELEASE);
}

void loop() {
  uint8_t i;
 
  Serial.print("tick");

  myMotor->run(FORWARD);
  for (i=0; i<255; i++) {
    myMotor->setSpeed(i); 
    delay(10);
  }
  for (i=255; i!=0; i--) {
    myMotor->setSpeed(i); 
    delay(10);
  }
 
  Serial.print("tock");

  myMotor->run(BACKWARD);
  for (i=0; i<255; i++) {
    myMotor->setSpeed(i); 
    delay(10);
  }
  for (i=255; i!=0; i--) {
    myMotor->setSpeed(i); 
    delay(10);
  }

  Serial.print("tech");
  myMotor->run(RELEASE);
  delay(1000);
}


So I gut this code to run,
Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"


Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *myMotor = AFMS.getMotor(1);


void setup() {
  Serial.begin(9600);           


  AFMS.begin();  // create with the default frequency 1.6KHz
 

}

void loop() {
  uint8_t i;
 
 

 
 

  myMotor->run(BACKWARD);
  for (i=50; i<51; i++) {
    myMotor->setSpeed(i); 
    delay(3000);
  }
  for (i=51; i!=50; i--) {
    myMotor->setSpeed(i); 
    delay(3000);
  }

 
  myMotor->run(RELEASE);
  delay(1000);
}

And I get this to run, but when I switch the word "BACKWARD" with "FORWARD" the motor won't run. The code compiles, but it won't run, and I have no idea why.

Aub
 
Posts: 6
Joined: Sun Dec 03, 2017 8:30 pm

Please be positive and constructive with your questions and comments.