Hi all,
Thanks a lot for this awesome library! This makes things a lot easier!
I'm currently making a followup tutorial on how to control a stepper motor using Touchdesigner. The goal is to have a default block that anyone can use: move the motor to a certain position, move the motor at a constant speed, a sine wave movement, ...
This will be the interface:
With buttons 0 - 4 correspoding with a certain movement: constant speed, lfo, manual, ...
I wired my motor in the following way:
This is the code:
//
// Parse incoming messages consisting of four decimal values followed by a carriage return
// Example "2 10000 6000 6100\n"
// In TouchDesigner: op('serial1').send("2 10000 6000 6100", terminator="\n")
//
#include <AccelStepper.h>
// Define a stepper and the pins it will use: 1 is enable pin, 8 is pulse pin, 9 is direction
AccelStepper stepper(1, 8, 9);
char buffer[32]; //maximum expected length
int len = 0; //necessary for the overflow.
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
int stateMotorGlobal; //motor state: 0 = stop motor, 1 = run at constant speed, 2 = run ccw, 3 = run cw
int speedMotorGlobal; //speed of the motor
int accelerationMotorGlobal; //acceleration of the motor
int stepsMotorGlobal; // number of steps
void setup()
{
Serial.begin(19200); //baud rate. Should be the same in 'Serial' dat: baud rate.
stateMotorGlobal = 0;
speedMotorGlobal = 0;
accelerationMotorGlobal = 0;
stepsMotorGlobal = 0;
stepper.setMaxSpeed(0); // MAX SPEED = steps/second
stepper.setAcceleration(0); // ACCELERATION = steps/seconds²
stepper.disableOutputs(); // Disable outputs, so no heating of the motor at idle
}
void loop()
{
checkSerial(); //check the serial communication and save variables
controlMotor(); //run motor
}
void controlMotor() {
if (stateMotorGlobal == 3) {
stepper.setMaxSpeed(speedMotorGlobal); // MAX SPEED = steps/second
stepper.setAcceleration(accelerationMotorGlobal); // ACCELERATION = steps/seconds²
stepper.enableOutputs(); //enable output pins
stepper.moveTo(-stepsMotorGlobal); //move the motor CW
stepper.run();
}
else if (stateMotorGlobal == 2) {
stepper.setMaxSpeed(speedMotorGlobal); // MAX SPEED = steps/second
stepper.setAcceleration(accelerationMotorGlobal); // ACCELERATION = steps/seconds²
stepper.enableOutputs(); //enable output pins
stepper.moveTo(stepsMotorGlobal); //move the motor CCW
stepper.run();
}
else if (stateMotorGlobal == 1) {
stepper.setMaxSpeed(speedMotorGlobal); // MAX SPEED = steps/second
stepper.setAcceleration(accelerationMotorGlobal); // ACCELERATION = steps/seconds²
stepper.setSpeed(speedMotorGlobal); // MAX SPEED = steps/second
stepper.enableOutputs(); //enable output pins
stepper.runSpeed();
stepper.run();
}
else {
stepper.disableOutputs(); //disable power
stepper.setCurrentPosition(0); // reset position
stepper.stop(); //stop motor
}
}
void checkSerial() {
if (Serial.available() > 0)
{
int incomingByte = Serial.read();
buffer[len++] = incomingByte;
newData = true; //flag
//
// check for overflow
//
if (len >= 32)
{
// overflow, resetting
len = 0;
}
//
// check for newline (end of message)
//
if (newData == true) {
if (incomingByte == '\n')
{
int stateMotor;
int speedMotor;
int accelerationMotor;
int stepsMotor;
int n = sscanf(buffer, "%d %d %d %d",&stateMotor, &speedMotor, &accelerationMotor, &stepsMotor);
if (n == 4)
{
stateMotorGlobal = stateMotor;
speedMotorGlobal = speedMotor;
accelerationMotorGlobal = accelerationMotor;
stepsMotorGlobal = stepsMotor;
}
else
{
// parsing error, reject
}
len = 0; // reset buffer counter
}
}
}
newData = false;
}
You get 4 values from Touchdesigner which will control the motor.
int stateMotorGlobal; //motor state: 0 = stop motor, 1 = run at constant speed, 2 = run ccw, 3 = run cw
int speedMotorGlobal; //speed of the motor
int accelerationMotorGlobal; //acceleration of the motor
int stepsMotorGlobal; // number of steps
Based on these 4 values, the motor will either stop, run at a constant speed, move ccw or move cw.
It all works fine except for 3 things:
1. The stepper.disableOutputs() does not work. There is still some torque on the motor. I think this is due to the enable needing a LOW to be switch off instead of a HIGH. Can this be fixed?
2. When I upload the code, i get a weird noise:
YouTube link Is this normal or is my wiring incorrect?
3. When I want to rotate at a constant speed, I cannot make a difference between CW and CCW. How can I program this?
Thanks a lot! If you have any suggestions on improving it or adding extra features, let me know! I just want to share this to everyone so they can use it in their projects :-)
Peace,
Jakster!