4 stepper motors on CNC Shield moving independently operated by wifi

:o

Hi there,

I'm having difficulty with a project where I'm running 4 NEMA 17 motors on a CNC shield attached to an Arduino Uno operated by a wifi module. I had the wifi module attached but sadly it won't work in the timeframe I'm hoping to finish this project (a week) so I'm using an IR sensor with a remote instead.

Basically my question is, I've separated the 4 motors by finding their dir/step numbers but I can't figure out how to code them in a sequence where they are moving at different speeds and times. Can anyone help?

Below is my code so far;

For the motors:

// Define stepper motor connections and steps per revolution:
#define dirPinX 5
#define stepPinX 2
#define dirPinY 6
#define stepPinY 3
#define dirPinZ 7
#define stepPinZ 4
#define dirPinA 13
#define stepPinA 12

#define enablePin 8

#define stepsPerRevolution 200

void setup() {
// Declare pins as output:
pinMode(stepPinX, OUTPUT);
pinMode(dirPinX, OUTPUT);
pinMode(stepPinY, OUTPUT);
pinMode(dirPinY, OUTPUT);
pinMode(stepPinZ, OUTPUT);
pinMode(dirPinZ, OUTPUT);
pinMode(stepPinA, OUTPUT);
pinMode(dirPinA, OUTPUT);
pinMode(enablePin, OUTPUT);
}

void loop() {
// Set the spinning direction clockwise:
digitalWrite(dirPinX, HIGH);
digitalWrite(dirPinY, HIGH);
digitalWrite(dirPinZ, HIGH);
digitalWrite(dirPinA, HIGH);

digitalWrite(enablePin, LOW);

// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPinX, HIGH);
digitalWrite(stepPinY, HIGH);
digitalWrite(stepPinZ, HIGH);
digitalWrite(stepPinA, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPinX, LOW);
digitalWrite(stepPinY, LOW);
digitalWrite(stepPinZ, LOW);
digitalWrite(stepPinA, LOW);
delayMicroseconds(2000);
}

delay(1000);

// Set the spinning direction counterclockwise:
digitalWrite(dirPinX, LOW);
digitalWrite(dirPinY, LOW);
digitalWrite(dirPinZ, LOW);
digitalWrite(dirPinA, LOW);

// Spin the stepper motor 1 revolution quickly:
for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPinX, HIGH);
digitalWrite(stepPinY, HIGH);
digitalWrite(stepPinZ, HIGH);
digitalWrite(stepPinA, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPinX, LOW);
digitalWrite(stepPinY, LOW);
digitalWrite(stepPinZ, LOW);
digitalWrite(stepPinA, LOW);
delayMicroseconds(1000);
}

delay(1000);

// Set the spinning direction clockwise:
digitalWrite(dirPinX, HIGH);
digitalWrite(dirPinY, HIGH);
digitalWrite(dirPinZ, HIGH);
digitalWrite(dirPinA, HIGH);

// Spin the stepper motor 5 revolutions fast:
for (int i = 0; i < 5 * stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPinX, HIGH);
digitalWrite(stepPinY, HIGH);
digitalWrite(stepPinZ, HIGH);
digitalWrite(stepPinA, HIGH);
delayMicroseconds(500);
digitalWrite(stepPinX, LOW);
digitalWrite(stepPinY, LOW);
digitalWrite(stepPinZ, LOW);
digitalWrite(stepPinA, LOW);
delayMicroseconds(500);
}

delay(1000);

// Set the spinning direction counterclockwise:
digitalWrite(dirPinX, LOW);
digitalWrite(dirPinY, LOW);
digitalWrite(dirPinZ, LOW);
digitalWrite(dirPinA, LOW);

//Spin the stepper motor 5 revolutions fast:
for (int i = 0; i < 5 * stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPinX, HIGH);
digitalWrite(stepPinY, HIGH);
digitalWrite(stepPinZ, HIGH);
digitalWrite(stepPinA, HIGH);
delayMicroseconds(500);
digitalWrite(stepPinX, LOW);
digitalWrite(stepPinY, LOW);
digitalWrite(stepPinZ, LOW);
digitalWrite(stepPinA, LOW);
delayMicroseconds(500);
}

delay(1000);
}

Any help would be greatly appreciated!!<3 Thanks in advance

I can't figure out how to code them in a sequence where they are moving at different speeds and times.

What is it that it is doing, now?

them in a sequence where they are moving at different speeds and times

could mean a lot of different things.

Give at least one example that describes in detail the motion of all four steppermotors.
Or draw a diagram with a time-axle with four lines where each line represents the motion of one steppermotor

best regards Stefan
any newbee can apply the most professional habit from the first line of code they write on their own:
add only ONE thing at a time. Test/debug that ONE thing until that ONE thing works reliable - repeat.
The sad thing is: only the REAL professionals write code this way.

Right now the steppers are moving in unison - the same format sequence of forward and backwards.

aarg:
What is it that it is doing, now?

Right now they are just moving in unison - forwards and backwards. If possible i would like to get them moving 1. halfway clockwise 2. full turn clockwise 3. full turn counter clockwise 4. halfway counter clockwise

OR something like this. And also at different speeds, if also possible.

If anyone knows how to attach an IR sensor to a CNC shield as well, I'd love to see you schematics!

I suspect this would be much easier with the AccelStepper library as it will look after all the housekeeping.

Whether you use the library or develop your own code you will need to get rid of all the delay() and delayMicroseconds(). If you want to create your own code look at how millis() and is used to manage timing in the second example in this Simple Stepper Code

...R

What your description is not specifying is if the motors should move at the same time or one after the other.

This makes a big difference in programming.

If the motors should move at the same time multiple step-signals have to be created at the same time.
This really is a task for a stepper-library which takes care of the details how fast the step-signal-pulses must be created etc.

So as we really can't look inside your head provide a more detailed description of how the motors should move

best regards Stefan

StefanL38:
So as we really can't look inside your head provide a more detailed description of how the motors should move

[/quote

This is true. Thanks for the help so far very much appreciated.

The motors are to move at the same time, but slower/faster than each other and at different step per revolution.

I'm not that savvy with all the terminology of coding as I'm still learning, but this is what I know thus far that I'm struggling to continue further with.

desperate cry for help

OK if the motors should run all at the same ime at different speeds and a different number of steps the basic concept is to have only one loop that creates the steps for all four motors "just in time"

If you want to finish this project in just a week cancel all dates and apointments and prepare to have work for 6-10 hours every day. This is not caused by writing 10000 lines of code. It is caused by the learning-cuve you have to drive up. If you are a natural software-developper-talent and are finsihed after 5 hours big congratulations.

A second approach is to use timer-interrupts. I have coded this approach for a single steppermotor. I (yet) don't know if this works for four stepper-motors at the same time. Which means is it possible to setup 4 independent timer-interrupts to run in parallel.

The one loop-approach
The basic principle is to have one loop that is running at high speed and do multiple checkings if the right amount of time has passed by since last run of the loop
and if yes create a stepper-pulse. second thing to check is how many pulses have been created and if pre-set amount of pulses has been created stop creating pulses.

This has to be done for each of the four stepper-motors. With seperate variables for each stepper-motor.

the loop is running at high speed but with a exact timing let's say every 5 milliseconds.

With each run of the loop a counter is incremented by 1. This means every 5 milliseconds the counter increases by 1

0 ms counter 0
5 ms counter 1
10 ms counter 2
15 ms counter 3
20 ms counter 4
etc. etc. etc.

Now if a step-pulse should be created every 15 ms there has to be an if-condition that checks is counter a mutliple of 3
because if counter is 3, 6, 9, 12 the time of 15, 30, 45, 60 milliseconds has passed by.

If your second motor shall run at a different speed you check for a different multiple

example every 100 milliseconds a step-pulse should be cerated
100 milliseconds means 100/ 5 = 20 each time the counter-variable is a mutliple of 20 create a step-pulse
so if counter is 20, 40, 60, 80 the time of 100, 200, 300, 400 milliseconds has passed by

At start you preset the number of steps each motor should move. And each time a step is created a variable
let's call it NumberOfSteps get's decremented by 1. Each time a step should be created an if-condition is checked
if (NumberOfSteps > 0) ==> create step
this means when the variable reaches 0 the step-creation stops
and starts only if you setup a new value for variable NumberOfSteps

Now creating a step every 5 milliseconds means 200 steps per second which would be one rotation per second in fullstep-mode or 60 rotations per minute. Not very fast.
First thing to examine is how much time does all the incrementing/decrementing and value-checking need?
of course additional commands need additional time. Which is slowing down the run-time of the loop.

So one thing to clear is how fast must your fasted steppermotor run?
If it has to be very very fast including microstepping with 8000 pulses per revolution an arduino is simply not fast enough.

If you have difficulties to wrap your head around my explanations. This is normal. That's the reason why I said you will need pretty much time to finsih this project. You would have to be extra-ordinary big talented to understand all this right away writing down the code and be finished in two hours.

It is normal that this takes more time.
So first step is to learn how timing with the function millis() / micros() work.
If millis()is to slow for your requierements tests with the fucntion micros() has to be made to see how fast the loop can be using the function micros() in a classical "loop()"

If this is still too slow using a timer-interrupt must be considered and beeing tested or calculated how fast this can go.

So please answer the question how fast have your steppermotors to run?
Anyway what is the main-purpose of the steppers running?
Is this an academic exercise "make 4 steppermotors move at different speeds at the same time" or is there a real application behind it?

best regards Stefan