// KnobToStepper.ino : demonstrate operation of a stepper motor driver and mapping an analog input to a position output
#include <Stepper.h>
// Define the pin numbers on which the outputs are generated.
#define DIR_PIN 2 // The direction pin controls the direction of stepper motor rotation.
#define STEP_PIN 3 // Each pulse on the STEP pin moves the stepper motor one angular unit.
/****************************************************************/
// This function is called once after reset to initialize the program.
void setup()
{
// Initialize the digital output pins to output drive mode.
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
}
/****************************************************************/
// Global variables.
int previous = 0; // the previous analog input value
/****************************************************************/
// This function executes a stepper motor motion for a given distance and speed.
// It does not return until the motion is complete, e.g. it 'blocks' for the
// duration.
// dist is the angular displacement in arbitrary units (negative for reverse movement)
// speed is a number from .01 -> 1, with 1 being fastest.
void rotate(float dist, float speed)
{
// Configure the direction pin on the stepper motor driver based on the sign
// of the displacement.
int dir = (dist > 0)? HIGH:LOW;
digitalWrite(DIR_PIN,dir);
// Compute an integer number of step cycles to perform using the absolute
// value of the displacment and arbitrary scaling.
int steps = abs(dist)*(1/0.225);
// Compute a delay time in microseconds controlling the duration of each half
// of the step cycle.
float usDelay = (1/speed) * 70;
// Loop for the given number of step cycles, producing a square wave step
// command for the stepper motor driver.
for(int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
}
}
/****************************************************************/
// This function is called repeatedly as fast as possible from within the
// built-in library to poll program events.
void loop()
{
// read the current value of the potentiometer input from analog input 0
int val = analogRead(0);
// compute the displacement from the previous reading and execute a
// proportional angular motor move at a specified speed
rotate( val - previous, 0.2 );
// save the last potentiometer value for the next displacement calculation
previous = val;
}
/****************************************************************/
Welcome to the Arduino forum. Well done on posting your code correctly!
Hello, I found this code online and it was written by a prof. I want to modify it to add another motor using a separate 10k pot. Is this feasible? This is what I came up with based on his code....
// KnobToStepper.ino : demonstrate operation of a stepper motor driver and mapping an analog input to a position output
#include <Stepper.h>
// Define the pin numbers on which the outputs are generated.
#define DIR_PIN_1 2
#define DIR_PIN_2 4 // The direction pin controls the direction of stepper motor rotation.
#define STEP_PIN_1 3
#define STEP_PIN_2 5 // Each pulse on the STEP pin moves the stepper motor one angular unit.
/****************************************************************/
// This function is called once after reset to initialize the program.
void setup()
{
// Initialize the digital output pins to output drive mode.
pinMode(DIR_PIN_1, OUTPUT);
pinMode(STEP_PIN_1, OUTPUT);
pinMode(DIR_PIN_2, OUTPUT);
pinMode(STEP_PIN_2, OUTPUT);
}
/****************************************************************/
// Global variables.
int previous = 0; // the previous analog input value
/****************************************************************/
// This function executes a stepper motor motion for a given distance and speed.
// It does not return until the motion is complete, e.g. it 'blocks' for the
// duration.
// dist is the angular displacement in arbitrary units (negative for reverse movement)
// speed is a number from .01 -> 1, with 1 being fastest.
void rotate(float dist, float speed)
{
// Configure the direction pin on the stepper motor driver based on the sign
// of the displacement.
int dir = (dist > 0)? HIGH:LOW;
digitalWrite(DIR_PIN_1,dir);
// Compute an integer number of step cycles to perform using the absolute
// value of the displacment and arbitrary scaling.
int steps = abs(dist)*(1/0.225);
// Compute a delay time in microseconds controlling the duration of each half
// of the step cycle.
float usDelay = (1/speed) * 70;
// Loop for the given number of step cycles, producing a square wave step
// command for the stepper motor driver.
for(int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN_1, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN_1, LOW);
delayMicroseconds(usDelay);
}
}
void rotate_1(float dist, float speed)
{
// Configure the direction pin on the stepper motor driver based on the sign
// of the displacement.
int dir = (dist > 0)? HIGH:LOW;
digitalWrite(DIR_PIN_2,dir);
// Compute an integer number of step cycles to perform using the absolute
// value of the displacment and arbitrary scaling.
int steps = abs(dist)*(1/0.225);
// Compute a delay time in microseconds controlling the duration of each half
// of the step cycle.
float usDelay = (1/speed) * 70;
// Loop for the given number of step cycles, producing a square wave step
// command for the stepper motor driver.
for(int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN_2, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN_2, LOW);
delayMicroseconds(usDelay);
}
}
/****************************************************************/
// This function is called repeatedly as fast as possible from within the
// built-in library to poll program events.
void loop()
{
// read the current value of the potentiometer input from analog input 0
int val = analogRead(0);
int val = analogRead(1);
// compute the displacement from the previous reading and execute a
// proportional angular motor move at a specified speed
rotate( val - previous, 0.2 );
rotate_1( val - previous, 0.2 );
// save the last potentiometer value for the next displacement calculation
previous = val;
}
/****************************************************************/
Thank you, apologies for the unorganized question I couldnt figure out how to add code and ask a question the same time lol
Does the first version work? Does you modified version work as you want?
The first one does work with one motor. I haven't bought the second motor yet because I don't fully understand arduino c-style coding yet. I have barely even used python in school. I wanted to make sure the variables and stuff I threw into the code, seem like they work and that there are not any glaring mistakes or if what im trying to d is possible or if I should just purchase a second arduino for the second motor.
Good grief. You are afraid of your own shadow! Get the second motor and begin testing the code as you write it. NEVER write a bunch of code without testing as you go.
You can't have two variables in the same scope with the same name. Try val_1 and val_2. You will also need 'previous_1' and 'previous_2'.
You don't need two 'rotate' functions since the only differences are the STEP and DIRECTION pins. You can pass the pins as arguments:
rotate( STEP_PIN_1, DIR_PIN_1, val_1 - previous_1, 0.2 );
rotate( STEP_PIN_2, DIR_PIN_2, val_2 - previous_2, 0.2 );
Thank you!! I have the motor ordered and will post the results asap!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.