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