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 :
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 ) :
#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.