Papermind

Mini

Apr 28th, 2019
3,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Mini CNC Arduino V1
  2.  * Created 29 04 2019
  3.  * by Papermindvention.blogspot.com
  4. */
  5.  
  6. #include <Stepper.h>
  7. #include <Servo.h>
  8.  
  9. //stepper dvd-rom >>> 100 step = 15 mm >> 1 mm = 6.6 step <dari> 100 step / 15 mili == 6,6 step
  10. #define SPR 20
  11. #define SUp 100
  12. #define SDown 179
  13.  
  14. char ValStr[80]; //Array untuk Menampung value String yang nantinya akan dirubah menjadi type data char
  15. char *CharID; //
  16. char *ValSX; //variabel yang akan menampung nilai untuk sumbuX
  17. char *ValSY; //variabel yang akan menampung nilai untuk sumbuY
  18. char *ValSX2; //variabel yang akan menampung nilai untuk sumbuX
  19. char *ValSY2; //variabel yang akan menampung nilai untuk sumbuY
  20.  
  21.   //variable-variable string
  22.   String inString = "";
  23.   float posx = 0.0 ; //posisi sekarang
  24.   float posy= 0.0; //posisi sekarang
  25.  
  26. Stepper SsumbuX(SPR, 2,3,4,5);
  27. Stepper SsumbuY(SPR, 8,9,10,11);
  28. Servo Zaxis;
  29.  
  30. void setup() {
  31.   Serial.begin(9600);
  32.   Zaxis.attach(6);
  33.   SsumbuX.setSpeed(250);
  34.   SsumbuY.setSpeed(250);
  35.   Zaxis.write(SUp);
  36. }
  37.  
  38.  
  39. void loop() {
  40.   while (Serial.available() > 0) {
  41.     char inChar = Serial.read();
  42.     inString += inChar;
  43.     if (inChar == '\n') {
  44.      for (int i=0; i< inString.length(); i++){ ValStr[i] = inString.charAt(i); } //proses merubah String menjadi character
  45.       CharID = strtok (ValStr, " ");
  46.       ValSX   = strtok (NULL, "X .");
  47.       ValSX2  = strtok (NULL, " ");
  48.       ValSY   = strtok (NULL, "Y .");
  49.       ValSY2  = strtok (NULL, " ");
  50.       moveXYZ(CharID);
  51.     }
  52.   }
  53. }
  54.  
  55. //void moveXYZ(char*i1, char*i2){
  56. void moveXYZ(char*i1){
  57.   String ID = "";
  58.       ID += i1;
  59.       if (ID == "M03"){
  60.           Zaxis.write(SDown);
  61.           ID = "";
  62.       } else if (ID == "M04"){
  63.           Zaxis.write(SUp);
  64.           ID = "";          
  65.       } if (ID == "G1" || ID == "G92"){
  66.         float x = atoi(ValSX) + (atoi(ValSX2)/100.00);
  67.         float y = atoi(ValSY) + (atoi(ValSY2)/100.00);
  68.  
  69.       //Formula
  70.         float abx = (abs(x-posx)*6.0);
  71.         float aby = (abs(y-posy)*6.0);
  72.         int sstep = 1;
  73.           int stepX =  posx > x ? -sstep : sstep;
  74.           int stepY =  posy > y ? -sstep : sstep;
  75.         long i;
  76.         long over;
  77.         if (abx > aby){
  78.           for (i=0; i<abx; ++i){
  79.             SsumbuX.step(stepX);
  80.             over += aby;
  81.             if (over >= abx){
  82.               over -= abx;
  83.               SsumbuY.step(stepY);
  84.             }
  85.           }
  86.           posx = x;
  87.           posy = y;
  88.         } else if (aby > abx){
  89.           for (i=0; i<aby; ++i){
  90.             SsumbuY.step(stepY);
  91.             over += abx;
  92.             if (over >= aby){
  93.               SsumbuX.step(stepX);
  94.             }
  95.           }
  96.           posx = x;
  97.           posy = y;
  98.         } ID = "";
  99.         } else if (ID.substring(0,2) == "G4"){
  100.           delay(150);
  101.           ID = "";
  102.         } else {
  103.           Serial.println("??");
  104.           ID = "";
  105.         } inString = "";
  106. }
Add Comment
Please, Sign In to add comment