Im trying to do a project were i want to make a stepper go in a cycle where it goes 2000steps forward in speed A and after that distance i need it to go 4000steps forward in speed B and then back 6000 steps in speed C until it hits a hal sensor to indicate home position.
At the same time we doing the cycle i want to make PIN 6 high for about 0.1sec at the start of the cycle
Right after the PIN 6 is set high we want to make a puls of 400Hz for 1sec on PIN 7.
Now i are making the pulses with a delay blink script and that does not work out pretty well....
How should i do this? we are using a 2wire driver for the stepper motor with the stepper.h library
Ok, i have read about it, still don't get the grip about it.
Im new to Arduino programming so can we please break this down.
my script does use different speeds now for one cycle then it just goes back to 1speed over the whole cycle and continuously cycles.
What have i done wrong? and how do i implement the timer interrupt to start the pulsevfunction on PIN 7 after each time the cycle has started over?
this is my code now:
#include <AccelStepper.h>
// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 3);
void setup()
{
stepper1.setMaxSpeed(5000.0);
stepper1.setAcceleration(40000.0);
stepper1.moveTo(25500);
}
void loop()
{
// Change direction at the limits
if (stepper1.distanceToGo() == 0)
{
stepper1.moveTo(-stepper1.currentPosition());
}
else if (stepper1.distanceToGo() == 16000) {
stepper1.setMaxSpeed(40000); }
stepper1.run();
}
sirch:
You need to use a timer interrupt. Using Delay stops all your code until the delay is over
No, you need to use the "millis" function, which itself makes use of a timer interrupt in the manner of the blink without delay" example. Advising beginners to use interrupts is very unhelpful, as they are rarely necessary or at all appropriate to most tasks.
OK, this is non-intuitive to beginners, but absolutely necessary so start by studying that code and someone less fatigued than myself at this time of night will no doubt guide you further.
And - OP,
Please go back to your posting, select "modify" on your message, highlight the code section and click on the "#" icon above the edit window to mark it up as code.
Makes it much easier to read - and to reply to your query.
I agree that interrupts should be avoided as far as possible and are not called for here.
There is another thread running at the moment with very similar requirements - are you working on the same project? I suggest you follow the same approach recommended there, running the stepper motor asynchronously and using a finite state machine to control what it does. This leaves your sketch free to monitor limit switches, make pins pulse and so on if it needs to as well.