Need Help how to control 4 dc motors with 2 potentiometers a

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
MEKACI
 
Posts: 12
Joined: Tue Apr 15, 2014 10:20 am

Need Help how to control 4 dc motors with 2 potentiometers a

Post by MEKACI »

Hi friends,

I am a mechatronic engineering 3rd class student at my university. I have decided to use arduino new for my tank project ;but I am a newbie. I am having trouble " to control 4 dc motors with 2 potentiometers ( 1 potentiometer will be for forward and backward feature; other potentiometer will be for turn right and left feature that I wanted to code that way ) at Adafruit Motor Shield " while coding or I didn't able to figure out what is the problem;because even the compiler doesn't give errors, the thing I want doesn't happen. Thus, I wanted to get the advices and helps from you friends. I am really stuck and it is really frustrating ;and there is noone at my university to ask too. Please help me friends, thanks.

The shield that I am using :

Image
Image
Image

The code I have written at <AFMotor.h> ( V1 Adafruit motor shield library ) ( Firstly, I was trying here to control 1 dc motor with 1 potentiometer at adafruit motor shield for forward and backward feature. My 1 dc motor with 1 potentiometer try didn't work ;so I didn't add the " turn right and left feature " at this code example ;because I was stucked at the first place ) :

Code: Select all

#include <AFMotor.h>

int potentiometerPin = 7;  // analog pin used to connect the potentiometer
int potentiometerValue;    // variable to read the value from the analog pin 

 
AF_DCMotor left1stMotor(1);  // 1. motor  ( Left back motor )

// AF_DCMotor left2ndMotor(2);  // 2. motor   ( Left front motor )
// AF_DCMotor left3rdMotor(3);  // 3. motor  ( Right back motor )
// AF_DCMotor left4thMotor(4);  // 4. motor  ( Right front motor )


int i;

void setup() 
{ 
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  left1stMotor.setSpeed(200);
  left1stMotor.run(RELEASE);
  
  // left2ndMotor.setSpeed(200);
  // left2ndMotor.run(RELEASE);
  
  // left3rdMotor.setSpeed(200);
  // left3rdMotor.run(RELEASE);
  
  // left4thMotor.setSpeed(200);
  // left4thMotor.run(RELEASE);
  
} 
 
void loop() 
{ 
  potentiometerValue = analogRead(potentiometerPin);            // reads the value of the potentiometer (value between 0 and 1023) 
  potentiometerValue = map(potentiometerValue, 0, 1023, 0, 255);     // scale it to use it with the dc motor (value between 0 and 255) 
  potentiometerValue = potentiometerValue/4; // convert 0-1023 range to 0-255 range
 
  
      Serial.print("tick");
      left1stMotor.run(FORWARD);
      //left2ndMotor.run(FORWARD);
      //left3rdMotor.run(FORWARD);
      //left4thMotor.run(FORWARD);
      
  for (i=0; i<255; i++) {
    if(potentiometerValue <= 127){
      left1stMotor.setSpeed(i);  // left1stMotor.setSpeed(254-(potentiometerValue*2))
      delay(10);
      
      // left2ndMotor.setSpeed(i);
      // delay(10);
      // left3rdMotor.setSpeed(i);
      // delay(10);
      // left4thMotor.setSpeed(i);
      // delay(10);
    }
 }
    Serial.print("tock");
    left1stMotor.run(BACKWARD);
    // left2ndMotor.run(BACKWARD);
    // left3rdMotor.run(BACKWARD);
    // left4thMotor.run(BACKWARD);
    
  for (i=0; i<255; i++) {
    if(potentiometerValue >= 127){
      left1stMotor.setSpeed(i);
      delay(10);
      
      // left2ndMotor.setSpeed(i);
      // delay(10);
      // left3rdMotor.setSpeed(i);
      // delay(10);
      // left4thMotor.setSpeed(i);  
      // delay(10);
    }
    
  }
      
      Serial.print("tech");
      left1stMotor.run(RELEASE);
      delay(1000);
      
      // left2ndMotor.run(RELEASE);
      // delay(1000);
      // left3rdMotor.run(RELEASE);
      // delay(1000);
      // left4thMotor.run(RELEASE);
      // delay(1000);
 }




The code I have written at #include <Wire.h> ,#include <Adafruit_MotorShield.h> , #include "utility/Adafruit_PWMServoDriver.h" ( V2 Adafruit motor shield library )
 ( Firstly, I was trying here to control 1 dc motor with 1 potentiometer at adafruit motor shield for forward and backward feature. My 1 dc motor with 1 potentiometer try didn't work ;so I didn't add the " turn right and left feature " at this code example ;because I was stucked at the first place  ) :

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *left1stMotor = AFMS.getMotor(1);
Adafruit_DCMotor *left2ndMotor = AFMS.getMotor(2);
Adafruit_DCMotor *left3rdMotor = AFMS.getMotor(3);
Adafruit_DCMotor *left4thMotor = AFMS.getMotor(4);

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
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  left1stMotor->setSpeed(150);
  left1stMotor->run(FORWARD);
  // turn on motor
  left1stMotor->run(RELEASE);
  
  
  left2ndMotor->setSpeed(150);
  left2ndMotor->run(FORWARD);
  // turn on motor
  left2ndMotor->run(RELEASE);
  
  
  left3rdMotor->setSpeed(150);
  left3rdMotor->run(FORWARD);
  // turn on motor
  left3rdMotor->run(RELEASE);
  
  
  left4thMotor->setSpeed(150);
  left4thMotor->run(FORWARD);
  // turn on motor
  left4thMotor->run(RELEASE);
  
}

void loop(){

  potentiometerValue = analogRead(potentiometerPin);            // reads the value of the potentiometer (value between 0 and 1023) 
  potentiometerValue = map(potentiometerValue, 0, 1023, 0, 255);     // scale it to use it with the dc motor (value between 0 and 255) 
  potentiometerValue = potentiometerValue/4; // convert 0-1023 range to 0-255 range

uint8_t i;
  
  Serial.print("tick");

  left1stMotor->run(FORWARD);
  
  // left2ndMotor->run(FORWARD);
  // left3rdMotor->run(FORWARD);
  // left4thMotor->run(FORWARD);
  
  for (i=0; i<255; i++) {
    if(potentiometerValue <= 127){
      left1stMotor->setSpeed(i);                    
      delay(10);
      
      // left2ndMotor->setSpeed(i);
      // delay(10);
      // left3rdMotor->setSpeed(i);
      // delay(10);
      // left4thMotor->setSpeed(i);
      // delay(10);
    }
 }
  Serial.print("tock");
  left1stMotor->run(BACKWARD);
    // left2ndMotor->run(BACKWARD);
    // left3rdMotor->run(BACKWARD);
    // left4thMotor->run(BACKWARD);
    
  for (i=0; i<255; i++) {
    if(potentiometerValue >= 127){
      left1stMotor->setSpeed(i);
      delay(10);
      
      // left2ndMotor->setSpeed(i);
      // delay(10);
      // left3rdMotor->setSpeed(i);
      // delay(10);
      // left4thMotor->setSpeed(i);  
      // delay(10);
    }
    
  }
  
 

      left1stMotor->run(RELEASE);
      delay(1000);
      
      // left2ndMotor->run(RELEASE);
      // delay(1000);
      // left3rdMotor->run(RELEASE);
      // delay(1000);
      // left4thMotor->run(RELEASE);
      // delay(1000);
 }
I want to learn that : " Can I supply power to arduino from arduino's normal power socket and from motor shield at the same time ;or power supply from motor shield is enough to supply the motors and arduino at the same time ;or power supply from arduino is enough to supply the motors and arduino at the same time ?

For the turn right and left feature with a potentiometer we should do like this : " while 2 left side motors stop working and only right side motors move forward for clockwise turn right rotation to turn right ( ;or right side motors move backward for non-clockwise turn left rotation ) ; or 2 motors at the left side move forward and 2 motors at the right side move backward ? there are many options and logics. Which one is bettter ?

Sorry for asking too much questions, it is my first time doing a solo project myself with arduino at coding.

My dc motors work between 9V-12V. Please someone help me please at coding by giving some examples that I wanted to do at my project coding. I want to learn and understand where I stucked and how to solve this problem. I look forward your replies to me, friends. Thank you very much.
Last edited by adafruit_support_bill on Tue Apr 15, 2014 11:28 am, edited 1 time in total.
Reason: please use the code button when submitting code. press [Code], then paste your code between the [code] [/code] tags.

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

Re: Need Help how to control 4 dc motors with 2 potentiomete

Post by adafruit_support_bill »

I have moved this topic to "General Project Help". The "Arduino Shields from Adafruit" forum is for Adafruit shields. What you have appears to be a copy of one of our older designs.

You are welcome to peruse our tutorials and forums for relevant information, but we can't provide personalized support for products that we do not manufacture.

MEKACI
 
Posts: 12
Joined: Tue Apr 15, 2014 10:20 am

Re: Need Help how to control 4 dc motors with 2 potentiomete

Post by MEKACI »

I was bought that picture motor shield on ebay ( I didn't know your motor shield product until I was started to make search about how to fix my problem ). Would you give me a link that where can I buy your adafruit motor shield too please ( I want to order ; I don't want to buy a wrong motor shield this time really ) ? Do you have shipping to Turkey too ? thanks.

Is there any possibility to give/write to me a simple code that is about how to how to control 1 dc motor with a 1 potentiometer with real adafruit motor shield ? thanks.

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

Re: Need Help how to control 4 dc motors with 2 potentiomete

Post by adafruit_support_bill »

Our current motor shield is the V2 : https://www.adafruit.com/product/1438
We do ship to Turkey. And we also have a distributor there: http://www.roboweb.net/

We do not sell the V1 motor shields anymore. But if you search, you should be able to find some examples similar to what you want to do. Here is one thread on the subject: http://forums.adafruit.com/viewtopic.php?f=31&t=31893

MEKACI
 
Posts: 12
Joined: Tue Apr 15, 2014 10:20 am

Re: Need Help how to control 4 dc motors with 2 potentiomete

Post by MEKACI »

Sorry for asking many questions to you all the time ( I am glad to find a polymath person like you, to ask the questions that prepossess my mind );but thank you very much for replying them for me instantly. I want to learn that some technical informations :


A) When I supply power to arduino :

1) Is arduino's normal power socket enough to supply power to motor shield ( which has 4 dc motors connected to it ) and arduino itself at the same time ?,

2) Is power supply from motor shield is enough to supply the 4 dc motors ( which is connected on the motor shield ) and arduino at the same time ?,


B) ( maximum ) How much volts and ampers does the adafruit motor shield can handle ( which is connected on the arduino ) ? thanks.

C) ( maximum ) How much volts and ampers does arduino can handle ( the adafruit motor shield is connected on the arduino ) ? thanks.

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

Re: Need Help how to control 4 dc motors with 2 potentiomete

Post by adafruit_support_bill »

You should be able to find the answers to those questions here: http://forums.adafruit.com/viewtopic.php?f=31&t=26873

You will also need to know the specifications for the motors you will be using.

MEKACI
 
Posts: 12
Joined: Tue Apr 15, 2014 10:20 am

Re: Need Help how to control 4 dc motors with 2 potentiomete

Post by MEKACI »

adafruit_support_bill friend, there is no such " dc motor speed/direction ( forward/backward ) with a potentionmeter at adafruit motor shield " examples in the internet. I checked everywhere for hours at google at your forums. There is only stepper + pot ;or stepper + encoder ;or dc motor + encoder examples. I have checked these examples and tried to convert them into dc motor + pot version ;but always failing or doesn't work at all ( even the code seems compiled successfully ). Would you give some tips please at coding of " dc motor + pot " example ?

I want to learn properly ; I don't want to do it as memorising or trial and error way. I don't have a stepper or encoder ;so I need your help friend or anyone who might help me. I will order adafruit shield soon ;and while waiting its coming I need help about this issue really. I don't like to be so persistent to ask but please anyone or you friend helps me to start from a point while learning the your motor shield at my problem here about " " dc motor + potentiometer " example for adafruit V2 motor shield ".

Would you/anyone show me a proper understandable " dc motor + potentiometer " example for adafruit V2 motor shield ? thank you very much.

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

Re: Need Help how to control 4 dc motors with 2 potentiomete

Post by adafruit_support_bill »

This tutoral shows how to connect and read from a potentiometer: https://learn.adafruit.com/adafruit-ard ... experiment
This tutoral shows how to use the V2 motor shield with a DC motor: https://learn.adafruit.com/adafruit-mot ... -dc-motors

To control motor speed with the pot, you can use the "setSped()" function from the V2 motor library.
Set the speed of the motor using setSpeed(speed) where the speed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want.
myMotor->setSpeed(100);
Since analog inputs are on a scale of 0-1023 and motor speed is on a scale of 0-255, you will need to divide the analog value by 4 to convert to motor speed.

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

Return to “General Project help”