Help with coding please - Arduino+AdaMotorshield+joystick

Hello someone give me some help og maybe a hint??
Thanks in advance.
I'm very-very new to Arduino, and have folloved the basic
setups to get familiar with the hardware ande code.

I am working on a pan-tilt head for my DSlR camera.
I will use 2 stepper motors for this project.

My goal is to control x+y axis with a simple
Thumbstick (joystick) (like the one used in a xbox360 contr.)

I have a Arduino Mega 1280 + Adafruit Motorshield

I have managed to make one of the steppers work quite well
with just connecting a potentiometer to Analog pin 0,
but i simply can't make it work with 2 steppers - i have read the
explanation from Adafruit many times and a lot more tutorials
on the net, but i didn't find anyone with the setup i have.

I am working with this code, but i get some erros when i try to compile
the code in Arduino software.

Code:
#include <Stepper.h>
#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor1(225, 1);
AF_Stepper motor2(225, 2);
int analogPin = A0; // X potentiometer wiper (middle terminal)
int analogPin = A5; // Y potentiometer wiper (middle terminal)

//connected to analog pin 0 and 5
// outside leads to ground and +5V
int val = 0; // Pot1 variable to store the value read
int prepos = 0;
int pos = 0; //0 to 224 .

void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

motor1.setSpeed(60); // 30 rpm
motor2.setSpeed(60); // 30 rpm
}

void loop() {

prepos = pos;
val = analogRead(analogPin); // read the input pins

pos = map (val, 0, 1024, 0, 224);
pos = map (val, 0, 1024, 0, 224);

if (pos != prepos){
int diststep = pos - prepos;
if (diststep < 0) {
motor1.step(-diststep, BACKWARD, INTERLEAVE);
motor2.step(-diststep, BACKWARD, INTERLEAVE);
}
if (diststep > 0) {
motor1.step(diststep, FORWARD, INTERLEAVE);
motor2.step(diststep, FORWARD, INTERLEAVE);
}

}

}

I am working with this code, but i get some erros when i try to compile
the code in Arduino software.

But, you want us to guess what they are?

int analogPin = A0; // X potentiometer wiper (middle terminal)
int analogPin = A5; // Y potentiometer wiper (middle terminal)

You have at least a redefinition error of 'int analogPin'.

Let us know what the Status Bar/Program notification area shows.

Thanks for your quick reply. I'm sorry if i was not clear enough in my request
as i said i'm quite new to Arduino :confused:

When i try to verify the code in get this error:

"redefinition of 'int analog pin"

sketch_sep27a_Stepper_test_with_pot_v1_02:8: error: redefinition of 'int analogPin'
sketch_sep27a_Stepper_test_with_pot_v1_02:7: error: 'int analogPin' previously defined here

As i stated earlier i managed to make the code work with one stepper and a potmeter.
what i have been trying is to add an extra motor and an ekstra potmeter.

with this code:

#include <AFMotor.h>

AF_Stepper motor1(225, 1);
int analogPin = 0; // potentiometer wiper (middle terminal)
//connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
int prepos = 0;
int pos = 0; //0 to 224 .

void setup()
{
motor1.setSpeed(60); // 30 rpm
}

void loop() {

prepos = pos;
val = analogRead(analogPin); // read the input pin

pos = map (val, 0, 1024, 0, 224);

if (pos != prepos){
int diststep = pos - prepos;
if (diststep < 0) {
motor1.step(-diststep, BACKWARD, INTERLEAVE);
}
if (diststep > 0) {
motor1.step(diststep, FORWARD, INTERLEAVE);
}

}

}

Thanks again :slight_smile:

As was pointed out above you are creating two int variables named analogPin, you need to rename one, or remove the int from the second one if you really want to reassign it to a new value just after you gave it one. 8^)

Thanks again i really appreaciate any help as i'm going
nuts, not being able to find a solution, as i know other people
had done it before, or at least pretty similar projects with the
same purpose. I'm not sure if the motorshield make the coding
easyer or if it just makes things more difficult to understand?

Answer to the last comment:
It helped on the errorcode to rename the analog Pin - i renamed to
Analog PinX and Analog PinY.

I have uploadet the code to the arduino, but the motors act strange.
No matter which way i move the joystick X or Y i get reaction on
both motors, but only a slight turn and a lot of humble, with or without
touching the stick.

I'm trying to understand whats going on rather than just make it work.

Would it help you guys to see a schematic of how this project is wired up?

Thanks / Jakob

Would it help you guys to see a schematic of how this project is wired up?

And the current code.

Seeing the schematic is useful, yes.

Also if you post code please // use the code tags (# button) so that its easy to read

Hello and thanks for your replys. I really appreciate it.
If your not familar with the Adafruit Motorshield i have its this one:

It has no importance for me to use this shield i just saw it as an
easy way to achieve my goal with this project: to start filming :slight_smile:
So if anyone have another entry to this project or a good
tutorial somewhere please let med know.

I tried to rename the 2 analog pins with an X and Y in the end
and it solved the issue with the error compiling the code.
But the 2 stepper motors act strange - they both move
when i move the joystick X or Y-axis, and i got a humbling
sound from both motors when the power is on.
and they do not really react very well - it's actually better
if i just connect one motor and a potentiometer.

I have tried to make the schematic in fritzing, but they
didn't have the Ada motorshield, so i had to impovise
in Photoshop with the original print - i moved it a bit,
so its possible to see the number of the pins the shield use.

The code as it is on the Arduino now:

#include <Stepper.h>
#include <AccelStepper.h>
#include <AFMotor.h>


AF_Stepper motor1(225, 1);
AF_Stepper motor2(225, 2);
int analogPinX = A0;     // X potentiometer wiper (middle terminal) 
int analogPinY = A5;     // Y potentiometer wiper (middle terminal) 

//connected to analog pin 0 and 5 
// outside leads to ground and +5V 
int val = 0;           // Pot1 variable to store the value read 
int prepos = 0; 
int pos = 0; //0 to 224 .

void setup() 
{ 
    // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  
  motor1.setSpeed(60);  // 30 rpm   
  motor2.setSpeed(60);  // 30 rpm   
}

void loop() { 
  
  prepos = pos; 
  val = analogRead(analogPinX);    // read the input pins
   val = analogRead(analogPinY);    // read the input pins  
  
  pos = map (val, 0, 1024, 0, 224);  
  
  if (pos != prepos){ 
    int diststep = pos - prepos; 
    if (diststep < 0) { 
    motor1.step(-diststep, BACKWARD, INTERLEAVE); 
    motor2.step(-diststep, BACKWARD, INTERLEAVE); 
    } 
    if (diststep > 0) { 
    motor1.step(diststep, FORWARD, INTERLEAVE); 
    motor2.step(diststep, FORWARD, INTERLEAVE); 
    } 
    
  } 
  
}

Thanks for looking at this.
/Jakob

AF_Stepper motor1(225, 1);

Your steppers have 225 steps per revolution? That is a very strange number of steps per revolution.

    if (diststep < 0) { 
    } 
    if (diststep > 0) {

If it's not less than 0, and it can't possibly be 0, doesn't it have to be greater than 0? No reason to test that.

if(diststep < 0)
{
}
else
{
}

I'd try disconnecting the motors, and just Serial.print() the pot values, to see if they are reasonable.

Thanks again for your support, though the answers leave a lot of new questions.
Can't you make an example or something, as i started out im new to this,
and i think its much easyer to learn from examples.

I found this code on the net and changed it a bit for my setup and i got fine
serial readings in Arduino both X-Y values from the thumbstick.

But i can't figure out how to mix this code with the Adafruit motorshield
to make it work - can you take a look.

I use this code:

#include <Stepper.h>
#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor1(20, 1);
AF_Stepper motor2(20, 2);

const int selectPin = 2;  
const int joystick_xPin = A0;  
const int joystick_yPin = A5;  
int oldX = 0;  
int oldY = 0;  
int oldSelect = 0;  
void setup()  
{  
  pinMode(selectPin, INPUT);  
  digitalWrite(selectPin, HIGH);  
  Serial.begin(9600);  
}
void loop()
{  
  int joystick_x;  
  int joystick_y;  
  int select;  
  joystick_x = map(analogRead(joystick_xPin), 0, 1023, 1, 20);  
  joystick_y = map(analogRead(joystick_yPin), 0, 1023, 1, 20);  
 select = !digitalRead(selectPin);  
  if((oldX != joystick_x) ||  
      (oldY != joystick_y) ||  
     (oldSelect != select)){  
       Serial.print("joystick X: ");  
       Serial.print(joystick_x);  
       Serial.print(" joystick Y: ");  
       Serial.print(joystick_y);  
       if(select){  
          Serial.print(" select");  
       }  
       Serial.println("");  
       oldX = joystick_x;  
       oldY = joystick_y;  
       oldSelect = select;  
  }  
 delay(10);  
}

Thanks

I forgot to mention that for this test setup i use 2 old CD-rom steppers,
as i can figure out they have a resolution of 24 steps pr. rotation.
/ Jakob

I forgot to mention that for this test setup i use 2 old CD-rom steppers,
as i can figure out they have a resolution of 24 steps pr. rotation.

So, why are you telling the class that they have 20?

AF_Stepper motor1(20, 1);
AF_Stepper motor2(20, 2);
  int joystick_x;  
  int joystick_y;  
  int select;  
  joystick_x = map(analogRead(joystick_xPin), 0, 1023, 1, 20);  
  joystick_y = map(analogRead(joystick_yPin), 0, 1023, 1, 20);  
 select = !digitalRead(selectPin);

It is quite OK to declare and initialized the variable in one step.

What do you want to do with the joystick values? Don't worry about how to do that in code. Just describe what increasing or decreasing X values should cause the one stepper to do, and what increasing or decreasing Y values should cause the other stepper to do.