Control 1 servo and 1 d.c motor with one joystick

i am new to arduino and trying to write code but i am having a lot of trouble i dont know where to start. i have found this video on youtube with exactly the same circuit that im looking for but there is no code or scematic with the video.

p.s i am using arduino uno and i have an adafruit motorshield v2.3 if required for the circuit

Cn someone please help???

potentometer with servo

adafruit.com/adafruit-motor-shield-v2

tank you very much for the reply but i have seen that link before and it does not give me informtion on exactly what im looking for.

can someone help create my own code for this project

What kind of joystick do you have? There are many kinds of joysticks. A link or a good picture would help.

https://www.google.ie/imgres?imgurl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FMlDi0vO9Evg%2Fmaxresdefault.jpg&imgrefurl=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DMlDi0vO9Evg&docid=6tL72Zw1-yF3HM&tbnid=Rb2U4uIMCf47rM%3A&w=1280&h=720&ei=qlfxVtGvO8fEOqyutYgD

That is an analog joystick so it has 2 potentiometers. Let's split the problem into 2 parts, get each part to work, then combine the parts into a finished sketch. First, have you tried the knob example (linked above) with one axis of the joystick in place of the pot in the example? Understanding how that works will be a big help for getting the motor to do what you want.

Thank you groundfungus,

yes i am familiar with that sketch and have gotten it to work. i am really just here to look for the right circuit and right code for the d.c motor also i am not sure how to properly join two different sketches without getting error messages

I have never used an Adafruit Motor Shield V2 nor the Adafruit library. The tutorial that I linked shows the connection for the motor and the reference for the library is linked in the tutorial. Based on a quick read of the library reference here is what I would try. Read the joystick, if the reading is less than 500, set the motor direction to BACKWORD, else if the reading is greater than 525 set direction to FORWARD using the run function. If between 500 and 525 do nothing stop the motor with run(RELEASE). The gap (500 to 525) gives "hysteresis" or dead zone. Calculate how far the reading is from 512. The absolute value of the difference {abs((512 - reading) / 2)} is the speed to set in the setSpeed function. Divide by 2 is to keep the speed value within the 0 - 255 that the setSpeed function expects.

edit: to be clear, do the calculation and have the setSpeed function before the run function in the code.

Merging scetches:
/CombiningArduinoSketches
Merging_Code

If you get errors when you try to combine the codes, post the code and errors and we will figure them out Please post code and errors in code tags and post the entire error text.

thansk once gain groundfungus, your posts will help immensly but as i said i am new to arduino is there any way you could give me a more basic explaination on how to tackle this or any similr pieces of code tht i could reference to??

The tutorial that I linked is quite good and should get you started.

Do you have the Adafruit_MotorShield library installed in your sketchbook libraries folder?
library install
Does your motor draw less than 1.2 A? What do you have for a motor supply? The Arduino 5V will most likely not drive a motor of any size. What voltage does your motor take? The motor voltage need to be over 4.5 V to work with that motor shield. From rhe FAQ: Can this shield control small 3V motors?
Not really, its meant for larger, 5V+ motors. It does not work for 3V motors unless you overdrive them at 5V and then they will burn out faster.

I put the dc motor tutorial code into a sketch. I can't compile it as I do not have the library. If it won't compile, post the error and I will try to fix it. This should get the motor to run. Once it runs we can work on speed control.

/*
Connecting DC Motors
To connect a motor, simply solder two wires to the terminals and then connect them to either the M1, M2, M3, or M4. 
Then follow these steps in your sketch
Include the required libraries
Make sure you #include the required libraries
*/

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

//Create the Adafruit_MotorShield object
    Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

//Create the DC motor object
    Adafruit_DCMotor *myMotor = AFMS.getMotor(1);

/*
with getMotor(port#). Port# is which port it is connected to. 
If you're using M1 its 1, M2 use 2, M3 use 3 and M4 use 4
*/

void setup{}
{
//Connect to the Controller
//In your setup() function, call 'begin()" on the Adafruit_MotorShield object:
    AFMS.begin();
    
//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);   // play with this

//To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE
//Of course, the Arduino doesn't actually know if the motor is 'forward' or 'backward', 
//so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield.

    myMotor->run(FORWARD);  // play with this
} 

void loop
{
  //all action in setup()
}

i tried to compile the sketch and i got these error messages.

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino/Genuino Uno"

sketch_mar22a.ino:12:45: fatal error: utility/Adafruit_PWMServoDriver.h: No such file or directory
compilation terminated.
Error compiling.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

a 9v battery will be used to supply the motor, i am not yet sure of the voltage of the motor as my teacher is ordering them. i do know that it will be over 5v it may be 6v or even 12v. i am unsure of the amperage aswell

Is it because im missing a library that the sketch will not compile?

the code that you supplied gave error messages but i tried the sketch in the examples "DC motor test" probably the same thing or similar nd it is working now!!

what should i do next?

OK! Please post the code that you have working and a link to the example so that I can see which library and commands (functions) to use.

the code i used is below

the image attached shows how i found the code

/*
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
----> Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit [v2.3] : ID 1438 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits
*/

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

// 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);
// 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!");

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

void loop() {
uint8_t i;

Serial.print("tick");

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

Serial.print("tock");

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

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

Capture.PNG

Now that you have code that makes the motor work my reply #7 might make more sense. Try to bowwow the code from the knob example to read the joystick.

have you had a chance to write the code yet, im not getting anywhere with it

HELLO,

i have an arduino uno and an adafruit motorshield v2.3 i am wondering how i can use these to control the speed and direction (ie forward and reverse) or a dc motor using a potenetiometer or in this case a two axis thumbstick (ps2)

i am new to arduino and need assistance on how to write code for this sketch, can someone help ???

If this is so urgent, why would you risk a forum time-out by CROSS-POSTING?

Threads merged.

DO NOT CROSS-POST, CROSS-POSTING WASTES TIME.

i apologise i am new to this forum and just trying to get some information