Step 3Do two things at once!
The next thing to do code-wise is to jam together the controls for the stepper motor and DC motor so you can control both at once.
If you look at the code in the previus steps you'll see that I used diferent pins and variable names for each set of test code. This lets us jam them togehter without much modification.
I've read that there are much better ways to combine blocks of code, but I am still learning, so all I have done is cut-and-past the two programs together.
I've incrased the anaolog values that are used as the 'center' position of the joystick. This helps to stop you from accidently running the wrong motor.
Latter we will have to splice in another batch of code to control the motor to open and close the claw!
Here is the Arduino code to control one stepper motor and one DC motor (labeled DC motor A, because we will be adding B latter)
/////////////////////// ARDUINO CODE //////////////////////////
//******* joystickj with 2 POTS to control a stepper and DC motor speed and direction ******//
//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
// 70000 value found through trial and error
// The delay between steps will determine the speed of the motor
/ / So, delay up = speed down
// control the stepper motor //
if (val_X >= 530){
digitalWrite(Enable_X,LOW); // 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,LOW);// 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,HIGH); // disable the stepper motor if the joystic is in the center
}
////////////// 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
|

































































