Devices & Components
1
Arduino Uno Rev3
1
Servo Module (Generic)
1
Breadboard (generic)
2
Dual H-Bridge motor drivers L293D
2
DVD step mottors
1
Jumper wires (generic)
Software & Tools
1
Processing
1
InkScape
Arduino IDE
Project description
Code
gcode sender
processing
1import java.awt.event.KeyEvent; 2import javax.swing.JOptionPane; 3import processing.serial.*; 4 5Serial port = null; 6 7String portname = null; 8 9boolean streaming = false; 10float speed = 0.001; 11String[] gcode; 12int i = 0; 13 14void openSerialPort() 15{ 16 if (portname == null) return; 17 if (port != null) port.stop(); 18 19 port = new Serial(this, portname, 9600); 20 21 port.bufferUntil('\ 22'); 23} 24 25void selectSerialPort() 26{ 27 String result = (String) JOptionPane.showInputDialog(frame, 28 "Select the serial port that corresponds to your Arduino board.", 29 "Select serial port", 30 JOptionPane.QUESTION_MESSAGE, 31 null, 32 Serial.list(), 33 0); 34 35 if (result != null) { 36 portname = result; 37 openSerialPort(); 38 } 39} 40 41void setup() 42{ 43 size(300, 100); 44 openSerialPort(); 45} 46 47void draw() 48{ 49 background(155); 50 fill(0); 51 int y = 24, dy = 12; 52 text("p: select serial port", 12, y); y += dy; 53 text("g: stream a g-code file", 12, y); y += dy; 54 text("x: stop streaming g-code", 12, y); y += dy; 55 text("c: resume streaming g-code", 12, y); y += dy; 56 text("current serial port: " + portname, 12, y); y -= dy; 57} 58 59void keyPressed() 60{ 61 if (!streaming) { 62 if (key == 'p') selectSerialPort(); 63 if (key == 'g') { 64 gcode = null; i = 0; 65 File file = null; 66 println("Loading file..."); 67 selectInput("Select a file to process:", "fileSelected", file); 68 } 69 } 70 71 if (key == 'x') streaming = false; 72 if (key == 'c') streaming = true; stream(); 73} 74 75void fileSelected(File selection) { 76 if (selection == null) { 77 println("Window was closed or the user hit cancel."); 78 } else { 79 println("User selected " + selection.getAbsolutePath()); 80 gcode = loadStrings(selection.getAbsolutePath()); 81 if (gcode == null) return; 82 streaming = true; 83 sendSize(); 84 } 85} 86 87void sendSize(){ 88 int total = 0; 89 for(String string : gcode){ 90 if(string.length()>0){ 91 if(string.charAt(0)=='G'||string.charAt(0)=='M'){ 92 total++; 93 } 94 } 95 } 96 if(total<10){ 97 port.write("C000"+total+'\ 98'); 99 }else if(total<100){ 100 port.write("C00"+total+'\ 101'); 102 }else if(total<1000){ 103 port.write("C0"+total+'\ 104'); 105 }else if (total <10000){ 106 port.write("C"+total+'\ 107'); 108 }else { 109 port.write("C9999"+'\ 110'); 111 } 112 stream(); 113} 114 115 116void stream() 117{ 118 if (!streaming) return; 119 120 while (true) { 121 if (i == gcode.length) { 122 streaming = false; 123 return; 124 } 125 126 if (gcode[i].trim().length() == 0) i++; 127 else break; 128 } 129 130 println(gcode[i]); 131 port.write(gcode[i] + '\ 132'); 133 i++; 134} 135 136void serialEvent(Serial p) 137{ 138 String s = p.readStringUntil('\ 139'); 140 println(s.trim()); 141 142 if (s.trim().startsWith("ok")) stream(); 143 if (s.trim().startsWith("error")) stream(); // XXX: really? 144} 145
CNC
arduino
1#include <Servo.h> 2#include <Stepper.h> 3#include <Wire.h> 4#include <LiquidCrystal_I2C.h> 5 6#define LINE_BUFFER_LENGTH 512 7 8LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 9float total = 1.0; 10String lcdText = ""; 11String percentText = ""; 12float cont = 0.0; 13float percent = 0.0; 14 15 16 17// Servo position for Up and Down 18const int penZUp = 30; 19const int penZDown = 120; 20 21// Servo on PWM pin 8 22const int penServoPin = 8; 23 24const int stepsPerRevolution = 20; 25 26Servo penServo; 27 28// Initialize steppers for X- and Y-axis using this Arduino pins for the L293D H-bridge 29Stepper myStepperY(stepsPerRevolution, 9,12,10,13); 30Stepper myStepperX(stepsPerRevolution, 7,4,3,6); 31 32struct point { 33 float x; 34 float y; 35 float z; 36}; 37 38// Current position of plothead 39struct point actuatorPos; 40 41// Drawing settings, should be OK 42float StepInc = 1; 43int StepDelay = 0; 44int LineDelay = 50; 45int penDelay = 50; 46 47// Calculate steps per mm. Enter here. 48float StepsPerMillimeterX = 8.0; 49float StepsPerMillimeterY = 8.0; 50 51// Drawing robot limits, in mm 52float Xmin = 0; 53float Xmax = 22; 54float Ymin = 0; 55float Ymax = 22; 56float Zmin = 0; 57float Zmax = 1; 58 59float Xpos = Xmin; 60float Ypos = Ymin; 61float Zpos = Zmax; 62 63// Set to true to get debug output. 64boolean verbose = true; 65 66// Needs to interpret 67// G1 for moving 68// G4 P300 (wait 150ms) 69// M300 S30 (pen down) 70// M300 S50 (pen up) 71// Discard anything with a ( 72// Discard any other command! 73 74/********************** 75 * void setup() - Initialisations 76 ***********************/ 77void setup() { 78 // Setup 79 Serial.begin( 9600 ); 80 lcd.begin (16,2); 81 lcd.setBacklight(HIGH); 82 83 penServo.attach(penServoPin); 84 penServo.write(penZUp); 85 delay(200); 86 87 // Decrease if necessary 88 myStepperX.setSpeed(300); 89 myStepperY.setSpeed(300); 90 91 // Set & move to initial default position 92 // TBD 93 94 // Notifications!!! 95 Serial.println("Ready to print"); 96 lcd.clear(); 97 lcd.setCursor(0,0); 98 lcd.print("Ready to print"); 99} 100 101 102void loop() 103{ 104 delay(200); 105 char line[ LINE_BUFFER_LENGTH ]; 106 char c; 107 int lineIndex; 108 bool lineIsComment, lineSemiColon; 109 110 lineIndex = 0; 111 lineSemiColon = false; 112 lineIsComment = false; 113 114 115 116 while (1) { 117 118 // Serial reception 119 while ( Serial.available()>0 ) { 120 121 c = Serial.read(); 122 if (( c == '\ 123') || (c == '\ ') ) { 124 if ( lineIndex > 0 ) { 125 line[ lineIndex ] = '\\0'; 126 if (verbose) { 127 Serial.print( "Received : "); 128 Serial.println( line ); 129 } 130 processIncomingLine( line, lineIndex ); 131 lineIndex = 0; 132 } 133 134 lineIsComment = false; 135 lineSemiColon = false; 136 Serial.println("ok"); 137 } 138 else { 139 if ( (lineIsComment) || (lineSemiColon) ) { 140 if ( c == ')' ) lineIsComment = false; 141 } 142 else { 143 if ( c == '(' ) { 144 lineIsComment = true; 145 } 146 else if ( c == ';' ) { 147 lineSemiColon = true; 148 } 149 else if ( lineIndex >= LINE_BUFFER_LENGTH-1 ) { 150 Serial.println( "ERROR - lineBuffer overflow" ); 151 lineIsComment = false; 152 lineSemiColon = false; 153 } 154 else if ( c >= 'a' && c <= 'z' ) { 155 line[ lineIndex++ ] = c-'a'+'A'; 156 } 157 else { 158 line[ lineIndex++ ] = c; 159 } 160 } 161 } 162 } 163 } 164} 165 166void processIncomingLine( char* line, int charNB ) { 167 int currentIndex = 0; 168 char buffer[ 64 ]; 169 struct point newPos; 170 171 newPos.x = 0.0; 172 newPos.y = 0.0; 173 174 // G1 for moving 175 // G1 X60 Y30 176 // G1 X30 Y50 177 // M300 S30 (pen down) 178 // M300 S50 (pen up) 179 180 while( currentIndex < charNB ) { 181 switch ( line[ currentIndex++ ] ) { 182 case 'G': 183 buffer[0] = line[ currentIndex++ ]; 184 // buffer[1] = line[ currentIndex++ ]; 185 // buffer[2] = '\\0'; 186 buffer[1] = '\\0'; 187 188 switch ( atoi( buffer ) ){ 189 case 0: 190 case 1: 191 char* indexX = strchr( line+currentIndex, 'X' ); 192 char* indexY = strchr( line+currentIndex, 'Y' ); 193 if ( indexY <= 0 ) { 194 newPos.x = atof( indexX + 1); 195 newPos.y = actuatorPos.y; 196 } 197 else if ( indexX <= 0 ) { 198 newPos.y = atof( indexY + 1); 199 newPos.x = actuatorPos.x; 200 } 201 else { 202 newPos.y = atof( indexY + 1); 203 indexY = '\\0'; 204 newPos.x = atof( indexX + 1); 205 } 206 drawLine(newPos.x, newPos.y ); 207 actuatorPos.x = newPos.x; 208 actuatorPos.y = newPos.y; 209 break; 210 } 211 cont++; 212 lcdPrint(); 213 break; 214 case 'M': 215 buffer[0] = line[ currentIndex++ ]; 216 buffer[1] = line[ currentIndex++ ]; 217 buffer[2] = line[ currentIndex++ ]; 218 buffer[3] = '\\0'; 219 switch ( atoi( buffer ) ){ 220 case 300: 221 { 222 char* indexS = strchr( line+currentIndex, 'S' ); 223 float Spos = atof( indexS + 1); 224 if (Spos == 30) { 225 penDown(); 226 } 227 if (Spos == 50) { 228 penUp(); 229 } 230 break; 231 } 232 case 114: 233 Serial.print( "Absolute position : X = " ); 234 Serial.print( actuatorPos.x ); 235 Serial.print( " - Y = " ); 236 Serial.println( actuatorPos.y ); 237 break; 238 default: 239 Serial.print( "Command not recognized : M"); 240 Serial.println( buffer ); 241 } 242 cont++; 243 lcdPrint(); 244 break; 245 case 'C': 246 Serial.println("flag"); 247 buffer[0] = line[ currentIndex++ ]; 248 buffer[1] = line[ currentIndex++ ]; 249 buffer[2] = line[ currentIndex++ ]; 250 buffer[3] = line[ currentIndex++ ]; 251 buffer[4] = '\\0'; 252 total = atoi(buffer); 253 254 break; 255 } 256 } 257 258 259 260} 261 262 263/********************************* 264 * Draw a line from (x0;y0) to (x1;y1). 265 * Bresenham algo from https://www.marginallyclever.com/blog/2013/08/how-to-build-an-2-axis-arduino-cnc-gcode-interpreter/ 266 **********************************/ 267void drawLine(float x1, float y1) { 268 269 if (verbose) 270 { 271 Serial.print("fx1, fy1: "); 272 Serial.print(x1); 273 Serial.print(","); 274 Serial.print(y1); 275 Serial.println(""); 276 } 277 278 // Bring instructions within limits 279 if (x1 >= Xmax) { 280 x1 = Xmax; 281 } 282 if (x1 <= Xmin) { 283 x1 = Xmin; 284 } 285 if (y1 >= Ymax) { 286 y1 = Ymax; 287 } 288 if (y1 <= Ymin) { 289 y1 = Ymin; 290 } 291 292 if (verbose) 293 { 294 Serial.print("Xpos, Ypos: "); 295 Serial.print(Xpos); 296 Serial.print(","); 297 Serial.print(Ypos); 298 Serial.println(""); 299 } 300 301 if (verbose) 302 { 303 Serial.print("x1, y1: "); 304 Serial.print(x1); 305 Serial.print(","); 306 Serial.print(y1); 307 Serial.println(""); 308 } 309 310 // Convert coordinates to steps 311 x1 = (int)(x1*StepsPerMillimeterX); 312 y1 = (int)(y1*StepsPerMillimeterY); 313 float x0 = Xpos; 314 float y0 = Ypos; 315 316 // Let's find out the change for the coordinates 317 long dx = abs(x1-x0); 318 long dy = abs(y1-y0); 319 int sx = x0<x1 ? StepInc : -StepInc; 320 int sy = y0<y1 ? StepInc : -StepInc; 321 322 long i; 323 long over = 0; 324 325 if (dx > dy) { 326 for (i=0; i<dx; ++i) { 327 myStepperX.step(sx); 328 over+=dy; 329 if (over>=dx) { 330 over-=dx; 331 myStepperY.step(sy); 332 } 333 delay(StepDelay); 334 } 335 } 336 else { 337 for (i=0; i<dy; ++i) { 338 myStepperY.step(sy); 339 over+=dx; 340 if (over>=dy) { 341 over-=dy; 342 myStepperX.step(sx); 343 } 344 delay(StepDelay); 345 } 346 } 347 348 if (verbose) 349 { 350 Serial.print("dx, dy:"); 351 Serial.print(dx); 352 Serial.print(","); 353 Serial.print(dy); 354 Serial.println(""); 355 } 356 357 if (verbose) 358 { 359 Serial.print("Going to ("); 360 Serial.print(x0); 361 Serial.print(","); 362 Serial.print(y0); 363 Serial.println(")"); 364 } 365 366 // Delay before any next lines are submitted 367 delay(LineDelay); 368 // Update the positions 369 Xpos = x1; 370 Ypos = y1; 371} 372 373// Raises pen 374void penUp() { 375 penServo.write(penZUp); 376 delay(LineDelay); 377 Zpos=Zmax; 378 if (verbose) { 379 Serial.println("Pen up!"); 380 } 381} 382// Lowers pen 383void penDown() { 384 penServo.write(penZDown); 385 delay(LineDelay); 386 Zpos=Zmin; 387 if (verbose) { 388 Serial.println("Pen down."); 389 } 390} 391 392void lcdPrint(){ 393 percent = (cont/total)*100; 394 if(percent<10){ 395 percentText = "0"+String((int)percent)+"%"; 396 }else{ 397 percentText = String((int)percent)+"%"; 398 } 399 if(!lcdText.equals(percentText)){ 400 lcdText = percentText; 401 lcd.clear(); 402 lcd.setCursor(0,0); 403 lcd.print(lcdText); 404 } 405 if(lcdText.equals("100%")){ 406 cont = 0; 407 lcdText = "Finish"; 408 lcd.clear(); 409 lcd.setCursor(0,0); 410 lcd.print(lcdText); 411 } 412 Serial.println(percentText); 413} 414
CNC
arduino
1#include <Servo.h> 2#include <Stepper.h> 3#include <Wire.h> 4#include <LiquidCrystal_I2C.h> 5 6#define LINE_BUFFER_LENGTH 512 7 8LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 9float total = 1.0; 10String lcdText = ""; 11String percentText = ""; 12float cont = 0.0; 13float percent = 0.0; 14 15 16 17// Servo position for Up and Down 18const int penZUp = 30; 19const int penZDown = 120; 20 21// Servo on PWM pin 8 22const int penServoPin = 8; 23 24const int stepsPerRevolution = 20; 25 26Servo penServo; 27 28// Initialize steppers for X- and Y-axis using this Arduino pins for the L293D H-bridge 29Stepper myStepperY(stepsPerRevolution, 9,12,10,13); 30Stepper myStepperX(stepsPerRevolution, 7,4,3,6); 31 32struct point { 33 float x; 34 float y; 35 float z; 36}; 37 38// Current position of plothead 39struct point actuatorPos; 40 41// Drawing settings, should be OK 42float StepInc = 1; 43int StepDelay = 0; 44int LineDelay = 50; 45int penDelay = 50; 46 47// Calculate steps per mm. Enter here. 48float StepsPerMillimeterX = 8.0; 49float StepsPerMillimeterY = 8.0; 50 51// Drawing robot limits, in mm 52float Xmin = 0; 53float Xmax = 22; 54float Ymin = 0; 55float Ymax = 22; 56float Zmin = 0; 57float Zmax = 1; 58 59float Xpos = Xmin; 60float Ypos = Ymin; 61float Zpos = Zmax; 62 63// Set to true to get debug output. 64boolean verbose = true; 65 66// Needs to interpret 67// G1 for moving 68// G4 P300 (wait 150ms) 69// M300 S30 (pen down) 70// M300 S50 (pen up) 71// Discard anything with a ( 72// Discard any other command! 73 74/********************** 75 * void setup() - Initialisations 76 ***********************/ 77void setup() { 78 // Setup 79 Serial.begin( 9600 ); 80 lcd.begin (16,2); 81 lcd.setBacklight(HIGH); 82 83 penServo.attach(penServoPin); 84 penServo.write(penZUp); 85 delay(200); 86 87 // Decrease if necessary 88 myStepperX.setSpeed(300); 89 myStepperY.setSpeed(300); 90 91 // Set & move to initial default position 92 // TBD 93 94 // Notifications!!! 95 Serial.println("Ready to print"); 96 lcd.clear(); 97 lcd.setCursor(0,0); 98 lcd.print("Ready to print"); 99} 100 101 102void loop() 103{ 104 delay(200); 105 char line[ LINE_BUFFER_LENGTH ]; 106 char c; 107 int lineIndex; 108 bool lineIsComment, lineSemiColon; 109 110 lineIndex = 0; 111 lineSemiColon = false; 112 lineIsComment = false; 113 114 115 116 while (1) { 117 118 // Serial reception 119 while ( Serial.available()>0 ) { 120 121 c = Serial.read(); 122 if (( c == '\n') || (c == '\r') ) { 123 if ( lineIndex > 0 ) { 124 line[ lineIndex ] = '\\0'; 125 if (verbose) { 126 Serial.print( "Received : "); 127 Serial.println( line ); 128 } 129 processIncomingLine( line, lineIndex ); 130 lineIndex = 0; 131 } 132 133 lineIsComment = false; 134 lineSemiColon = false; 135 Serial.println("ok"); 136 } 137 else { 138 if ( (lineIsComment) || (lineSemiColon) ) { 139 if ( c == ')' ) lineIsComment = false; 140 } 141 else { 142 if ( c == '(' ) { 143 lineIsComment = true; 144 } 145 else if ( c == ';' ) { 146 lineSemiColon = true; 147 } 148 else if ( lineIndex >= LINE_BUFFER_LENGTH-1 ) { 149 Serial.println( "ERROR - lineBuffer overflow" ); 150 lineIsComment = false; 151 lineSemiColon = false; 152 } 153 else if ( c >= 'a' && c <= 'z' ) { 154 line[ lineIndex++ ] = c-'a'+'A'; 155 } 156 else { 157 line[ lineIndex++ ] = c; 158 } 159 } 160 } 161 } 162 } 163} 164 165void processIncomingLine( char* line, int charNB ) { 166 int currentIndex = 0; 167 char buffer[ 64 ]; 168 struct point newPos; 169 170 newPos.x = 0.0; 171 newPos.y = 0.0; 172 173 // G1 for moving 174 // G1 X60 Y30 175 // G1 X30 Y50 176 // M300 S30 (pen down) 177 // M300 S50 (pen up) 178 179 while( currentIndex < charNB ) { 180 switch ( line[ currentIndex++ ] ) { 181 case 'G': 182 buffer[0] = line[ currentIndex++ ]; 183 // buffer[1] = line[ currentIndex++ ]; 184 // buffer[2] = '\\0'; 185 buffer[1] = '\\0'; 186 187 switch ( atoi( buffer ) ){ 188 case 0: 189 case 1: 190 char* indexX = strchr( line+currentIndex, 'X' ); 191 char* indexY = strchr( line+currentIndex, 'Y' ); 192 if ( indexY <= 0 ) { 193 newPos.x = atof( indexX + 1); 194 newPos.y = actuatorPos.y; 195 } 196 else if ( indexX <= 0 ) { 197 newPos.y = atof( indexY + 1); 198 newPos.x = actuatorPos.x; 199 } 200 else { 201 newPos.y = atof( indexY + 1); 202 indexY = '\\0'; 203 newPos.x = atof( indexX + 1); 204 } 205 drawLine(newPos.x, newPos.y ); 206 actuatorPos.x = newPos.x; 207 actuatorPos.y = newPos.y; 208 break; 209 } 210 cont++; 211 lcdPrint(); 212 break; 213 case 'M': 214 buffer[0] = line[ currentIndex++ ]; 215 buffer[1] = line[ currentIndex++ ]; 216 buffer[2] = line[ currentIndex++ ]; 217 buffer[3] = '\\0'; 218 switch ( atoi( buffer ) ){ 219 case 300: 220 { 221 char* indexS = strchr( line+currentIndex, 'S' ); 222 float Spos = atof( indexS + 1); 223 if (Spos == 30) { 224 penDown(); 225 } 226 if (Spos == 50) { 227 penUp(); 228 } 229 break; 230 } 231 case 114: 232 Serial.print( "Absolute position : X = " ); 233 Serial.print( actuatorPos.x ); 234 Serial.print( " - Y = " ); 235 Serial.println( actuatorPos.y ); 236 break; 237 default: 238 Serial.print( "Command not recognized : M"); 239 Serial.println( buffer ); 240 } 241 cont++; 242 lcdPrint(); 243 break; 244 case 'C': 245 Serial.println("flag"); 246 buffer[0] = line[ currentIndex++ ]; 247 buffer[1] = line[ currentIndex++ ]; 248 buffer[2] = line[ currentIndex++ ]; 249 buffer[3] = line[ currentIndex++ ]; 250 buffer[4] = '\\0'; 251 total = atoi(buffer); 252 253 break; 254 } 255 } 256 257 258 259} 260 261 262/********************************* 263 * Draw a line from (x0;y0) to (x1;y1). 264 * Bresenham algo from https://www.marginallyclever.com/blog/2013/08/how-to-build-an-2-axis-arduino-cnc-gcode-interpreter/ 265 **********************************/ 266void drawLine(float x1, float y1) { 267 268 if (verbose) 269 { 270 Serial.print("fx1, fy1: "); 271 Serial.print(x1); 272 Serial.print(","); 273 Serial.print(y1); 274 Serial.println(""); 275 } 276 277 // Bring instructions within limits 278 if (x1 >= Xmax) { 279 x1 = Xmax; 280 } 281 if (x1 <= Xmin) { 282 x1 = Xmin; 283 } 284 if (y1 >= Ymax) { 285 y1 = Ymax; 286 } 287 if (y1 <= Ymin) { 288 y1 = Ymin; 289 } 290 291 if (verbose) 292 { 293 Serial.print("Xpos, Ypos: "); 294 Serial.print(Xpos); 295 Serial.print(","); 296 Serial.print(Ypos); 297 Serial.println(""); 298 } 299 300 if (verbose) 301 { 302 Serial.print("x1, y1: "); 303 Serial.print(x1); 304 Serial.print(","); 305 Serial.print(y1); 306 Serial.println(""); 307 } 308 309 // Convert coordinates to steps 310 x1 = (int)(x1*StepsPerMillimeterX); 311 y1 = (int)(y1*StepsPerMillimeterY); 312 float x0 = Xpos; 313 float y0 = Ypos; 314 315 // Let's find out the change for the coordinates 316 long dx = abs(x1-x0); 317 long dy = abs(y1-y0); 318 int sx = x0<x1 ? StepInc : -StepInc; 319 int sy = y0<y1 ? StepInc : -StepInc; 320 321 long i; 322 long over = 0; 323 324 if (dx > dy) { 325 for (i=0; i<dx; ++i) { 326 myStepperX.step(sx); 327 over+=dy; 328 if (over>=dx) { 329 over-=dx; 330 myStepperY.step(sy); 331 } 332 delay(StepDelay); 333 } 334 } 335 else { 336 for (i=0; i<dy; ++i) { 337 myStepperY.step(sy); 338 over+=dx; 339 if (over>=dy) { 340 over-=dy; 341 myStepperX.step(sx); 342 } 343 delay(StepDelay); 344 } 345 } 346 347 if (verbose) 348 { 349 Serial.print("dx, dy:"); 350 Serial.print(dx); 351 Serial.print(","); 352 Serial.print(dy); 353 Serial.println(""); 354 } 355 356 if (verbose) 357 { 358 Serial.print("Going to ("); 359 Serial.print(x0); 360 Serial.print(","); 361 Serial.print(y0); 362 Serial.println(")"); 363 } 364 365 // Delay before any next lines are submitted 366 delay(LineDelay); 367 // Update the positions 368 Xpos = x1; 369 Ypos = y1; 370} 371 372// Raises pen 373void penUp() { 374 penServo.write(penZUp); 375 delay(LineDelay); 376 Zpos=Zmax; 377 if (verbose) { 378 Serial.println("Pen up!"); 379 } 380} 381// Lowers pen 382void penDown() { 383 penServo.write(penZDown); 384 delay(LineDelay); 385 Zpos=Zmin; 386 if (verbose) { 387 Serial.println("Pen down."); 388 } 389} 390 391void lcdPrint(){ 392 percent = (cont/total)*100; 393 if(percent<10){ 394 percentText = "0"+String((int)percent)+"%"; 395 }else{ 396 percentText = String((int)percent)+"%"; 397 } 398 if(!lcdText.equals(percentText)){ 399 lcdText = percentText; 400 lcd.clear(); 401 lcd.setCursor(0,0); 402 lcd.print(lcdText); 403 } 404 if(lcdText.equals("100%")){ 405 cont = 0; 406 lcdText = "Finish"; 407 lcd.clear(); 408 lcd.setCursor(0,0); 409 lcd.print(lcdText); 410 } 411 Serial.println(percentText); 412} 413
gcode sender
processing
1import java.awt.event.KeyEvent; 2import javax.swing.JOptionPane; 3import processing.serial.*; 4 5Serial port = null; 6 7String portname = null; 8 9boolean streaming = false; 10float speed = 0.001; 11String[] gcode; 12int i = 0; 13 14void openSerialPort() 15{ 16 if (portname == null) return; 17 if (port != null) port.stop(); 18 19 port = new Serial(this, portname, 9600); 20 21 port.bufferUntil('\n'); 22} 23 24void selectSerialPort() 25{ 26 String result = (String) JOptionPane.showInputDialog(frame, 27 "Select the serial port that corresponds to your Arduino board.", 28 "Select serial port", 29 JOptionPane.QUESTION_MESSAGE, 30 null, 31 Serial.list(), 32 0); 33 34 if (result != null) { 35 portname = result; 36 openSerialPort(); 37 } 38} 39 40void setup() 41{ 42 size(300, 100); 43 openSerialPort(); 44} 45 46void draw() 47{ 48 background(155); 49 fill(0); 50 int y = 24, dy = 12; 51 text("p: select serial port", 12, y); y += dy; 52 text("g: stream a g-code file", 12, y); y += dy; 53 text("x: stop streaming g-code", 12, y); y += dy; 54 text("c: resume streaming g-code", 12, y); y += dy; 55 text("current serial port: " + portname, 12, y); y -= dy; 56} 57 58void keyPressed() 59{ 60 if (!streaming) { 61 if (key == 'p') selectSerialPort(); 62 if (key == 'g') { 63 gcode = null; i = 0; 64 File file = null; 65 println("Loading file..."); 66 selectInput("Select a file to process:", "fileSelected", file); 67 } 68 } 69 70 if (key == 'x') streaming = false; 71 if (key == 'c') streaming = true; stream(); 72} 73 74void fileSelected(File selection) { 75 if (selection == null) { 76 println("Window was closed or the user hit cancel."); 77 } else { 78 println("User selected " + selection.getAbsolutePath()); 79 gcode = loadStrings(selection.getAbsolutePath()); 80 if (gcode == null) return; 81 streaming = true; 82 sendSize(); 83 } 84} 85 86void sendSize(){ 87 int total = 0; 88 for(String string : gcode){ 89 if(string.length()>0){ 90 if(string.charAt(0)=='G'||string.charAt(0)=='M'){ 91 total++; 92 } 93 } 94 } 95 if(total<10){ 96 port.write("C000"+total+'\n'); 97 }else if(total<100){ 98 port.write("C00"+total+'\n'); 99 }else if(total<1000){ 100 port.write("C0"+total+'\n'); 101 }else if (total <10000){ 102 port.write("C"+total+'\n'); 103 }else { 104 port.write("C9999"+'\n'); 105 } 106 stream(); 107} 108 109 110void stream() 111{ 112 if (!streaming) return; 113 114 while (true) { 115 if (i == gcode.length) { 116 streaming = false; 117 return; 118 } 119 120 if (gcode[i].trim().length() == 0) i++; 121 else break; 122 } 123 124 println(gcode[i]); 125 port.write(gcode[i] + '\n'); 126 i++; 127} 128 129void serialEvent(Serial p) 130{ 131 String s = p.readStringUntil('\n'); 132 println(s.trim()); 133 134 if (s.trim().startsWith("ok")) stream(); 135 if (s.trim().startsWith("error")) stream(); // XXX: really? 136} 137
Downloadable files
untitled
untitled

Documentation
Structure
https://www.thingiverse.com/thing:2901709
Structure
https://www.thingiverse.com/thing:2901709
Comments
Only logged in users can leave comments