Step 4Second round of code for alternative stepper motor and driver
I used the EasyDriver from SparkFun along with a NEME17 stepper motor during my initial testing.
I have a bigger motor and driver board from my old RepStrap, (NEM23 stepper with RepRap stepper motor driver v1.2 by Zach Hoeken). Using this requires two minor adujstemnts to the code.
1 - The enable pins are opposite.
EasyDriver: Low = Enable
Steper Driver v1.2 : High = Enable
2 - I can't seem to drive the NEM23 as fast as the NEM17. Both boards allow you to switch between full and half step. I'm probably half stepping the NEM23 and full stepping the NEM17. Instead of figuring out the actual difference, I've just adjusted the delay between steps in the code.
Here is the code. I've added NOTES to the code to show where I have made changes.
//////////////////// ARDUINO CODE /////////////////////////////////////
//******* joystickj with 2 POTS to control a stepper and DC motor speed and direction ******//
//NOTE: modified to run NEM23 stepper with Stepper Motor Driver v1.2 from RepRap
//declare pins for Stepper
int potPin_X = 1;
int Step_X = 13;
int Dir_X = 12;
int Enable_X = 8;
//declare values for stepper
int Speed_X = 0; //step speed (delay between steps)
int val_X= 0;
int h = 0;
// declare pins for DC motor A
int potPin_A = 2; // select the input pin for the potentiometer
int val_A = 0; // variable to store the value coming from the sensor
//Declare variables for DC A
int j = 0;
int Dir_A = 4;
int Speed_A = 5;
void setup() {
// setup up stepper pins
pinMode(Step_X, OUTPUT);
pinMode(Dir_X, OUTPUT);
pinMode(Enable_X, OUTPUT);
//setup DC motor A pins
pinMode(Dir_A, OUTPUT);
pinMode(Speed_A, OUTPUT);
//Serial.begin(9600); // note that serial comm can be used to debug
// but it will slow down the code and slow down the stepper motor alot (and be confusing to me)
}
void loop() {
///////// STEPPER READ AND CONTROLL /////////////
// read location of joystick and calculate values for stepper motor
val_X = analogRead(potPin_X); // read the value from the sensor
h = val_X - 517; // 517 is center positions - how far from center?
h = abs(h); //absolute value
//Speed_X = 70000/h; //This math inverts the value and scales as needed (value found through trial and error)
// The delay between steps will determine the speed of the motor
// So, delay up = speed down
//NOTE: Speed_X=70000/h worked well for this combination: EasyDriver -> NEM17 stepper
Speed_X = 160000/h; //NOTE: Speed calculation for Stepper Motor Driver V1-2 (RepRap) -> NEM23 stepper
// control the stepper motor //
//NOTE:
// for EasyDriver: HIGH = disable
// for RepReap Stepper driver v1.2: LOW = Disable
if (val_X >= 530){
digitalWrite(Enable_X,HIGH); // enable
digitalWrite(Dir_X, HIGH); // Set direction
digitalWrite(Step_X,HIGH);
delayMicroseconds(2);
digitalWrite(Step_X,LOW);
delayMicroseconds(Speed_X);
}
if (val_X <= 500) {
digitalWrite(Enable_X,HIGH);// enable
digitalWrite(Dir_X, LOW); // Other direction
digitalWrite(Step_X,HIGH);
delayMicroseconds(2);
digitalWrite(Step_X,LOW);
delayMicroseconds(Speed_X);
}
if (val_X <=530 && val_X >= 500) {
digitalWrite(Enable_X,LOW); // disable the stepper motor if the joystic is in the center
// for EasyStepper: HIGH = disable
// for RepReap Stepper driver v1.2: LOW = Disable
}
////////////// DC MOTOR A - READ AND CONTROL ///////////////////
// Read location of joystick and calculate the distance and from center
val_A = analogRead(potPin_A); // read the value from the sensor
j = val_A - 517; // 517 is center positions - how far from center
j = abs(j); //absolute value
// put some bounds on j to keep PWM values useful
// below 100 the motor won't move and PWM max is 255
if (j >= 510){
j = 510; //the most the PWM pin can do is 255
}
if (j <=200 && j>=10){
j=200; //below 100 PWM the motor makes a high pich sound and does not move
}
if (j <=10){
j=0; // below 10 the joystick is very close to center
}
//Run DC motor A based on analog input from joystick
if (val_A >= 530){
digitalWrite(Dir_A, HIGH); // other direction
analogWrite(Speed_A, j/2); // PWM out (divide by 2 because max is 255)
}
if (val_A <= 500){
digitalWrite(Dir_A, LOW); //
analogWrite(Speed_A, j/2); //
}
if (val_A <=530 && val_A >= 500) {
analogWrite(Speed_A, 0); // turn off if the joystick is in the center
}
// print values for debugging
// Serial.print(val_A); // send numbers to PC so you can see what it going on
// Serial.print(",");
// Serial.println(j);
}
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|
































































