I plan to control this using 2 analog joysticks for all of the movement.
Question is how do I control motor direction on the Adafruit motor shield using a analog joystick? I have seen examples for the motor shield on using it for PIR robots, but more of a motor forward motor backwards. I want speed control.
What kind of analog joystick? a game port one? something knicked from an arcade machine? An analog joystick is just two 100k ohm potentiometers, one for X axis and the other for the Y axis. Well, the value might not be 100k depending on where the stick is intended to be used.
I think you can read the position of the joystick via 2 of the analog ports. Just need to calibrate the values for center and the extremes.
#include <AFMotor.h> // imported AFMotor library
AF_DCMotor motor(3);
int SPEED = 0;
int val = 0;
void setup() {
Serial.begin(9600);
motor.run(RELEASE);
delay(50);
}
void moveForward() {
motor.run(FORWARD);
motor.setSpeed(SPEED);
Serial.print(" ");
Serial.print( SPEED);
}
void moveBackward() {
motor.run(BACKWARD);
motor.setSpeed(SPEED);
Serial.print(" ");
Serial.print( SPEED);
}
void loop() {
int val = analogRead(1);
if (val > 540) {
// move faster the higher the value from the potentiometer
SPEED = 2048 - 1024 * val / 512 + 1;
moveForward();
} else if (val < 480) {
// move faster the lower the value from the potentiometer
SPEED = 1024 * val / 512 + 1;
moveBackward();
} else {
motor.run(RELEASE);
}
My values are off. Mid point between forward and middle and backward and middle are the high speeds. modifying it for 2 joysticks makes it very buggy, but usable.