Kodingan Nich
Kodingan Nich
h>
#include <AFMotor.h>
// Should be right for DVD steppers, but is not too important here
Servo penServo;
// Initialize steppers for X- and Y-axis using this Arduino pins for the L293D H-bridge
AF_Stepper myStepperY(stepsPerRevolution,1);
AF_Stepper myStepperX(stepsPerRevolution,2);
struct point {
float x;
float y;
float z;
};
// Current position of plothead
float StepInc = 1;
int StepDelay = 0;
float Xmin = 0;
float Ymin = 0;
float Zmin = 0;
float Zmax = 1;
// G1 for moving
/**********************
***********************/
void setup() {
// Setup
Serial.begin( 9600 );
penServo.attach(penServoPin);
penServo.write(penZUp);
delay(100);
// Decrease if necessary
myStepperX.setSpeed(600);
myStepperY.setSpeed(600);
// TBD
// Notifications!!!
Serial.println("Mini CNC Plotter alive and kicking!");
Serial.print(Xmin);
Serial.print(" to ");
Serial.print(Xmax);
Serial.println(" mm.");
Serial.print(Ymin);
Serial.print(" to ");
Serial.print(Ymax);
Serial.println(" mm.");
/**********************
***********************/
void loop()
delay(100);
char c;
int lineIndex;
lineIndex = 0;
lineSemiColon = false;
lineIsComment = false;
while (1) {
// Serial reception - Mostly from Grbl, added semicolon support
while ( Serial.available()>0 ) {
c = Serial.read();
if (verbose) {
Serial.println( line );
lineIndex = 0;
else {
lineIsComment = false;
lineSemiColon = false;
Serial.println("ok");
else {
else {
else if ( c == '(' ) { // Enable comments flag and ignore all characters until ')' or EOL.
lineIsComment = true;
}
else if ( c == ';' ) {
lineSemiColon = true;
lineIsComment = false;
lineSemiColon = false;
else {
line[ lineIndex++ ] = c;
int currentIndex = 0;
newPos.x = 0.0;
newPos.y = 0.0;
// Needs to interpret
// G1 for moving
// G4 P300 (wait 150ms)
// G1 X60 Y30
// G1 X30 Y50
case 'U':
penUp();
break;
case 'D':
penDown();
break;
case 'G':
buffer[0] = line[ currentIndex++ ]; // /!\ Dirty - Only works with 2 digit commands
// buffer[2] = '\0';
buffer[1] = '\0';
case 1:
char* indexX = strchr( line+currentIndex, 'X' ); // Get X/Y position in the string (if any)
if ( indexY <= 0 ) {
newPos.y = actuatorPos.y;
}
newPos.x = actuatorPos.x;
else {
indexY = '\0';
drawLine(newPos.x, newPos.y );
// Serial.println("ok");
actuatorPos.x = newPos.x;
actuatorPos.y = newPos.y;
break;
break;
case 'M':
buffer[0] = line[ currentIndex++ ]; // /!\ Dirty - Only works with 3 digit commands
buffer[3] = '\0';
case 300:
// Serial.println("ok");
if (Spos == 30) {
penDown();
}
if (Spos == 50) {
penUp();
break;
Serial.print( actuatorPos.x );
Serial.println( actuatorPos.y );
break;
default:
Serial.println( buffer );
/*********************************
**********************************/
Serial.print(x1);
Serial.print(",");
Serial.print(y1);
Serial.println("");
x1 = Xmax;
x1 = Xmin;
y1 = Ymax;
y1 = Ymin;
if (verbose)
Serial.print(Xpos);
Serial.print(",");
Serial.print(Ypos);
Serial.println("");
}
if (verbose)
Serial.print(x1);
Serial.print(",");
Serial.print(y1);
Serial.println("");
x1 = (int)(x1*StepsPerMillimeterX);
y1 = (int)(y1*StepsPerMillimeterY);
float x0 = Xpos;
float y0 = Ypos;
long dx = abs(x1-x0);
long dy = abs(y1-y0);
long i;
long over = 0;
myStepperX.onestep(sx,STEP);
over+=dy;
if (over>=dx) {
over-=dx;
myStepperY.onestep(sy,STEP);
delay(StepDelay);
else {
myStepperY.onestep(sy,STEP);
over+=dx;
if (over>=dy) {
over-=dy;
myStepperX.onestep(sx,STEP);
delay(StepDelay);
if (verbose)
Serial.print("dx, dy:");
Serial.print(dx);
Serial.print(",");
Serial.print(dy);
Serial.println("");
if (verbose)
Serial.print("Going to (");
Serial.print(x0);
Serial.print(",");
Serial.print(y0);
Serial.println(")");
delay(LineDelay);
Xpos = x1;
Ypos = y1;
// Raises pen
void penUp() {
penServo.write(penZUp);
delay(penDelay);
Zpos=Zmax;
digitalWrite(15, LOW);
digitalWrite(16, HIGH);
if (verbose) {
Serial.println("Pen up!");
// Lowers pen
void penDown() {
penServo.write(penZDown);
delay(penDelay);
Zpos=Zmin;
digitalWrite(15, HIGH);
digitalWrite(16, LOW);
if (verbose) {
Serial.println("Pen down.");