Pages: [1] 2   Go Down
Author Topic: Arduino Motor Shield Stepper Motor control with Joystick  (Read 6631 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I recently purchased an Arduino Uno R3 board with a Arduino Motor Shield Rev3 as well as a joystick breakout board. My aim is to use the thumb joystick to control the direction of the stepper motor on the X-axis, left to right as well as the speed. So far I've been looking for good tutorials on the net with regards to the subject but had no luck so far.

Attached to this article are two photos showing how I connected my joystick to the motor shield board as well as a closeup view of the specific joystick I bought. Here is a link to the joystick as well:

http://robotics.org.za/index.php?route=product/product&keyword=joystick%20breakout%20board&category_id=0&product_id=217

Below is some sample code I downloaded from the net which rotates the shaft from one direction to the other with a delay. How can I modify this code to work with my setup and control the motor via the joystick:

#include <Stepper.h>
 
const int spr = 200; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;

Stepper stepper(spr, dir_cha, dir_chb);

void setup()
{
    pinMode(pwm_cha, OUTPUT);
    pinMode(pwm_chb, OUTPUT);
    pinMode(brake_cha, OUTPUT);
    pinMode(brake_chb, OUTPUT);
    digitalWrite(pwm_cha, HIGH);
    digitalWrite(pwm_chb, HIGH);
    digitalWrite(brake_cha, LOW);
    digitalWrite(brake_chb, LOW);

    stepper.setSpeed(75);
 }
 
void loop()
{
  stepper.step(spr);

  delay(0);

  stepper.step(-spr);

  delay(0);
}

Any help would really be appreciated!

Regards




* Connection.jpg (207.62 KB, 800x600 - viewed 227 times.)

* Joystickmodule2-500x500.jpg (49.1 KB, 500x375 - viewed 52 times.)
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could you give more info on how exactly you one to control the motor with the joystick.
what i mean is what does pushing the joystick to the right do to the motor and what know, please explain so we could help you more
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, when the joystick is in the center and not moving, the motor shouldn't do anything and stop. When the joystick is pressed to the left or right, the motor must move in the respective direction and the further the joystick moves from the center, the faster the speed of the motor must be. So basically left and right movement via the joystick as well as speed adjusting as the joystick moves further from the center pin.

The purpose of this setup is to slide a camera left and right, faster and slower on a rail of plus minus 1.5m in length. Attached to the stepper motor's shaft will be a pulley and on the other side just a normal shaft enclosed in a bearing unit also containing a pulley. I'll then use timing belt to slide the camera left and right.

Here are my stepper motor specs:

2 Phase Hybrid (Model: 23HS6430)
3A
Bipolar
4 Wires
1.8 degree step angle

I'm really not very clued up with prototyping and therefore if you can help me to amend the code to work with my setup, I'll really be happy since I've been battling and struggling to get the project going. But looking at my connection setup, is everything connected to the right pins etc?

Looking forward to hear from you.

Regards

Ian
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the answer for your question is located here
http://arduino.cc/en/Tutorial/MotorKnob
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well what you need to do is to divide the value of a pot into half
so i would say that 507 until 517 is a good middle value.
then take the value between 517 until 1023 and map it with the min and max of the stepper speed.
after that take between 0 to 507 value of pot n map it with the min n max of the stepper speed.
third is that determine which side of the pot make the stepper move clockwise. what i give here should give you an idea on how to do it
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In theory I know what you're saying and it makes sense to me. However, how can I practically amend this code and incorporate everything that you have said? I'm not a code guru and this is all new to me.

Can you perhaps show me what the code will look like?

****************************************

#include <Stepper.h>
 
const int spr = 200; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;

Stepper stepper(spr, dir_cha, dir_chb);

void setup()
{
    pinMode(pwm_cha, OUTPUT);
    pinMode(pwm_chb, OUTPUT);
    pinMode(brake_cha, OUTPUT);
    pinMode(brake_chb, OUTPUT);
    digitalWrite(pwm_cha, HIGH);
    digitalWrite(pwm_chb, HIGH);
    digitalWrite(brake_cha, LOW);
    digitalWrite(brake_chb, LOW);

    stepper.setSpeed(75);
 }
 
void loop()
{
  stepper.step(spr);

  delay(0);

  stepper.step(-spr);

  delay(0);
}

****************************************

I like this code since it seems to work with my motor quite well. Now I just need to know how to amend it to work with the joystick.

But thank you so much for the advice that you have given me already. I appreciate it!
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well ok i had the chance to study the code, From what i can say is that the code have some part that need to be change for instant
Code:
const int spr = 200; // motor steps per revolution
this part is the part which dictate how much step the stepper would take to complete a  rotation.
i would recommend to you to put the total number of step it take for your set up form its start position on the far left to the far right or vice versa. How ever this is not something that is really must needed and you could actually make it variable.... maybe by making use of the other axis of your joystick?

anyway part that really control the speed of your setup is this one
Code:
stepper.setSpeed(75);

this is the part that call for the speed of how fast the stepper rotate.
if my understanding of your project is correct, this part need to be implemented in the loop function and now the setup function cause it need to be variable. However before you impliment any of the changes that i suggest, i would recommend that you play with the value 75 that is in the code with something that is higher up until the point your motor stall. This would mean, that the value you got there is the max speed that the stepper could go. one more thing is would also suggest that you do this with the full load of your setup. this is to make sure that you get the most accurate value for your setup and not just something that is calculated or theoretical.

ok now for the pseudo code ( only the loop part but i thing you would get the idea well)

Code:
void loop()
{
  PVal=analogRead(Pot1);
  if (PVal >= 507 || PVal <= 517)
  { // fill in with the one you need to make it brake
  }
  if (PVal > 517 )
  {
    int K= map (Pval, 518,1023,0,MAX); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
     stepper.setSpeed(K); this set the speed of the stepper motor
     stepper.step(spr); this set the direction and the number of step it would take
   }
 if (PVal < 507 )
  {
    int K= map (Pval, 0,506,0,MAX); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
     stepper.setSpeed(K); //this set the speed of the stepper motor
     stepper.step(-spr); //this set the direction and the number of step it would take
   }
 

this should help you with your project anyway keep us posted and if you be kind enough to send us a link to a you tube showing how it work would be great .
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Btw i would also like to suggest to you that, you pun 2 home sensor, this could be a simple limit switch or the type that use light sensor to detect your carriage on both end of the travel. this would help if let say your carriage at it end but you keep giving it the signal to go forward. although for a stepper this will not cause lots of trouble since if the motor will stall if it cant move the carriage but it might or could shorten the life of the motor... so putting the sensor will save it n give the motor a much longer life and reliability 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, I've tried to fill in the gaps but still no luck...

Here is what I've done:

#include <Stepper.h>
 
const int spr = 200; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;

int Pot1 = 0;

Stepper stepper(spr, dir_cha, dir_chb);

void setup()
{
    pinMode(pwm_cha, OUTPUT);
    pinMode(pwm_chb, OUTPUT);
    pinMode(brake_cha, OUTPUT);
    pinMode(brake_chb, OUTPUT);
    digitalWrite(pwm_cha, HIGH);
    digitalWrite(pwm_chb, HIGH);
    digitalWrite(brake_cha, LOW);
    digitalWrite(brake_chb, LOW);

    //stepper.setSpeed(75);
 }
 

void loop()
{
  int PVal=analogRead(Pot1);
  if (PVal >= 507 || PVal <= 517)
  { // fill in with the one you need to make it brake
  }
  if (PVal > 517 )
  {
    int K= map (PVal, 518,1023,0,75); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
     stepper.setSpeed(K); //this set the speed of the stepper motor
     stepper.step(spr); //this set the direction and the number of step it would take
   }
 if (PVal < 507 )
  {
    int K= map (PVal, 0,506,0,75); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
     stepper.setSpeed(K); //this set the speed of the stepper motor
     stepper.step(-spr); //this set the direction and the number of step it would take
   }
}

Nothing seems to be happening...

Sorry, I know that I come forth as someone with no brains for this stuff, but I'm no guru. I will gladly post a video if I can get it working.
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok i get your code now..

urm lets see ok,
Code:
int Pot1 = 0;
change that to
Code:
int Pot1 = A0;

this should do it
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Ash

I amended the code accordingly but still no luck. Giving me one-liner codes won't really help me since I don't know arduino coding or any other type of programming to a huge extend.

Here is the code the last time with the changes...

#include <Stepper.h>
 
const int spr = 200; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;

int Pot1 = A0;

Stepper stepper(spr, dir_cha, dir_chb);

void setup()
{
    pinMode(Pot1, INPUT);
    pinMode(pwm_cha, OUTPUT);
    pinMode(pwm_chb, OUTPUT);
    pinMode(brake_cha, OUTPUT);
    pinMode(brake_chb, OUTPUT);
    digitalWrite(pwm_cha, HIGH);
    digitalWrite(pwm_chb, HIGH);
    digitalWrite(brake_cha, LOW);
    digitalWrite(brake_chb, LOW);
}
 
void loop()
{
  int PVal=analogRead(Pot1);
  if (PVal >= 507 || PVal <= 517)
  {
   
  }
  if (PVal > 517 )
  {
    int K= map (PVal, 518,1023,0,75); // max is the value that you get from your experiment with the max speed of the stepper before stalling
    stepper.setSpeed(K); //this set the speed of the stepper motor
    stepper.step(spr); //this set the direction and the number of step it would take
  }
  if (PVal < 507 )
  {
    int K= map (PVal, 0,506,0,75); // max is the value that you get from your experiment with the max speed of the stepper before stalling
    stepper.setSpeed(K); //this set the speed of the stepper motor
    stepper.step(-spr); //this set the direction and the number of step it would take
  }
}

For some reason nothing is happening.

But thanks anyway for your help. I'm sure I'll manage somehow somewhere eventually while searching the net. I need a solution quite urgent and can't struggle like this for too long.

Regards
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have been using your problem to learn about the stepper board.  And the problem you are having is due to the Shield using A0 as current sensing.  Move the Pot to A2 and amend the code accordingly.

Secondly, the mapping are slightly wrong - the last if statement needs to be reversed.

Finally, I have improved the response time when in the slow speed by mapping the number of steps to the speed.

I have put some prints in to look at the values and also there is some servo stuff to show the position of the pot - this can all be removed

regards,
 James

Code:
#include <Stepper.h>
#include <Servo.h>
 
const int num_step = 50; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;


int Pot1 = 2;
int val;
Servo myservo;  // create servo object to control a servo
Stepper stepper(num_step, dir_cha, dir_chb);

void setup()
{
    Serial.begin(9600);
    pinMode(pwm_cha, OUTPUT);
    pinMode(pwm_chb, OUTPUT);
    pinMode(brake_cha, OUTPUT);
    pinMode(brake_chb, OUTPUT);
    digitalWrite(pwm_cha, HIGH);
    digitalWrite(pwm_chb, HIGH);
    digitalWrite(brake_cha, LOW);
    digitalWrite(brake_chb, LOW);
    
    myservo.attach(2);
 }
 
void loop()
{
  int PVal=analogRead(Pot1);
  if (PVal >= 507 || PVal <= 517)
  { // fill in with the one you need to make it brake
  }
  if (PVal > 517 )
  {
    int K= map (PVal, 518,1023,0,400); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
     int S=map (K, 0,400,0,num_step);
     stepper.setSpeed(K); //this set the speed of the stepper motor
     stepper.step(S); //this set the direction and the number of step it would take
     Serial.print("Forward, K=");
     Serial.print(K);
     Serial.print(" PVAL=");
     Serial.println(PVal);    
   }
 if (PVal < 507 )
  {
    int K= map (PVal, 0,506,400,0); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
     int S=map (K, 0,400,0,num_step);
     stepper.setSpeed(K); //this set the speed of the stepper motor
     stepper.step(-S); //this set the direction and the number of step it would take
     Serial.print("Backward, K=");
     Serial.print(K);
     Serial.print(" PVAL=");
     Serial.println(PVal);    
   }
  
  // This next bit just outputs the pot position to a servo
  val = map(PVal, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);      
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ash, you are awesome man!! Thanks so much. My headaches are over. You cracked the code and full credit goes to you. I'll post a link on youtube as soon as my project is complete.

But thanks again! I really appreciate it very much!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ash, you are awesome man!! Thanks so much. My headaches are over. You cracked the code and full credit goes to you. I'll post a link on youtube as soon as my project is complete.

But thanks again! I really appreciate it very much!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ash, you are awesome man!! Thanks so much. My headaches are over. You cracked the code and full credit goes to you. I'll post a link on youtube as soon as my project is complete.

But thanks again! I really appreciate it very much!
Logged

Pages: [1] 2   Go Up
Jump to: