Go Down

Topic: Trouble controlling 5V motors using Adafruit motor sheild (Read 352 times) previous topic - next topic

freddie070

So, I have been doing some research to test out coding for two 5V hobby motors through a dual h-bridge Adafruit shield (connected to my Arduino Uno).  Eventually, I would like to control each one separately so that the vehicle can turn (one motor per side on a tracked vehicle).  While I have tried using different ways of coding from other posts, right now I am only getting one motor to run, but I'm not sure where I am going wrong.  Do any of you see where I am lost (besides everywhere  :P haha).

Code: [Select]

#include <AFMotor.h>

AF_DCMotor motor2(2);
AF_DCMotor motor4(4);

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

  // turn on motor
  motor2.setSpeed(200);
  motor4.setSpeed(200);

  motor2.run(RELEASE);
  motor4.run(RELEASE);
}

void loop() {
  uint8_t i;
 
  Serial.print("tick");
 
  motor2.run(FORWARD);
  for (i=0; i<255; i++) {
    motor2.setSpeed(i); 
    motor4.setSpeed(i); 
    delay(10);
}


  for (i=255; i!=0; i--) {
    motor2.setSpeed(i); 
    motor4.setSpeed(i); 
    delay(10);
}


  Serial.print("tock");

  motor2.run(BACKWARD);
  motor4.run(BACKWARD);
  for (i=0; i<255; i++) {
    motor2.setSpeed(i); 
    motor4.setSpeed(i); 
    delay(10);
}


  for (i=255; i!=0; i--) {
    motor2.setSpeed(i); 
    motor4.setSpeed(i); 
     delay(10);
}
 


  Serial.print("tech");
  motor2.run(RELEASE);
  motor4.run(RELEASE);
  delay(1000);
}
~~~Freddie

MarkT

You're not trying to power the motorshield from the Arduino 5V rail perchance?  Motors need a separate supply (and given that that shield uses the L293D it should be about 2V higher than you want to reach the motors).
[ I won't respond to messages, use the forum please ]

Go Up