Dc motor control

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Lakesed
 
Posts: 58
Joined: Tue Mar 11, 2014 7:30 pm

Dc motor control

Post by Lakesed »

I am trying to control 2 dc motors on a robot. I am using the MotorShield V2. I can make the motors run normally with inline code. I am trying to create a test Function to start the motors since I will be changing speed, direction and stopping the motors from many places in the sketch. When I compile the sketch I get "myMotor was not declared in this scope. How can I declare it or get around the problem? The name of the function is RTmotoFWD. It is a simple test to start 1 motor. Here is the sketch:

Code: Select all

/* 
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_PWMServoDriver.h"
void RTmotoFwd() {    //Right Motor
     myMotor->run(FORWARD);
     (i=200); //i<255; i++) 
      myMotor->setSpeed(i);  
      delay(10);
    }
// 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
  
void loop() {
    
  Serial.print("Right  Motor");
  RTmotoFwd();
 

  myMotor->run(RELEASE);
  delay(1000);
}
Last edited by Franklin97355 on Wed Jul 23, 2014 8:51 pm, edited 1 time in total.
Reason: Added [code] tags.

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: Dc motor control

Post by Franklin97355 »

Couple of things stand out. Your function calls myMotor before it is declared. Move the declarations above your function.
Variables need to be declared in c++ so you need to add int i; at the top of your function. You are also missing a closing brace at the end of setup().

User avatar
Lakesed
 
Posts: 58
Joined: Tue Mar 11, 2014 7:30 pm

Re: Dc motor control

Post by Lakesed »

That worked. There were a couple of other things that had to be moved also. Thank you for your quick response.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”