Hi
I am building a small project where I want 2x STEPPER MOTORS to control a "Magnetic Sketch Board" with 2 Axis. See picture
I want the STEPPER MOTORS to draw a Circle using the 2 Axis simultaneously.
1 STEPPER MOTOR shall follow Sinus Coordinates
1 STEPPER MOTOR shall follow Co-Sinus Coordinates
I just made the CODE for drawing a Quarter of a Circle (509/2038 Steps).
The STEPPER MOTOR I ask to follow "Sinus Coordinates" does not run.
The STEPPER MOTOR I ask to follow "Co-Sinus Coordinates" seems to run the (0-90-0)deg path fine.
Am I doing something wrong with my Syntax using Sinus...?
#include <AccelStepper.h> //#include <MultiStepper.h> //Define Constants: #define FULLSTEP 4 #define HALFSTEP 8 //Define Motor Pins (2x Stepper Motors) #define motorPin1 2 #define motorPin2 3 #define motorPin3 4 #define motorPin4 5 #define motorPin5 6 #define motorPin6 7 #define motorPin7 8 #define motorPin8 9 #define t 509 // 1/4 * 2038 (quater of a full rotation) #define position1 cos( (PI/2)/509 )*t #define position2 sin( (PI/2)/509 )*t #define speed1 500 #define speed2 500 #define acc1 50. #define acc2 50. #define maxS 1000. AccelStepper stepper1(HALFSTEP,motorPin1,motorPin3,motorPin2,motorPin4); AccelStepper stepper2(HALFSTEP,motorPin5,motorPin7,motorPin6,motorPin8); void setup() { // put your setup code here, to run once: stepper1.setMaxSpeed(maxS); stepper1.setAcceleration(acc1); stepper1.setSpeed(speed1); stepper1.moveTo(position1); stepper2.setMaxSpeed(maxS); stepper2.setAcceleration(acc2); stepper2.setSpeed(speed2); stepper2.moveTo(position2); } void loop() { // put your main code here, to run repeatedly: if (stepper1.distanceToGo()==0) stepper1.moveTo(-stepper1.currentPosition()); if (stepper2.distanceToGo()==0) stepper2.moveTo(-stepper2.currentPosition()); stepper1.run(); stepper2.run(); }
@revsbeck
A few points points (as a start)
- you never change t, so your sine and cosine values never change.
- you seem to be using t as 1/4 turn in height, is that what you meant ?
- you seem to be using a constant angle (PI/2)/509
In the loop, you may need to change it to something like ...
for (t=0;t<509;t++) {
stepper1.moveTo(position1);
stepper2.moveTo(position2);
while (stepper1.distanceToGo>0 || stepper2.distanceToGo()>0) {
stepper1.run();
stepper2.run();
} // while
} // for t
These changes introduce a changing value for t, invoke your #defined expression for value and then invokes a while loop to wait for them to run out. Since they both have the same number of steps, they'll run out together anyway
I'm not sure, however, that it will provide the trace you're expecting.
Anything seems possible when you don't know what you're talking about.
Thank You for your hint...... I have built the Code wrong, since "t" is not changing as you mention.
I will try to setup the "For Loop" like you recommend.
Anything seems possible when you don't know what you're talking about.
I finally relieved the Prototype Parts from the Laser Cutter and 3D Printer.
Seems like my "Circle Test" does work as indented...... SINUS part does not run correct.
I will put it aside for a while, and start to make simple test.
- Horizontal Line (Running STEPMOTOR-1) ; (Status=Completed)
- Vertical Line (Running STEPMOTOR-2) ; (Status=Completed)
- Incline Line (Running STEPMOTOR-1+STEPMOTOR-2 at the same time) ; (Status=Completed)
- Draw Triangle
- Draw Square
- Draw Circle
Can you help me with the CODE, so I can draw in sequence, but in one continuous flow step by step.
#4 Draw Triangle
- Horizontal Line
- Vertical Line
- Incline Line
My current Code for #3 is:
#include <AccelStepper.h> //#include <MultiStepper.h> //Define Constants: #define FULLSTEP 4 #define HALFSTEP 8 //Define Motor Pins (2 x Stepper Motors) #define motorPin1 2 #define motorPin2 3 #define motorPin3 4 #define motorPin4 5 #define motorPin5 6 #define motorPin6 7 #define motorPin7 8 #define motorPin8 9 //Define Motor Motion: #define position1 2048*5 #define position2 2048*5 #define speed1 750 #define speed2 750 #define acc1 100. #define acc2 100. #define maxS 1000. AccelStepper stepper1(HALFSTEP,motorPin1,motorPin3,motorPin2,motorPin4); AccelStepper stepper2(HALFSTEP,motorPin5,motorPin7,motorPin6,motorPin8); void setup() { // put your setup code here, to run once: stepper1.setMaxSpeed(maxS); stepper1.setAcceleration(acc1); stepper1.setSpeed(speed1); stepper1.moveTo(position1); stepper2.setMaxSpeed(maxS); stepper2.setAcceleration(acc2); stepper2.setSpeed(speed2); stepper2.moveTo(position2); } void loop() { // put your main code here, to run repeatedly: stepper1.moveTo(position1); stepper2.moveTo(position2); stepper1.run(); stepper2.run(); }
Here's an attempt at what you requested. It compile clean but I can't test it, so it may have a few surprises.
I've added comments to help you understand what I'm doing.
I'm using variable called 'distance' as the root length for all of the commands, you may need to change that upwards because it's basically the number of steps to be moved and 100 may be far too short to show the movement involved clearly.
Please let me know if it doesn't work as intended. I have called it code_3 as in your name above.
Anything seems possible when you don't know what you're talking about.
Hi Will..... Thank you for taking your time writing this code
The "Sketcher" start up and the result is this:
Merry Christmas !
Yes, it definitely looks like you need to change the 'distance' variable larger size.
Do you understand and feel comfortable with being able to draw line segments in 2 dimensions now ?
Anything seems possible when you don't know what you're talking about.
I just made this code, which can draw a triangle.
The problem is that it continue to run the program over and over again....
What is the best way to code a "stop", when it will stop when the last line is done?
This Code can draw a Triangle (2048=stepper 360deg):
#include <AccelStepper.h> #include <MultiStepper.h> //Define Constants: #define FULLSTEP 4 #define HALFSTEP 8 //Define Motor Pins (2 x Stepper Motors) #define motorPin1 2 #define motorPin2 3 #define motorPin3 4 #define motorPin4 5 #define motorPin5 6 #define motorPin6 7 #define motorPin7 8 #define motorPin8 9 AccelStepper stepper1(HALFSTEP,motorPin1,motorPin3,motorPin2,motorPin4); AccelStepper stepper2(HALFSTEP,motorPin5,motorPin7,motorPin6,motorPin8); MultiStepper steppers; void setup() { Serial.begin(9600); stepper1.setMaxSpeed(750); stepper2.setMaxSpeed(750); stepper1.setSpeed(250); stepper2.setSpeed(250); stepper1.setAcceleration(50); stepper2.setAcceleration(50); steppers.addStepper(stepper1); steppers.addStepper(stepper2); } void loop() { long positions[2]; positions[0]=2048*4; positions[1]=0; steppers.moveTo(positions); steppers.runSpeedToPosition(); delay(1000); positions[0]=2048*4; positions[1]=2048*4; steppers.moveTo(positions); steppers.runSpeedToPosition(); delay(1000); positions[0]=0; positions[1]=0; steppers.moveTo(positions); steppers.runSpeedToPosition(); delay(1000); }
Such dedication, working on Xmas day 🙂
Normally, you don't stop looping (that's kinda the point :).
What you can do is put in a huuuuuuge delay at the end of your code so it'll pause long enough for you admire your work and still have time to unplug it before the next loop.
Anything seems possible when you don't know what you're talking about.
Actually, thats an interesting question about how to"stop" the action.
How about setting up a counter (loopCount) initialized to 1 in the declaration section.
Then, in the loop, have ...
while (loopCount==1) {
++loopCount;
.... and then your stuff to be run
}
After the first loop, the loopCount condition will prevent your code from executing again (although it will keep looping).
Anything seems possible when you don't know what you're talking about.