An if will not do because of the blocking structure of your programm. Remember, if you call stepper1.writeSteps( -steps );
The stepper will start moving, and your sketch will run on in parallel. You can ask at which position the stepper is by means of the method stepper1.readSteps().
But of course if you write if (stepper1.readSteps() == 4400 )
The stepper will not yet be there when you execute that if. So you again have to deal with while to wait until it has reached that position.
But as I understand 4400 steps is the endposition? Then the stepper is there when it stops moving.
Some comments in the code what should happen at the various points would be helpful. The names of your relaispins seem not to be very meaningful.
Yes your correct, Ive tried an if and it didn’t work, my issue now is not with any stepper motors, it’s with the claw which is a brushed 12v motor, it works off 4 relays, 2 for close and 2 for open, the ram also working off a 12v by a solenoid, this is why Ive put delays for them, when the arm gets to the tin opener in the video, the claw needs to open releasing the can, then the tin opener can freely open the can, once the can is opened (9 seconds later) the claw needs to close again and the ram will lift the tin opener, I have no clue as to how they can both be worked at the same time.
while (digitalRead(button2) == HIGH)
{
}
digitalWrite(relay1, LOW); //close claw positive
digitalWrite(relay2, LOW); //close claw negative
delay(650);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
stepper1.writeSteps( -steps );
delay(1650);
stepper2.writeSteps( -300 );
while ( stepper1.moving() || stepper2.moving() ); //1 move arm to tin opener, 2 turn claw slightly
digitalWrite(signal_pin, HIGH); // push ram up for tin opener this is where I need relays 1,2,3,4 to work at the same time as this delay
delay(9000);
digitalWrite(signal_pin, LOW); //release ram
delay(1500);
stepper1.doSteps( 1800 ); //move arm back 1800 steps
while ( stepper1.moving() );
stepper2.doSteps( -4000 ); //turn arm nearly 180 degrees
while ( stepper2.moving() );
delay(400);
stepper2.doSteps( 4300 ); //turn arm back to vertical
while ( stepper2.moving() );
stepper1.writeSteps( 0 ); //move arm back to start
digitalWrite(relay3, LOW); //open claw positive
digitalWrite(relay4, LOW); //open claw negative
delay(600);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
But isn't all that controlled by switching of relays? Why can't you switch the relays on/off one after the other as needed with or without delays in between?
Maybe it’s not coming across as I mean it to, the 4 relays (2 at a time) only need to work a motor for 0.6 of a second (to open the claw) at the same time the ram needs to be pushing for 9 seconds (to work the tin opener) these both need to start at the same time.
I didn’t realise what you meant when you spoke about blocking, I understand now, the arduino can only do one thing at a time when it has a delay involved, so I’m looking at adding metro.h from the library, could this be a solution to my problem
Activate all relays for motor and ram. After 0.6 seconds stop the motor and than after additonal 8.4 seconds stop the ram. I still don't understand the problem...
while (digitalRead(button2) == HIGH)
{
}
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
delay(650);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
stepper1.writeSteps( -steps ); // starting first stepper.
// Direction depends from motor wiring and mechanics. Change to -steps to invert direction.
delay(1000);
stepper2.writeSteps( -350 ); // starting second stepper. Steps may need to be adjusted
while ( stepper1.moving() || stepper2.moving() ); // wait until both steppers have reached their target position
digitalWrite(signal_pin, HIGH);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
delay(600);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
delay(7400);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(signal_pin, LOW);
delay(650);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
delay(1500);
stepper1.doSteps( 1800 );
while ( stepper1.moving() );
stepper2.doSteps( -4000 );
while ( stepper2.moving() );
delay(400);
stepper2.doSteps( 4350 );
while ( stepper2.moving() );
stepper1.writeSteps( 0 );
delay(400);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
delay(550);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
}
Why couldn’t I work that out
Thanks you so much once again MicroBahner without you this code would never ever have been finished, I’ll post a video of it completed shortly,
Thank you, thank you, thank you again MicroBahner
Thanks, like I said I couldn’t have done it without your superior intelligence, I was just trying to do a video but I had a few hardware issues and the air running out so I’ll post one tomorrow for you, I’m very happy with it now, thanks you
When moving stepper1, I would not use doSteps, but writeSteps. Each position of the swivel arm has a specific step position between 0 and -4400. and so you can exactly position it ( as you do it with the first and last movement ).
And you can ask where it is. For example, when you open the claw to drop the can into the trash. It is better to do that when the swivel has reached the correct position, instead of relying on delay time.