I will go ahead and share what i am trying to accomplish to make it easier. I am building a autonomous tool to help me wrap a speaker voice coil for a project. I already have a simplified version that just uses a gear motor but i want to use 2 stepper motors for more precision and to be autonomous. I am using a uno and 2 a4988 drivers with 2 stepper motors. One will be turning the shaft and the other will be guiding a wire guide piece on a screw and linear rails. Ultimately i want to be able to position the motors and wire and hit a button and have the motors do all the work because making them by hand is just no fun. So i need to be able to have the main motor turn at a set speed and x amount of full rotations then switch directions and go x amount back and stop, but while the main motor is spinning, i need the screw guide motor to turn x amount of rotations for every rotation of main motor and switch directions with main motor and go back to were it started. The screw motor will need to turn slower than the main motor but i need to figure out through trial and error how much slower it needs to be, it should be the pitch of the threads and width of the wire i am using. I have wrote some simple code that turns the motor 5 rotations one way then 5 the other at the press of a button. But trying to make this code run 2 motors at different speeds is impossible because i am using a delay to set the speed of the steppers so no matter what both steppers will turn at the same speed determined by the sum of the delays. So how could i set the speed of the motors and then the delta of the screw to main motor in rotations or steps?
const int stepPin1 = 3;
const int dirPin1 = 4;
const int stepPin2 = 5;
const int dirPin2 = 6;
int button = 7;
void setup() {
pinMode(stepPin1,OUTPUT);
pinMode(dirPin1,OUTPUT);
pinMode(button,INPUT);
pinMode(stepPin2,OUTPUT);
pinMode(dirPin2,OUTPUT);
}
void loop() {
if(digitalRead(button) == HIGH) {
digitalWrite(dirPin1,HIGH);
for(int x = 0; x < 5; x++) {
for(int x = 0; x < 3200; x++) {
digitalWrite(stepPin1,HIGH);
delayMicroseconds(100);
digitalWrite(stepPin1,LOW);}}
delay(10);
digitalWrite(dirPin1,LOW);
for(int x = 0; x < 5; x++){
for(int x = 0; x < 3200; x++) {
digitalWrite(stepPin1,HIGH);
delayMicroseconds(100);
digitalWrite(stepPin1,LOW);}}}}