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
/*
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.
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.
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 { }.
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.
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.
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
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.
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.