Pins on Motor Shield

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
janiemiec20
 
Posts: 8
Joined: Sat Apr 13, 2013 6:33 am

Pins on Motor Shield

Post by janiemiec20 »

Hello,
I have the LADYADA Motor Shield (www.ladyada.net/make/shield) and I wanted to use it to control two motors (Connected to HUB 1 and 2).
According to the website I should use the following code to control the motors:

Code: Select all

// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>

AF_DCMotor motor(4);

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

  // turn on motor
  motor.setSpeed(200);
 
  motor.run(RELEASE);
}

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

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

  Serial.print("tech");
  motor.run(RELEASE);
  delay(1000);
}
I do not want to use the library to control the motors because it causes problems with my code below. I need to use this code for the robot:

Code: Select all

const int RightSensor = 4;
const int LeftSensor = 3;

int SensorLeft;
int SensorRight;
int SensorDifference;


void setup() {

pinMode(4, OUTPUT); //left motor
pinMode(7, OUTPUT); //right motor
pinMode(LeftSensor, INPUT);
pinMode(RightSensor, INPUT);
Serial.begin(7600);
Serial.println(" \nBeginning Light Seeking Behavior");
}


void loop() {
SensorLeft = 1023 - analogRead(LeftSensor);
delay(1);
SensorRight = 1023 - analogRead(RightSensor);
delay(1);
SensorDifference = abs(SensorLeft - SensorRight);

Serial.print("Left Sensor = ");
Serial.print(SensorLeft);
Serial.print("\t");
Serial.print("Right Sensor = ");
Serial.print(SensorRight);
Serial.print("\t");

if (SensorLeft > SensorRight && SensorDifference > 75) {
Serial.println("Left");
digitalWrite(4, HIGH); 
delay(250);
digitalWrite(4, LOW);
delay(100);

}

if (SensorLeft < SensorRight && SensorDifference > 75) {
Serial.println("Right");
digitalWrite(7, HIGH);
delay(250);
digitalWrite(7, LOW);
delay(100);
}

else if (SensorDifference < 75) {
Serial.println("Forward");
digitalWrite(4, HIGH);
digitalWrite(7, HIGH);
delay(500);
digitalWrite(4, LOW);
digitalWrite(7, LOW);
delay(250);

}
Serial.print("\n");
}

Here is the problem:
I do not know which pins control which motor, I found this : https://docs.google.com/document/d/1OdGS0QHKCdbrbAs_x62gmfSx_UdUPFPgZjwZnVDVHDg/preview however I do not understand a word of what it says.

What I want to know is which pin should I set to high, to run what motor forward and backwards? I will need this to make the code for the robot work.
(I have tried using the library that comes with the board but when I add the values as outputs nothing happens).

Please help  :smiley-sad-blue:

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

Re: Pins on Motor Shield

Post by adafruit_support_bill »

Have you read the FAQ? http://learn.adafruit.com/adafruit-motor-shield/faq
which pin should I set to high, to run what motor forward and backwards?
You can't. As explained in the FAQ and in the document you linked, the H-bridges are controlled by the latch. You must shift the control bits into the latch to control the motors. There is no single pin you can enable to turn on a motor.

I see that your existing code uses pins 4 and 7. You will have to change that to use with the shield because pins 4, 7, 8 and 12 are used to control the latch.

janiemiec20
 
Posts: 8
Joined: Sat Apr 13, 2013 6:33 am

Re: Pins on Motor Shield

Post by janiemiec20 »

Thanks for your reply,
You can't. As explained in the FAQ and in the document you linked, the H-bridges are controlled by the latch.
Then I have a problem, since I can't control the motors with the pins then how can I implement the Motor Shield code into the robot sketch? How can I replace:

Code: Select all

pinMode(4, OUTPUT); //left motor
pinMode(7, OUTPUT); //right motor
with the controls that are used by the shield?

Is this it?

Code: Select all

Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control)
Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control)
Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control)
Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control)

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

Re: Pins on Motor Shield

Post by adafruit_support_bill »

Declare them like this:

Code: Select all

#include <AFMotor.h>

AF_DCMotor leftMotor(1);
AF_DCMotor rightMotor(2);
ANd to turn a motor on, use:

Code: Select all

   leftMotor.setSpeed(200);
   leftMotor.run(FORWARD);

janiemiec20
 
Posts: 8
Joined: Sat Apr 13, 2013 6:33 am

Re: Pins on Motor Shield

Post by janiemiec20 »

Great thank you! I will try it.

janiemiec20
 
Posts: 8
Joined: Sat Apr 13, 2013 6:33 am

Re: Pins on Motor Shield

Post by janiemiec20 »

So I have done as you said:

Code: Select all

const int RightSensor = 4;
const int LeftSensor = 3;

int SensorLeft;
int SensorRight;
int SensorDifference;

#include <AFMotor.h>

AF_DCMotor leftMotor(1);
AF_DCMotor rightMotor(2);

void setup() {


pinMode(LeftSensor, INPUT);
pinMode(RightSensor, INPUT);
Serial.begin(9600);
Serial.println(" \nBeginning Light Seeking Behavior");
}


void loop() {
SensorLeft = 1023 - analogRead(LeftSensor);
delay(1);
SensorRight = 1023 - analogRead(RightSensor);
delay(1);
SensorDifference = abs(SensorLeft - SensorRight);

Serial.print("Left Sensor = ");
Serial.print(SensorLeft);
Serial.print("\t");
Serial.print("Right Sensor = ");
Serial.print(SensorRight);
Serial.print("\t");

if (SensorLeft > SensorRight && SensorDifference > 75) {
Serial.println("Left");
rightMotor.setSpeed(200);
rightMotor.run(FORWARD);
delay(250);
rightMotor.run(RELEASE);
delay(100);

}

if (SensorLeft < SensorRight && SensorDifference > 75) {
Serial.println("Right");
leftMotor.setSpeed(200);
leftMotor.run(FORWARD);
delay(250);
leftMotor.run(RELEASE);
delay(100);
}

else if (SensorDifference < 75) {
Serial.println("Forward");
leftMotor.setSpeed(200);
rightMotor.setSpeed(200);
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);
delay(500);
leftMotor.run(RELEASE);
rightMotor.run(RELEASE);

}
Serial.print("\n");
}
However the robot does not move. No motors turn on.

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

Re: Pins on Motor Shield

Post by adafruit_support_bill »

Post a photo showing how you have everything connected.

janiemiec20
 
Posts: 8
Joined: Sat Apr 13, 2013 6:33 am

Re: Pins on Motor Shield

Post by janiemiec20 »

That's how I connected the LDR:
Image apart from the fact that the first LDR is connected to A4 and second is connected to A3. The LDR seems to work because when I run this test program:

Code: Select all

//LDR input test console

void setup() {
Serial.begin(9600);
}
void loop() {
int sensor1Value = analogRead(A4);
Serial.print("Analog Pin 4 Reading = ");
Serial.println(sensor1Value);
delay(1000);
int sensor2Value = analogRead(A3);
Serial.print("Analog Pin 3 Reading = ");
Serial.println(sensor2Value);
delay(1000);
}
I can see the values change as I shine the light. The robot syntax (which I got from instructables and adapted it) posts to serial print and when the robot is on I can see that I recognises the light. I have connected the motors in the following way:
Image
Besides the code should run the motors even if there is no input.
I am really getting confused with the whole thing right now.

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

Re: Pins on Motor Shield

Post by adafruit_support_bill »

That board doesn't look like an Adafruit MotorShield.

janiemiec20
 
Posts: 8
Joined: Sat Apr 13, 2013 6:33 am

Re: Pins on Motor Shield

Post by janiemiec20 »

Well it says it's from LADYADA and has the website from which I found this forum. http://www.ladyada.net/make/mshield and if I click on the forum section I get directed here. I mean it works just as described.

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

Re: Pins on Motor Shield

Post by adafruit_support_bill »

Where did you purchase it?

janiemiec20
 
Posts: 8
Joined: Sat Apr 13, 2013 6:33 am

Re: Pins on Motor Shield

Post by janiemiec20 »

eBay

Regardless do you think this is the code error? Perhaps could you help me with fixing the LDR detection part?

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

Re: Pins on Motor Shield

Post by adafruit_support_bill »

It is a copy of an older design, assembled with some non-standard parts. I'm sorry but we can't support this shield.

janiemiec20
 
Posts: 8
Joined: Sat Apr 13, 2013 6:33 am

Re: Pins on Motor Shield

Post by janiemiec20 »

I found the solution, the pins within the code. It works. Thanks for your support.

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

Return to “Arduino Shields from Adafruit”