Hi guys,
I’m struggling with what seems like a really simple issue, but despite having trawled the forums and Google I can’t get my head round it!
I’m building a (sort of) printer and have a moving stage actuated by stepper motors, and inkjet printhead controlled by the Tone() function. The code below causes the moving stage to move back and forth in the x direction, with a small y movement at the end of each x stroke. This means the stage moves in a pattern of tightly packed parallel lines. This movement stops once a certain displacement has been reached in the Y direction. Whilst this movement is happening, the Tone() sends a waveform down a BNC cable, causing the printhead to fire at the specified frequency.
When positioned below a stationary printhead, this will mean that a series of parallel lines are printed onto the moving stage.
#include <AccelStepper.h>
#define X_STEP_PIN 54
#define X_DIR_PIN 55
#define X_ENABLE_PIN 38
#define Y_STEP_PIN 60
#define Y_DIR_PIN 61
#define Y_ENABLE_PIN 56
#define Z_STEP_PIN 36
#define Z_DIR_PIN 34
#define Z_ENABLE_PIN 30
#define Trigger_PIN 12
AccelStepper Xaxis(1, X_STEP_PIN, X_DIR_PIN);
AccelStepper Yaxis(1, Y_STEP_PIN, Y_DIR_PIN);
AccelStepper Zaxis(1, Z_STEP_PIN, Z_DIR_PIN);
void setup() {
Xaxis.setMaxSpeed(10000);
Xaxis.setAcceleration(10000);
Xaxis.moveTo(330);
Xaxis.setEnablePin(X_ENABLE_PIN);
Xaxis.setPinsInverted(false,false,true);
Xaxis.enableOutputs();
Yaxis.setMaxSpeed(500);
Yaxis.setAcceleration(10000);
Yaxis.setEnablePin(Y_ENABLE_PIN);
Yaxis.setPinsInverted(false,false,true);
Yaxis.enableOutputs();
Zaxis.setMaxSpeed(10000);
Zaxis.setAcceleration(1000);
Zaxis.moveTo(10000);
Zaxis.setEnablePin(Z_ENABLE_PIN);
Zaxis.setPinsInverted(false,false,true);
Zaxis.enableOutputs();
}
void loop()
{ while (Yaxis.currentPosition() == 660){
Yaxis.stop();
Xaxis.stop();
noTone(Trigger_PIN);}
if (Xaxis.distanceToGo() == 0){
Yaxis.move(33);
Xaxis.moveTo(-Xaxis.currentPosition());}
Xaxis.run();
Yaxis.run();
tone(Trigger_PIN, 1000);
}
The bit I’m having trouble with seems like it should be much simpler than all this but I can’t get it to work. I need to be able to add simple x, y or z movements either before or after this printing sequence so that I can hold the printhead away from the printing area initially, then move it across, do the printing, then move it back.
For example, I’d like my code to be able to do:
move 10000 in x direction
do printing sequence
move -10000 in x direction.
Whenever I try and add anything at the start or end of the loop though everything just goes absolutely crazy! I suspect this is due to a fundamental lack of understanding of what is going on but I’m running out of time and my research so far hasn’t helped.
Any pointers would be greatly anticipated!
Cheers,
Michael