I am using the Accel Stepper library through the Arduino IDE to program a teensy to drive 3 stepper motors. I am using Touchdesigner to control the position of each motor. I am programing the speed of each motor initially in the Arduino sketch in the setup(). When I run my bounce() function the speed of the motors changes accordingly and everythihng works properly, but as soon as I run the touch() function and begin communicating through Touchdesigner, the motors stop running at the desired speed. The motors will run to the appropriate positions, but at relatively slow speed that I cannot seem to change. Any insight would be helpful. Thank you.
psuh.toe (5.0 KB)
#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
// AccelStepper Setup
AccelStepper stepperX(1, 4, 5); // 1 = Easy Driver interface
AccelStepper stepperY(1, 6, 7);
AccelStepper stepperZ(1, 8, 9);
const int stepperAmount = 3;
const int inputAmount = stepperAmount * 2;
AccelStepper* steppers[stepperAmount] = {
&stepperX,
&stepperY,
&stepperZ,
};
// Define the Pins used
int myHomeSwitches[stepperAmount] = { 10, 11, 12 };
long Travel;
int move_finished = 1;
long initial_homing = -1;
int max_distance = 9000;
int min_distance = 5;
int desiredSpeed = 4000;
char inputBuffer[inputAmount];
int recieved = 0;
void setup() {
Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds
for (int i = 0; i < stepperAmount; i++) {
pinMode(myHomeSwitches[i], INPUT_PULLUP);
}
delay(5); // Wait for EasyDriver wake up
// Set Max Speed and Acceleration of each Steppers at startup for homing
for (int i = 0; i < stepperAmount; i++) {
steppers[i]->setMaxSpeed(1000.0);
steppers[i]->setAcceleration(1000.0);
}
// Start Homing procedure of Stepper Motor at startup
Serial.print("Steppers are Homing . . . . . . . . . . . ");
for (int i = 0; i < stepperAmount; i++) {
while (digitalRead(myHomeSwitches[i])) { // Make the Stepper move CCW until the switch is activated
steppers[i]->moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
steppers[i]->run(); // Start moving the stepper
delay(5);
}
steppers[i]->setCurrentPosition(0); // Set the current position as zero for now
steppers[i]->setMaxSpeed(1000.0); // Set Max Speed of Stepper (Slower to get better accuracy)
steppers[i]->setAcceleration(1000.0); // Set Acceleration of Stepper
}
initial_homing = 1;
for (int i = 0; i < stepperAmount; i++) {
while (!digitalRead(myHomeSwitches[i])) { // Make the Stepper move CW until the switch is deactivated
steppers[i]->moveTo(initial_homing);
steppers[i]->run();
initial_homing++;
delay(5);
}
steppers[i]->setCurrentPosition(0);
steppers[i]->setMaxSpeed(5000.0);
steppers[i]->setSpeed(5000); // Set Max Speed of Stepper (Faster for regular movements)
steppers[i]->setAcceleration(5000); // Set Acceleration of Stepper
// Print out Instructions on the Serial Monitor at Start
// Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
}
Serial.println("Homing Completed");
Serial.println("");
}
void loop() {
// manualINPUT();
//bounce();
touch();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void touch() {
if (Serial.available() > 0) {
recieved = Serial.readBytes(inputBuffer, inputAmount);
for (int i = 0; i < stepperAmount; i++) {
if (recieved > 0) {
int divider = inputBuffer[i * 2];
int remainder = inputBuffer[i * 2 + 1];
Travel = divider * 256 + remainder;
if (steppers[i]->distanceToGo() == 0) {
steppers[i]->moveTo(Travel);
}
steppers[i]->run();
}
}
}
}
void bounce() {
for (int i = 0; i < stepperAmount; i++) {
int TravelX = 9000;
int TravelY = 5000;
int TravelZ = 1000;
if (steppers[0]->distanceToGo() == 0) {
steppers[0]->moveTo(TravelX);
}
if (steppers[1]->distanceToGo() == 0) {
steppers[1]->moveTo(TravelY);
}
if (steppers[2]->distanceToGo() == 0) {
steppers[2]->moveTo(TravelZ);
}
steppers[i]->run();
Travel = 5;
if (steppers[i]->distanceToGo() == 0) {
steppers[i]->moveTo(Travel);
}
steppers[i]->run();
}
}
void manualINPUT() {
while (Serial.available() > 0) { // Check if values are available in the Serial Buffer
move_finished = 0; // Set variable for checking move of the Stepper
Travel = Serial.parseInt(); // Put numeric value from buffer in TravelX variable
if (Travel < 0 || Travel > max_distance) { // Make sure the position entered is not beyond the HOME or MAX position
Serial.println("");
Serial.println("Please enter a value greater than zero and smaller or equal to max_distance.....");
Serial.println("");
} else {
Serial.print("Moving stepper into position: ");
Serial.println(Travel);
stepperX.moveTo(Travel); // Set new moveto position of Stepper
delay(1000); // Wait 1 seconds before moving the Stepper
}
}
if (Travel >= 0 && Travel <= max_distance) {
// Check if the Stepper has reached desired position
if ((stepperX.distanceToGo() != 0)) {
stepperX.run(); // Move Stepper into position
}
// If move is completed display message on Serial Monitor
if ((move_finished == 0) && (stepperX.distanceToGo() == 0)) {
Serial.println("COMPLETED!");
Serial.println("");
Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
move_finished = 1; // Reset move variable
}
}
}