// For: https://forum.arduino.cc/t/using-state-of-button-to-stop-for-loop/1041864
// Wokwi project: https://wokwi.com/projects/345499610234487379
// 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;
}
}
}
}