Step 16: Code for all the parts
Here is all the code jamed together. Now we can run the stepper motor back and forth along the track, raise and lower the claw, and open and close the claw.
I am getting a high pich squeal from somewhere... I'll just watch for smoke for now.
//////////////// ARDUINO CODE /////////////////
//******* 2 joysticks with POTS to control a stepper and two DC motors 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;
// declare pins for DC motor B // claw open/close
int potPin_B = 3; // select the input pin for the pot
int val_B = 0; // variable to store the value coming from the sensor
//Declare variables for DC motor B
int k = 0;
int Dir_B = 7;
int Speed_B = 6; //pwm
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);
//setup DC motor B pins
pinMode(Dir_B, OUTPUT);
pinMode(Speed_B, 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
}
////////////// DC MOTOR B - READ AND CONTROL ///////////////////
// Read location of joystick and calculate the distance and from center
val_B = analogRead(potPin_B); // read the value from the sensor
k = val_B - 517; // 517 is center positions - how far from center
k = abs(k); //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 (k >= 510){
k = 510; //the most the PWM pin can do is 255
}
if (k <=200 && k>=10){
k=200; //below 100 PWM the motor makes a high pich sound and does not move
}
if (k <=10){
k=0; // below 10 the joystick is very close to center
}
//Run DC motor B based on analog input from joystick
if (val_B >= 530){
digitalWrite(Dir_B, HIGH); // other direction
analogWrite(Speed_B, k/2); // PWM out (divide by 2 because max is 255)
}
if (val_B <= 500){
digitalWrite(Dir_B, LOW); //
analogWrite(Speed_B, k/2); //
}
if (val_B <=530 && val_B >= 500) {
analogWrite(Speed_B, 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);
}
Remove these ads by
Signing Up













































Not Nice
















Visit Our Store »
Go Pro Today »



