// Define stepper motor connections and steps per revolution:
#define dirPin 10
#define stepPin 9
#define zeroPin 8
const int theta_relative[] = {7,7,7,7,7,7,7,7,7,7,6,6,6,6,6,6,6,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,4,4,4,4,4,4,4,4,3,3,3,3,3,3,2,2,2,2,2,2,2,1,1,1,1,1,1,0,0,0,-0,-0,-1,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2,-2,-2,-3,-3,-3,-3,-3,-3,-4,-4,-4,-4,-4,-4,-4,-4,-5,-5,-5,-5,-5,-5,-5,-5,-5,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,-1,-1,-1,-0,-0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7};
const long int frame[] = {571,571,571,571,571,571,571,571,571,571,667,667,667,667,667,667,667,667,667,667,667,667,667,667,800,800,800,800,800,800,800,800,800,1000,1000,1000,1000,1000,1000,1000,1000,1333,1333,1333,1333,1333,1333,2000,2000,2000,2000,2000,2000,2000,4000,4000,4000,4000,4000,4000,100000,100000,100000,100000,100000,4000,4000,4000,4000,4000,4000,2000,2000,2000,2000,2000,2000,2000,1333,1333,1333,1333,1333,1333,1000,1000,1000,1000,1000,1000,1000,1000,800,800,800,800,800,800,800,800,800,667,667,667,667,667,667,667,667,667,667,667,667,667,667,571,571,571,571,571,571,571,571,571,571,571,571,571,571,571,571,571,571,571,571,667,667,667,667,667,667,667,667,667,667,667,667,667,667,800,800,800,800,800,800,800,800,800,1000,1000,1000,1000,1000,1000,1000,1000,1333,1333,1333,1333,1333,1333,2000,2000,2000,2000,2000,2000,2000,4000,4000,4000,4000,4000,4000,100000,100000,100000,100000,100000,4000,4000,4000,4000,4000,4000,2000,2000,2000,2000,2000,2000,2000,1333,1333,1333,1333,1333,1333,1000,1000,1000,1000,1000,1000,1000,1000,800,800,800,800,800,800,800,800,800,667,667,667,667,667,667,667,667,667,667,667,667,667,667,571,571,571,571,571,571,571,571,571,571};
int no_of_steps;
bool buttonState;
void setup() {
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
int x = 1;
for(int i = 0; i < 250 ; i = i + x){
if (theta_relative[i] > 0) {
no_of_steps = theta_relative[i];
digitalWrite(dirPin, LOW);
while (no_of_steps > 0) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(0.75*frame[i]);
digitalWrite(stepPin, LOW);
delayMicroseconds(0.25*frame[i]);
no_of_steps = no_of_steps - 1;
}
}
else {
no_of_steps = abs(theta_relative[i]);
digitalWrite(dirPin, HIGH);
while (no_of_steps > 0) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(0.75*frame[i]);
digitalWrite(stepPin, LOW);
delayMicroseconds(0.25*frame[i]);
no_of_steps = no_of_steps - 1;
}
}
}
}
Hello,
My experiment includes controlling a stepper motor to perform sinusoidal motion. I have achieved this but I would now like to wire a button to stop the execution of the for loop in my code seen above. When this button is pressed I would like the for loop to finish its current iteration and then stop while the button is supressed. Once released it would continue the for loop as normal. The reasoning for this is because I would like to finish the for loop so the motor can return back to its zero position so I dont have to recalibrate its position when I stop the motion. I am having trouble with what to do. I have tried while and if loops to check the state of the button and to stop the motion but achieved nothing. Would appreciate any help with this.
Thanks