The following script should be used to check the connection to a stepper motor and determinate the maximum speed ect. true a menu.
After starting the program the first entered command doesnt work, the second creates a respond.
Any suggestion?
#include <AccelStepper.h>
// PINS
int YAxisDIR = 13; // Stepper direction output
int XAxisDIR = 12;
int YAxisPWM = 11; // Stepper steps output
int XAxisPWM = 10;
int XYEnable = 6; // M415C Enable output
int yStopper1 = 5; // Y Rotor end stops
int yStopper2 = 4;
int xStopper1 = 3; // X Rotor end stops
int xStopper2 = 2;
int index = 0; // Used in the commandProcessing routine
int commLetter = 0;
// DEGREES / STEPS CONVERSION
float stepsPerRotation = 200; // 200 steps per rotation (1.8 degrees per step)
int xGearRatio = 42; // stepper ratio per X degree of rotation (1:42)
int yGearRatio = 42; // stepper ratio per Y degree of rotation
int xMicroSteps = 2; // Value of the microsteps
int yMicroSteps = 2; // Posible value's are 1, 2, 4, 8 and 16
int xOffset = 8; // Numer of degrees offset from 0 or 180 degree
int yOffset = 8; // Numer of degrees offset from 0 or 180 degree
typedef enum {
X_ROTOR, Y_ROTOR
}
stepperfn;
long deg2step( float degrees, stepperfn stepper );
long deg2step( float degrees, stepperfn stepper ) {
if (stepper == X_ROTOR) return (( degrees / 1.8) * xMicroSteps * xGearRatio);
if (stepper == Y_ROTOR) return ((-degrees / 1.8) * yMicroSteps * yGearRatio);
}
// Steppers , see AccelStepper documotation for more information
AccelStepper stepperX(1, XAxisPWM, XAxisDIR);
AccelStepper stepperY(1, YAxisPWM, YAxisDIR);
int xMotorSpeed = 0; // Maximum steps per second
int yMotorSpeed = 0; // Maximum steps per second
int xMotorAccel = 0; // Steps per second for acceleration
int yMotorAccel = 0; // Steps per second for acceleration
char strValue[7];
char xStrBuffer[8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
int newValue = 0L; // Holds the new value where the dish should point to
float newXAxis = 0L; // Holds the new value where the dish should point to
int command;
boolean First = true;
void setup() {
// put your setup code here, to run once:
Serial.begin(19200); // Start the Serial communication
while (!Serial);
pinMode(yStopper1, INPUT_PULLUP ); // Initialize the switches inputs
pinMode(yStopper2, INPUT_PULLUP ); // The internal resistors are made active
pinMode(xStopper1, INPUT_PULLUP );
pinMode(xStopper2, INPUT_PULLUP );
pinMode(XYEnable , OUTPUT); // This is the M415C enablen pin
stepperX.setMaxSpeed(xMotorSpeed / 4); // Set the X rotor-motor maximum speed
stepperX.setAcceleration(xMotorAccel); // Set the X rotor-motor acceleration speed
stepperY.setMaxSpeed(yMotorSpeed / 4); // Set the Y rotor-motor maximum speed
stepperY.setAcceleration(yMotorAccel); // Set the Y rotor-motor acceleration speed
digitalWrite(XYEnable, HIGH); // Enable use of M415C controllers
Serial.println("Stappenmotor testprogramma V1");
command = 'h';
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() || First == true) {
readCommand(Serial.read());
First = false;
switch (command) {
case ('h'):
Serial.println("Beschikbare opdrachten:");
Serial.println("h - Deze lijst");
Serial.println("s100 - Aantal pulsen per seconde");
command = ' ';
break;
case ('s'):
xMotorSpeed = atoi(strValue);
Serial.print("Set speed to:");
Serial.print(xMotorSpeed);
Serial.println(" Pulsen per secende.");
command = ' ';
break;
}
}
}
void readCommand(char ch) {
String stringBuffer = " ";
if (ch == '\n' || ch == '\r') {
strValue[index] = 0x00;
stringBuffer = strValue;
stringBuffer.toLowerCase();
index = 0;
command = stringBuffer[0];
for (int i = 1; stringBuffer[i] != 0x00; i++) {
strValue[i - 1] = stringBuffer[i];
}
} else {
strValue[index] = ch;
index ++;
}
}
Thanks