Update
Here the update regarding the code I tried to program. I have a stepper with an attached disc with little holes every 45 degrees where a microswitch can rest into (hole is the exact size of the microswitch; and be in off position then). I would like the stepper to move 45 degree (until hole 1) and then (as a first step) wait for 3 seconds and then move another 45 degree to hole and stop etc.
As a next step I would like the same to be controlled via serial from another software. So I can send to serial a "move" command and then the same as described above will happen once the Arduino get the "move" command.
Below my attempt. It does do the homing, but not very smoothly and the next step (just moving 3 seconds) is a very rough movement. Here my qestions:
- How can I make this move smoothly ?
- How should I adapt this code to make the stepper stop once it is in the hole ?
- How can I make the microswitch get out of the hole after it rests in there ?
Thank you very much for your help !
Code
#include "AccelStepper.h"
int M2dirpin = 4; //Motor Y direction pin
int M2steppin = 5; //Motor Y step pin
int M2en=12; //Motor Y enable pin
#define home_switch 9 // Pin 9 connected to Home Switch (MicroSwitch)
// AccelStepper Setup
AccelStepper stepperX(1, 5, 4); // (1,2,3) = (Dedicated Board, STEP, DIR)
// Stepper Travel Variables
long initial_homing = -1; // Used to Home Stepper at startup
void setup() {
pinMode(home_switch, INPUT_PULLUP);
pinMode(M2dirpin,OUTPUT);
pinMode(M2steppin,OUTPUT);
pinMode(M2en,OUTPUT);
digitalWrite(M2en,LOW);
Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds
stepperX.setMaxSpeed(50.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(50.0); // Set Acceleration of Stepper
// Start Homing procedure of Stepper Motor at startup
Serial.print("Stepper is Homing . . . . . . . . . . . ");
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepperX.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepperX.run(); // Start moving the stepper
delay(20);
}
stepperX.setCurrentPosition(0); // Set the current position as zero for now
stepperX.setMaxSpeed(50.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(50.0); // Set Acceleration of Stepper
initial_homing = 1;
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
stepperX.moveTo(initial_homing);
stepperX.run();
initial_homing++;
delay(20);
}
stepperX.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
// Move 10 steps forward from the limit switch (to get out of the switch)
while (stepperX.currentPosition() != 10)
{
stepperX.setSpeed(10);
stepperX.run();
}
}
void loop()
{
stepper1();
}
void stepper1()
{
stepperX.move(22);
unsigned long startMillis = millis();
while( millis()-startMillis<3000 )
{
stepperX.run();
}
}
Old question: I apologize for the high level, pseudo code question, but I am trying to understand conceptional how I can achieve the following:
I have 3 steppers (1 circular motion, 2 linear motion) which I would like to stop every time at the exact same position. The first stepper should rotate e.g. 45 degrees and stop, after which the second stepper should push down (like a linear actuator) stay at that position for a certain time and move back to the initial position, after that the 3rd stepper should also (linear motion) move to certain position and return. These three steps would than repeat for a very long time.
Do Steppers accumulate errors in position and if yes how can I make sure that this is not happening ?
I read abut "homing" and tried microswitches, but wasn't able to combine it with the three stepper setup. Also 3D printers should always stop exactly at the same place and therefore I assumed there must be a solution for this.
Is there a template code I can start from that is similar to the setup described ?
How can I make sure these steps are done strictly after each other.
Again I apologize for the high level question, but all my attempts so far to solve this failed.
Thank you !
How can I make sure these steps are done strictly after each other.
..... i think that you need to run a few example sketches in the Arduino IDE to get a feel for how a microcontroller operatesall my attempts so far to solve this failed
..... please add the code which you tried and a connection diagram to your post