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
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.
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.
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()
}
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!!
/*
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
// 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);
}
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 ???