Go Down

Topic: Stepper motor and Joystick [HELP] (Read 1 time) previous topic - next topic

focamonca

Hi folks!
I'm stuck in this simple project:

I've a dslr slider and I want to motorize it.
I have an Arduino UNO, Arduino motor shield, a Joystick, a Stepper motor nema 17, pulley and belt, a 3d printer and an external Battery supply.
The real problem starts on coding:

what I want to do is: if i pull my joystick with the thumb to the right the dslr goes to the right and thesame for left, the directions up and down isn't needed. But the speed has to be proportional to the movement of the joystick.

this is the approximative code that obviously doesn't works. (is the first time i do something with stepper motor so please be patient)

Quote
#include <Stepper.h>
#include <Servo.h>  // pd

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);     
}


Robin2

There are two separate issues here.

First there is the interpretation of the data from the joystick so that you get a speed and direction value proportionate to the stick position.

If the stick gives values between 0 and 1023 then if you subtract 512 you will get a range from -512 to +512 approx. Write a short program to display those values on the serial monitor.

Second, assuming you have a value between -X and + X, how do you make the motor move at that speed.

An h-bridge motor shield is a poor choice for controlling a stepper motor. Life is much easier with a specialized stepper driver. See the stepper motor basics link below

Even if you choose to stick with the motor shield the principle of changing a stepper motor's speed is simply to vary the time between successive steps. This simple stepper code illustrates the idea (but it is not designed for the motor shield).

...R
Planning an Implementing a Program   Several things at a time    Serial Input Basics    Stepper Motor Basics
Python-Arduino Comms   Python GUI demo    EzScrn

Two or three hours spent thinking and reading documentation solves most programming problems.

MarkT

What are the details of the NEMA17 stepper?  Low impedance bipolar motors won't
work satisfactorily with motor shields, a current-drive stepper controller (chopper
drive) is what is needed for that type of motor.

A high impedance unipolar stepper (5,6,8 wire) would be easy to
drive from ULN2803 (again no motor shield needed).  It won't be able to go
anything like as fast as a low impedance motor with a chopper drive.

A high impedance bipolar motor can be driven from a motor shield, again not
rapidly, but ULN2803's are cheaper than motor shields anyway, so in general
a motor shield isn't the right thing to drive steppers.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up