Motor/stepper/Servo Shield V2

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
TLeske
 
Posts: 56
Joined: Sat Jan 11, 2014 8:30 am

Motor/stepper/Servo Shield V2

Post by TLeske »

I am a beginner at Arduino. I have the Adafruit motor/stepper/servo shield (V2) running on a Mega ADK board. I am trying to control 2 DC motors based on ultrasonic distance measurement. I have used the Motor Test example as a basis for the code (see below). My problem is this: The control of the motors works fine when the board is connected to the USB port, but not if it is unplugged. I suspect that it is because the code is writing the distance to the serial port, and not used to control the motors. Can anyone help with this? THANKS

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"

  
  const int TrigPin = A2;
  const int EchoPin = A3;
  float cm;


// 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);
Adafruit_DCMotor *myMotor2=AFMS.getMotor(2);
// 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!");


  pinMode(TrigPin, OUTPUT);
  pinMode(EchoPin, INPUT);




  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);
  myMotor2->setSpeed(150);
  myMotor->run(FORWARD);
  myMotor2->run(BACKWARD);
  // turn on motor
  myMotor->run(RELEASE);
  myMotor2->run(RELEASE);
}

void loop() {
  uint8_t i;
  
  
    digitalWrite(TrigPin, LOW); //Low high and low level take a short time to TrigPin pulse
    delayMicroseconds(2);
    digitalWrite(TrigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(TrigPin, LOW);

    cm = pulseIn(EchoPin, HIGH) / 58.0; //Echo time conversion into cm
    cm = (int(cm * 100.0)) / 100.0; //Keep two decimal places
    
    Serial.print(cm);
    Serial.print("cm");
    Serial.println();
  
 
  
  Serial.print("tick");


if (cm>10)

//Serial.print(cm);

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

else

Serial.print(cm);

{

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

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


}
Last edited by adafruit_support_bill on Tue Jan 28, 2014 6:58 am, edited 1 time in total.
Reason: Please use the 'code' button when submitting code - click 'code' and paste your code between the tags.

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor/stepper/Servo Shield V2

Post by adafruit_support_bill »

You should be able to write to the serial port even if there is nothing connected. What are you using to power the system when not connected to USB? Post a photo showing all your wiring.

TLeske
 
Posts: 56
Joined: Sat Jan 11, 2014 8:30 am

Re: Motor/stepper/Servo Shield V2

Post by TLeske »

Thanks. I am powering the system using the 9V DC jack. Photo attached.
Attachments
IMG_0694.jpg
IMG_0694.jpg (67.69 KiB) Viewed 462 times

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor/stepper/Servo Shield V2

Post by adafruit_support_bill »

From what I can see the connections look ok. Is the VIN power jumper installed on the shield?
What do the motors do when running from battery power?

Looking at your code, I think there may be a bug here:

Code: Select all

else

Serial.print(cm);

{

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

}
The scope of the 'else' clause is just the "Serial.print(cm);" If you want to only reverse when closer than 10cm, you need to move that line inside the brackets { }.

TLeske
 
Posts: 56
Joined: Sat Jan 11, 2014 8:30 am

Re: Motor/stepper/Servo Shield V2

Post by TLeske »

Thanks. Yes, the jumper is installed. The motors run fine when only running on battery power, but they do not follow the code i.e. not influenced by the distance measured.

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor/stepper/Servo Shield V2

Post by adafruit_support_bill »

but they do not follow the code i.e. not influenced by the distance measured.
As I mentioned in the previous post, the reverse-turn part of your code is not part of the scope of the 'else' clause, so it will execute unconditionally.

TLeske
 
Posts: 56
Joined: Sat Jan 11, 2014 8:30 am

Re: Motor/stepper/Servo Shield V2

Post by TLeske »

Thank you. I have changed the code to the following (by including the serial.print commands within the brackets of the "if" and "else" brackets: It is still behaving the same way (even if I include the serial.print command in the "else" brackets only). Sorry about having to ask these basic questions...I suppose one has to start somewhere when learning this stuff :)


Code: Select all

if (cm>10)

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

else


{

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

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


}
Last edited by adafruit_support_bill on Tue Jan 28, 2014 8:37 am, edited 2 times in total.
Reason: Please use the 'code' button when submitting code - click 'code' and paste your code between the tags.

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor/stepper/Servo Shield V2

Post by adafruit_support_bill »

Please use the "Code" button when submitting code.

Without serial diagnostic feedback, it is hard to know what the code is doing or if your sensor is functioning. One simple diagnostic aid is to add some leds to some digital pins and flash them in various parts of your program. That will give you a visual indication of what the code is doing.

TLeske
 
Posts: 56
Joined: Sat Jan 11, 2014 8:30 am

Re: Motor/stepper/Servo Shield V2

Post by TLeske »

Thanks. I will use the "code" button in future. This is what the serial is showing:

Perhaps you can edit the bit of code that I sent, and post it?

Estiale: 3
279.06cm
tick279.06Estimated pre-scale: 2.81
Final pre-scale: 3
277.41cm
tick277.41Estimated pre-scale: 2.81
Final pre-scale: 3
277.00cm
tick277.00Estimated pre-scale: 2.81
Final pre-scale: 3
278.25cm
tick278.25Estimated pre-scale: 2.81
Final pre-scale: 3
-232.89cm
tick-232.89-232.89Estimated pre-scale: 2.81
Final pre-scale: 3
6.20cm
tick6.206.20tech7.12cm
tick7.127.12tech6.65cm
tick6.656.65Estimated pre-scale: 2.81
Final pre-scale: 3
6.70cm
tick6.706.70tech281.08cm
tick281.08tech280.27cm
tick280.27tech279.77cm
tick279.77

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

Return to “Other Arduino products from Adafruit”