# Problem with sketch for mini CNC plotter

**URL:** <https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392>\
**Category:** Programming\
**Created:** [March 2, 2021, 12:14pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392 "2021-03-02T12:14:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![smoothg](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@smoothg](https://forum.arduino.cc/u/smoothg)\
**Post date:** [March 2, 2021, 12:14pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/1 "2021-03-02T12:14:59Z")

</div>

So I'm new with Arduino and don't really know how to code. I'm doing mini CNC plotter using Arduino UNO R3 (one you can buy on aliexpress), Arduino Motor Shield L293D, TowerPro SG 90 servo and 2 motors from DVD drives.  
I'm doing project from this site (I will post code and picture below so you don't have to go in there) -\> [How to make Arduino mini CNC plotter machine - Electric Diy Lab](https://electricdiylab.com/how-to-make-arduino-mini-cnc-plotter-machine/) (if it doesn't open here's less detailed video of it [How to make Arduino Mini CNC plotter machine - YouTube](https://www.youtube.com/watch?v=Gm6bH3p6cNQ&t=577s))  
Problem starts when I need to send code to Arduino. Code is too long so I'll attach ino file.

Here's wiring picture if it helps in anyway.

 ![](https://europe1.discourse-cdn.com/arduino/original/4X/7/4/3/743ff603a89642383fdfd83dc76c06ab268a1856.jpeg)

[sketch\_cnc\_plotter.ino](https://forum.arduino.cc/uploads/short-url/dslU9GApHF3rwMhd3qEYRziNFmt.ino) (9.98 KB)

---

<div class="post-metadata">

**Author:** ![Ballscrewbob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ballscrewbob/32/950458_2.png) [@Ballscrewbob](https://forum.arduino.cc/u/Ballscrewbob)\
**Post date:** [March 2, 2021, 1:18pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/2 "2021-03-02T13:18:57Z")

</div>

Where did you download the sketch from as it seems to have errors ?  
The first link is dead BTW.

Also note I removed your avatar so please select something else.

---

<div class="post-metadata">

**Author:** ![smoothg](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@smoothg](https://forum.arduino.cc/u/smoothg)\
**Post date:** [March 2, 2021, 1:52pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/3 "2021-03-02T13:52:45Z")

</div>

The sketch is mine, I copied code from this site (this site works only on phone for some reason). Like I mentioned before code is too long so I attached ino. If you go to this site on phone it should work fine.

Btw. It coppied my avatar automatically from my Google account, sorry about that.

---

<div class="post-metadata">

**Author:** ![missdrew](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@missdrew](https://forum.arduino.cc/u/missdrew)\
**Post date:** [March 2, 2021, 1:59pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/4 "2021-03-02T13:59:24Z")

</div>

you have setup at the top that should not be there.

---

<div class="post-metadata">

**Author:** ![Ballscrewbob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ballscrewbob/32/950458_2.png) [@Ballscrewbob](https://forum.arduino.cc/u/Ballscrewbob)\
**Post date:** [March 2, 2021, 2:09pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/5 "2021-03-02T14:09:20Z")

</div>

Avatar again removed GRRR !

And as MissDrew states you have issues in the sketch itself that need to be fixed first.  
Would you like this post moved to programming ?

---

<div class="post-metadata">

**Author:** ![missdrew](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@missdrew](https://forum.arduino.cc/u/missdrew)\
**Post date:** [March 2, 2021, 2:26pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/6 "2021-03-02T14:26:27Z")

</div>

Quite a few errors, if you copied this from a phone, then I can understand these, generally you had 2 setup() & 2 loop(), and a lot of [ instead of { . Here is an edited version (untested) , but it does compile.

```cpp
#include <Servo.h>
#include <AFMotor.h>
#define LINE_BUFFER_LENGTH 512
char STEP = MICROSTEP ;
const int penZUp = 120;
const int penZDown = 50;
const int penServoPin = 10 ;
const int stepsPerRevolution = 48;
Servo penServo;
AF_Stepper myStepperY(stepsPerRevolution, 1);
AF_Stepper myStepperX(stepsPerRevolution, 2);
struct point {
  float x;
  float y;
  float z;
};
struct point actuatorPos;
float StepInc = 1;
int StepDelay = 1;
int LineDelay = 0;
int penDelay = 50;
float StepsPerMillimeterX = 100.0;
float StepsPerMillimeterY = 100.0;
float Xmin = 0;
float Xmax = 40;
float Ymin = 0;
float Ymax = 40;
float Zmin = 0;
float Zmax = 1;
float Xpos = Xmin;
float Ypos = Ymin;
float Zpos = Zmax;
boolean verbose = false;

void setup()
{
  Serial.begin( 9600 );
  penServo.attach(penServoPin);
  penServo.write(penZUp);
  delay(100);
  // Decrease if necessary
  myStepperX.setSpeed(600);
  myStepperY.setSpeed(600);
  Serial.println("Mini CNC Plotter alive and kicking!");
  Serial.print("X range is from ");
  Serial.print(Xmin);
  Serial.print(" to ");
  Serial.print(Xmax);
  Serial.println(" mm.");
  Serial.print("Y range is from ");
  Serial.print(Ymin);
  Serial.print(" to ");
  Serial.print(Ymax);
  Serial.println(" mm.");
}

void loop()
{
  delay(100);
  char line[LINE_BUFFER_LENGTH];
  char c;
  int lineIndex;
  bool lineIsComment, lineSemiColon;
  lineIndex = 0;
  lineSemiColon = false;
  lineIsComment = false;

  while (1)
  {
    // Serial reception - Mostly from Grbl, added semicolon support
    while ( Serial.available() > 0 )
    {
      c = Serial.read();
      if (( c == '\n') || (c == '\r') )
      { // End of line reached
        if ( lineIndex > 0 )
        {
          line[lineIndex] = '\0';  
          if (verbose)
          {
            Serial.print( "Received : ");
            Serial.println( line );
          }
          processIncomingLine( line, lineIndex );
          lineIndex = 0;
        }
        lineIsComment = false;
        lineSemiColon = false;
        Serial.println("ok");
      }
      else
      {
        if ( (lineIsComment) || (lineSemiColon) )
        {
          if ( c == ')' )  lineIsComment = false;
        }
        else
        {
          if ( c <= ' ' )
          {}
          else if ( c == '/' )
          {}
          else if ( c == '(' )
          {
            lineIsComment = true;
          }
          else if ( c == ';' )
          {
            lineSemiColon = true;
          }
          else if ( lineIndex >= LINE_BUFFER_LENGTH - 1 )
          {
            Serial.println( "ERROR - lineBuffer overflow" );
            lineIsComment = false;
            lineSemiColon = false;
          }
          else if ( c >= 'a' && c <= 'z' )
          {
            line[lineIndex++] = c - 'a' + 'A';
          }
          else
          {
            line[lineIndex++] = c;
          }
        }
      }
    }
  }
}
void processIncomingLine( char* line, int charNB )
{
  int currentIndex = 0;
  char buffer[64];
  struct point newPos;
  newPos.x = 0.0;
  newPos.y = 0.0;

  while ( currentIndex < charNB )
  {
    switch ( line[currentIndex++] )
{ 
      case 'U':
        penUp();
        break;
      case 'D':
        penDown();
        break;
      case 'G':
        buffer[0] = line[currentIndex++];          // /!\ Dirty - Only works with 2 digit commands
        //      buffer[1] = line[currentIndex++];
        //      buffer[2] = '\0';
        buffer[1] = '\0';

        switch ( atoi( buffer ) ) {                  // Select G command
          case 0:                                   // G00 & G01 - Movement or fast movement. Same here
          case 1:
            // /!\ Dirty - Suppose that X is before Y
            char* indexX = strchr( line + currentIndex, 'X' ); // Get X/Y position in the string (if any)
            char* indexY = strchr( line + currentIndex, 'Y' );
            if ( indexY <= 0 ) {
              newPos.x = atof( indexX + 1);
              newPos.y = actuatorPos.y;
            }
            else if ( indexX <= 0 ) {
              newPos.y = atof( indexY + 1);
              newPos.x = actuatorPos.x;
            }
            else {
              newPos.y = atof( indexY + 1);
              indexY = '\0';
              newPos.x = atof( indexX + 1);
            }
            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[1] = line[currentIndex++];
        buffer[2] = line[currentIndex++];
        buffer[3] = '\0';
        switch ( atoi( buffer ) ) {
          case 300:
            {
              char* indexS = strchr( line + currentIndex, 'S' );
              float Spos = atof( indexS + 1);
              //         Serial.println("ok");
              if (Spos == 30) {
                penDown();
              }
              if (Spos == 50) {
                penUp();
              }
              break;
            }
          case 114:                                // M114 - Repport position
            Serial.print( "Absolute position : X = " );
            Serial.print( actuatorPos.x );
            Serial.print( "  -  Y = " );
            Serial.println( actuatorPos.y );
            break;
          default:
            Serial.print( "Command not recognized : M");
            Serial.println( buffer );
        }
    }
  }
}
void drawLine(float x1, float y1)
{
  if (verbose)
  {
    Serial.print("fx1, fy1: ");
    Serial.print(x1);
    Serial.print(",");
    Serial.print(y1);
    Serial.println("");
  }
  if (x1 >= Xmax)
  {
    x1 = Xmax;
  }
  if (x1 <= Xmin)
  {
    x1 = Xmin;
  }
  if (y1 >= Ymax)
  {
    y1 = Ymax;
  }
  if (y1 <= Ymin)
  {
    y1 = Ymin;
  }
  if (verbose)
  {
    Serial.print("Xpos, Ypos: ");
    Serial.print(Xpos);
    Serial.print(",");
    Serial.print(Ypos);
    Serial.println("");
  }
  if (verbose)
  {
    Serial.print("x1, y1: ");
    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);
  int sx = x0 < x1 ? StepInc : -StepInc;
  int sy = y0 < y1 ? StepInc : -StepInc;
  long i;
  long over = 0;
  if (dx > dy)
  {
    for (i = 0; i < dx; ++i)
    {
      myStepperX.onestep(sx, STEP);
      over += dy;
      if (over >= dx)
      {
        over -= dx;
        myStepperY.onestep(sy, STEP);
      }
      delay(StepDelay);
    }
  }
  else
  {
    for (i = 0; i < dy; ++i)
    {
      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("");
    Serial.print("Going to (");
    Serial.print(x0);
    Serial.print(",");
    Serial.print(y0);
    Serial.println(")");
  }
  delay(LineDelay);
  Xpos = x1;
  Ypos = y1;
}
void penUp() 
{
  penServo.write(penZUp);
  delay(penDelay);
  Zpos = Zmax;
  digitalWrite(15, LOW);
  digitalWrite(16, HIGH);
  if (verbose) 
  {
    Serial.println("Pen up!");
  }
}
void penDown() 
{
  penServo.write(penZDown);
  delay(penDelay);
  Zpos = Zmin;
  digitalWrite(15, HIGH);
  digitalWrite(16, LOW);
  if (verbose) 
  {
    Serial.println("Pen down.");
  }
}

```

---

<div class="post-metadata">

**Author:** ![smoothg](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@smoothg](https://forum.arduino.cc/u/smoothg)\
**Post date:** [March 2, 2021, 2:35pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/7 "2021-03-02T14:35:38Z")

</div>

> ballscrewbob:  
> Avatar again removed GRRR !
> 
> And as MissDrew states you have issues in the sketch itself that need to be fixed first.  
> Would you like this post moved to programming ?

I don't mind moving post to programming I just want script to be fixed. Don't know about avatar though, I didn't change it again (I'll just remove it myself).

---

<div class="post-metadata">

**Author:** ![smoothg](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@smoothg](https://forum.arduino.cc/u/smoothg)\
**Post date:** [March 2, 2021, 2:43pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/8 "2021-03-02T14:43:07Z")

</div>

> missdrew:  
> Quite a few errors, if you copied this from a phone, then I can understand these, generally you had 2 setup() & 2 loop(), and a lot of [ instead of { . Here is an edited version (untested) , but it does compile.

I'll test it when I come back home, thanks in advance!

---

<div class="post-metadata">

**Author:** ![missdrew](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@missdrew](https://forum.arduino.cc/u/missdrew)\
**Post date:** [March 2, 2021, 2:48pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/9 "2021-03-02T14:48:03Z")

</div>

Also - update that Avatar! I find it offensive!

---

<div class="post-metadata">

**Author:** ![smoothg](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@smoothg](https://forum.arduino.cc/u/smoothg)\
**Post date:** [March 2, 2021, 7:02pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/10 "2021-03-02T19:02:58Z")

</div>

I just came home and tested updated version. It still has some errors.  
Here's full list of them

Arduino:1.8.14 Hourly Build 2021/01/29 11:33 (Windows 10), Płytka:"Arduino Uno"

Alternatives for Servo.h: [Servo@1.1.6]

ResolveLibrary(Servo.h)

-\> candidates: [Servo@1.1.6]

Alternatives for AFMotor.h: [Adafruit-Motor-Shield-library-master@1.0.1]

ResolveLibrary(AFMotor.h)

-\> candidates: [Adafruit-Motor-Shield-library-master@1.0.1]

C:\Users\wikto\Desktop\Arduino\CNC plotter\sketch\_cnc\_plotter\sketch\_cnc\_plotter.ino: In function 'void setup()':

sketch\_cnc\_plotter:37:1: error: a function-definition is not allowed here before '{' token

{

^

sketch\_cnc\_plotter:59:1: error: a function-definition is not allowed here before '{' token

{

^

sketch\_cnc\_plotter:132:1: error: a function-definition is not allowed here before '{' token

{

^

sketch\_cnc\_plotter:214:1: error: a function-definition is not allowed here before '{' token

{

^

sketch\_cnc\_plotter:311:1: error: a function-definition is not allowed here before '{' token

{

^

sketch\_cnc\_plotter:323:1: error: a function-definition is not allowed here before '{' token

{

^

exit status 1

a function-definition is not allowed here before '{' token

Also I've changed my Avatar (again sorry for that, I didn't choose it)

---

<div class="post-metadata">

**Author:** ![missdrew](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@missdrew](https://forum.arduino.cc/u/missdrew)\
**Post date:** [March 2, 2021, 8:11pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/11 "2021-03-02T20:11:36Z")

</div>

Don't use hourly builds. Download 1.8.13

> Multiple libraries were found for "Servo.h"  
> Used: C:\Users\drewa\Documents\Arduino\libraries\Servo  
> Not used: Z:\ArduinoCustom\arduino-1.8.13\libraries\Servo  
> Using library Servo at version 1.1.6 in folder: C:\Users\drewa\Documents\Arduino\libraries\Servo  
> Using library Adafruit\_Motor\_Shield\_library at version 1.0.1 in folder: C:\Users\drewa\Documents\Arduino\libraries\Adafruit\_Motor\_Shield\_library  
> "C:\Users\drewa\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5/bin/avr-size" -A "C:\Users\drewa\AppData\Local\Temp\arduino\_build\_960921/sketch\_mar02c.ino.elf"  
> Sketch uses 9330 bytes (28%) of program storage space. Maximum is 32256 bytes.  
> Global variables use 479 bytes (23%) of dynamic memory, leaving 1569 bytes for local variables. Maximum is 2048 bytes.

It compiled fine for me

---

<div class="post-metadata">

**Author:** ![smoothg](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@smoothg](https://forum.arduino.cc/u/smoothg)\
**Post date:** [March 3, 2021, 7:58am UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/12 "2021-03-03T07:58:44Z")

</div>

```cpp
Arduino:1.8.13 (Windows 10), Płytka:"Arduino Uno"

Alternatives for AFMotor.h: [Adafruit-Motor-Shield-library-master@1.0.1]

ResolveLibrary(AFMotor.h)

 -> candidates: [Adafruit-Motor-Shield-library-master@1.0.1]

sketch_mar03a:37:1: error: a function-definition is not allowed here before '{' token

{

^

sketch_mar03a:59:1: error: a function-definition is not allowed here before '{' token

{

^

sketch_mar03a:132:1: error: a function-definition is not allowed here before '{' token

{

^

sketch_mar03a:214:1: error: a function-definition is not allowed here before '{' token

{

^

sketch_mar03a:311:1: error: a function-definition is not allowed here before '{' token

{

^

sketch_mar03a:323:1: error: a function-definition is not allowed here before '{' token

{

^

sketch_mar03a:333:1: error: expected '}' at end of input

}

^
exit status 1

a function-definition is not allowed here before '{' token

```

So, I've installed Arduino 1.8.13 but still when I click verify/upload i get these errors.

---

<div class="post-metadata">

**Author:** ![Ballscrewbob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ballscrewbob/32/950458_2.png) [@Ballscrewbob](https://forum.arduino.cc/u/Ballscrewbob)\
**Post date:** [March 3, 2021, 1:26pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/13 "2021-03-03T13:26:05Z")

</div>

@smoothg

Could you please take a few moments to [Learn How To Use The Forum](https://forum.arduino.cc/index.php?topic=710766.0) then you will know how to post error messages etc.

Other general [help and troubleshooting advice can be found here.](https://forum.arduino.cc/index.php?topic=52113.0)  
It will help you get the best out of the forum in the future.

---

<div class="post-metadata">

**Author:** ![missdrew](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@missdrew](https://forum.arduino.cc/u/missdrew)\
**Post date:** [March 3, 2021, 8:00pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/14 "2021-03-03T20:00:38Z")

</div>

Post the code that you are trying to compile. Also, explain how you installed the libraries, because they look wrongly installed.

---

<div class="post-metadata">

**Author:** ![jimLee](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimlee/32/214865_2.png) [@jimLee](https://forum.arduino.cc/u/jimLee)\
**Post date:** [March 3, 2021, 8:19pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/15 "2021-03-03T20:19:46Z")

</div>

After all that.. Now I'm dying of curiosity to see the avatar.

-jim lee

---

<div class="post-metadata">

**Author:** ![smoothg](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@smoothg](https://forum.arduino.cc/u/smoothg)\
**Post date:** [March 3, 2021, 8:39pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/16 "2021-03-03T20:39:25Z")

</div>

> missdrew:  
> Quite a few errors, if you copied this from a phone, then I can understand these, generally you had 2 setup() & 2 loop(), and a lot of [ instead of { . Here is an edited version (untested) , but it does compile.
> 
> ```arduino
> #include <Servo.h>
> 
> ```

#include \<AFMotor.h\>  
#define LINE\_BUFFER\_LENGTH 512  
char STEP = MICROSTEP ;  
const int penZUp = 120;  
const int penZDown = 50;  
const int penServoPin = 10 ;  
const int stepsPerRevolution = 48;  
Servo penServo;  
AF\_Stepper myStepperY(stepsPerRevolution, 1);  
AF\_Stepper myStepperX(stepsPerRevolution, 2);  
struct point {  
&nbsp;float x;  
&nbsp;float y;  
&nbsp;float z;  
};  
struct point actuatorPos;  
float StepInc = 1;  
int StepDelay = 1;  
int LineDelay = 0;  
int penDelay = 50;  
float StepsPerMillimeterX = 100.0;  
float StepsPerMillimeterY = 100.0;  
float Xmin = 0;  
float Xmax = 40;  
float Ymin = 0;  
float Ymax = 40;  
float Zmin = 0;  
float Zmax = 1;  
float Xpos = Xmin;  
float Ypos = Ymin;  
float Zpos = Zmax;  
boolean verbose = false;

void setup()  
{  
&nbsp;Serial.begin( 9600 );  
&nbsp;penServo.attach(penServoPin);  
&nbsp;penServo.write(penZUp);  
&nbsp;delay(100);  
&nbsp;// Decrease if necessary  
&nbsp;myStepperX.setSpeed(600);  
&nbsp;myStepperY.setSpeed(600);  
&nbsp;Serial.println("Mini CNC Plotter alive and kicking!");  
&nbsp;Serial.print("X range is from ");  
&nbsp;Serial.print(Xmin);  
&nbsp;Serial.print(" to ");  
&nbsp;Serial.print(Xmax);  
&nbsp;Serial.println(" mm.");  
&nbsp;Serial.print("Y range is from ");  
&nbsp;Serial.print(Ymin);  
&nbsp;Serial.print(" to ");  
&nbsp;Serial.print(Ymax);  
&nbsp;Serial.println(" mm.");  
}

void loop()  
{  
&nbsp;delay(100);  
&nbsp;char line[LINE\_BUFFER\_LENGTH];  
&nbsp;char c;  
&nbsp;int lineIndex;  
&nbsp;bool lineIsComment, lineSemiColon;  
&nbsp;lineIndex = 0;  
&nbsp;lineSemiColon = false;  
&nbsp;lineIsComment = false;

while (1)  
&nbsp;{  
&nbsp; &nbsp;// Serial reception - Mostly from Grbl, added semicolon support  
&nbsp; &nbsp;while ( Serial.available() \> 0 )  
&nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp;c = Serial.read();  
&nbsp; &nbsp; &nbsp;if (( c == '\n') || (c == '\r') )  
&nbsp; &nbsp; &nbsp;{ // End of line reached  
&nbsp; &nbsp; &nbsp; &nbsp;if ( lineIndex \> 0 )  
&nbsp; &nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;line[lineIndex] = '\0'; &nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (verbose)  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Serial.print( "Received : ");  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Serial.println( line );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;processIncomingLine( line, lineIndex );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lineIndex = 0;  
&nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp;lineIsComment = false;  
&nbsp; &nbsp; &nbsp; &nbsp;lineSemiColon = false;  
&nbsp; &nbsp; &nbsp; &nbsp;Serial.println("ok");  
&nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp;else  
&nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp;if ( (lineIsComment) || (lineSemiColon) )  
&nbsp; &nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( c == ')' ) &nbsp;lineIsComment = false;  
&nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp;else  
&nbsp; &nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( c \<= ' ' )  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if ( c == '/' )  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if ( c == '(' )  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lineIsComment = true;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if ( c == ';' )  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lineSemiColon = true;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if ( lineIndex \>= LINE\_BUFFER\_LENGTH - 1 )  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Serial.println( "ERROR - lineBuffer overflow" );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lineIsComment = false;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lineSemiColon = false;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if ( c \>= 'a' && c \<= 'z' )  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;line[lineIndex++] = c - 'a' + 'A';  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;line[lineIndex++] = c;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp;}  
&nbsp;}  
}  
void processIncomingLine( char\* line, int charNB )  
{  
&nbsp;int currentIndex = 0;  
&nbsp;char buffer[64];  
&nbsp;struct point newPos;  
&nbsp;newPos.x = 0.0;  
&nbsp;newPos.y = 0.0;

while ( currentIndex \< charNB )  
&nbsp;{  
&nbsp; &nbsp;switch ( line[currentIndex++] )  
{  
&nbsp; &nbsp; &nbsp;case 'U':  
&nbsp; &nbsp; &nbsp; &nbsp;penUp();  
&nbsp; &nbsp; &nbsp; &nbsp;break;  
&nbsp; &nbsp; &nbsp;case 'D':  
&nbsp; &nbsp; &nbsp; &nbsp;penDown();  
&nbsp; &nbsp; &nbsp; &nbsp;break;  
&nbsp; &nbsp; &nbsp;case 'G':  
&nbsp; &nbsp; &nbsp; &nbsp;buffer[0] = line[currentIndex++]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// /!\ Dirty - Only works with 2 digit commands  
&nbsp; &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp; &nbsp;buffer[1] = line[currentIndex++];  
&nbsp; &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp; &nbsp;buffer[2] = '\0';  
&nbsp; &nbsp; &nbsp; &nbsp;buffer[1] = '\0';

switch ( atoi( buffer ) ) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Select G command  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 0: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // G00 & G01 - Movement or fast movement. Same here  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 1:  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// /!\ Dirty - Suppose that X is before Y  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char\* indexX = strchr( line + currentIndex, 'X' ); // Get X/Y position in the string (if any)  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char\* indexY = strchr( line + currentIndex, 'Y' );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( indexY \<= 0 ) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;newPos.x = atof( indexX + 1);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;newPos.y = actuatorPos.y;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if ( indexX \<= 0 ) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;newPos.y = atof( indexY + 1);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;newPos.x = actuatorPos.x;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;newPos.y = atof( indexY + 1);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;indexY = '\0';  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;newPos.x = atof( indexX + 1);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;drawLine(newPos.x, newPos.y );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp; &nbsp; &nbsp;Serial.println("ok");  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;actuatorPos.x = newPos.x;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;actuatorPos.y = newPos.y;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;  
&nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp;break;  
&nbsp; &nbsp; &nbsp;case 'M':  
&nbsp; &nbsp; &nbsp; &nbsp;buffer[0] = line[currentIndex++]; &nbsp; &nbsp; &nbsp; &nbsp;// /!\ Dirty - Only works with 3 digit commands  
&nbsp; &nbsp; &nbsp; &nbsp;buffer[1] = line[currentIndex++];  
&nbsp; &nbsp; &nbsp; &nbsp;buffer[2] = line[currentIndex++];  
&nbsp; &nbsp; &nbsp; &nbsp;buffer[3] = '\0';  
&nbsp; &nbsp; &nbsp; &nbsp;switch ( atoi( buffer ) ) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 300:  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char\* indexS = strchr( line + currentIndex, 'S' );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;float Spos = atof( indexS + 1);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp; &nbsp; &nbsp; Serial.println("ok");  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (Spos == 30) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;penDown();  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (Spos == 50) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;penUp();  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 114: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// M114 - Repport position  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Serial.print( "Absolute position : X = " );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Serial.print( actuatorPos.x );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Serial.print( " &nbsp;- &nbsp;Y = " );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Serial.println( actuatorPos.y );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Serial.print( "Command not recognized : M");  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Serial.println( buffer );  
&nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp;}  
&nbsp;}  
}  
void drawLine(float x1, float y1)  
{  
&nbsp;if (verbose)  
&nbsp;{  
&nbsp; &nbsp;Serial.print("fx1, fy1: ");  
&nbsp; &nbsp;Serial.print(x1);  
&nbsp; &nbsp;Serial.print(",");  
&nbsp; &nbsp;Serial.print(y1);  
&nbsp; &nbsp;Serial.println("");  
&nbsp;}  
&nbsp;if (x1 \>= Xmax)  
&nbsp;{  
&nbsp; &nbsp;x1 = Xmax;  
&nbsp;}  
&nbsp;if (x1 \<= Xmin)  
&nbsp;{  
&nbsp; &nbsp;x1 = Xmin;  
&nbsp;}  
&nbsp;if (y1 \>= Ymax)  
&nbsp;{  
&nbsp; &nbsp;y1 = Ymax;  
&nbsp;}  
&nbsp;if (y1 \<= Ymin)  
&nbsp;{  
&nbsp; &nbsp;y1 = Ymin;  
&nbsp;}  
&nbsp;if (verbose)  
&nbsp;{  
&nbsp; &nbsp;Serial.print("Xpos, Ypos: ");  
&nbsp; &nbsp;Serial.print(Xpos);  
&nbsp; &nbsp;Serial.print(",");  
&nbsp; &nbsp;Serial.print(Ypos);  
&nbsp; &nbsp;Serial.println("");  
&nbsp;}  
&nbsp;if (verbose)  
&nbsp;{  
&nbsp; &nbsp;Serial.print("x1, y1: ");  
&nbsp; &nbsp;Serial.print(x1);  
&nbsp; &nbsp;Serial.print(",");  
&nbsp; &nbsp;Serial.print(y1);  
&nbsp; &nbsp;Serial.println("");  
&nbsp;}  
&nbsp;x1 = (int)(x1 \* StepsPerMillimeterX);  
&nbsp;y1 = (int)(y1 \* StepsPerMillimeterY);  
&nbsp;float x0 = Xpos;  
&nbsp;float y0 = Ypos;  
&nbsp;long dx = abs(x1 - x0);  
&nbsp;long dy = abs(y1 - y0);  
&nbsp;int sx = x0 \< x1 ? StepInc : -StepInc;  
&nbsp;int sy = y0 \< y1 ? StepInc : -StepInc;  
&nbsp;long i;  
&nbsp;long over = 0;  
&nbsp;if (dx \> dy)  
&nbsp;{  
&nbsp; &nbsp;for (i = 0; i \< dx; ++i)  
&nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp;myStepperX.onestep(sx, STEP);  
&nbsp; &nbsp; &nbsp;over += dy;  
&nbsp; &nbsp; &nbsp;if (over \>= dx)  
&nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp;over -= dx;  
&nbsp; &nbsp; &nbsp; &nbsp;myStepperY.onestep(sy, STEP);  
&nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp;delay(StepDelay);  
&nbsp; &nbsp;}  
&nbsp;}  
&nbsp;else  
&nbsp;{  
&nbsp; &nbsp;for (i = 0; i \< dy; ++i)  
&nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp;myStepperY.onestep(sy, STEP);  
&nbsp; &nbsp; &nbsp;over += dx;  
&nbsp; &nbsp; &nbsp;if (over \>= dy)  
&nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp;over -= dy;  
&nbsp; &nbsp; &nbsp; &nbsp;myStepperX.onestep(sx, STEP);  
&nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp;delay(StepDelay);  
&nbsp; &nbsp;}  
&nbsp;}  
&nbsp;if (verbose)  
&nbsp;{  
&nbsp; &nbsp;Serial.print("dx, dy:");  
&nbsp; &nbsp;Serial.print(dx);  
&nbsp; &nbsp;Serial.print(",");  
&nbsp; &nbsp;Serial.print(dy);  
&nbsp; &nbsp;Serial.println("");  
&nbsp; &nbsp;Serial.print("Going to (");  
&nbsp; &nbsp;Serial.print(x0);  
&nbsp; &nbsp;Serial.print(",");  
&nbsp; &nbsp;Serial.print(y0);  
&nbsp; &nbsp;Serial.println(")");  
&nbsp;}  
&nbsp;delay(LineDelay);  
&nbsp;Xpos = x1;  
&nbsp;Ypos = y1;  
}  
void penUp()  
{  
&nbsp;penServo.write(penZUp);  
&nbsp;delay(penDelay);  
&nbsp;Zpos = Zmax;  
&nbsp;digitalWrite(15, LOW);  
&nbsp;digitalWrite(16, HIGH);  
&nbsp;if (verbose)  
&nbsp;{  
&nbsp; &nbsp;Serial.println("Pen up!");  
&nbsp;}  
}  
void penDown()  
{  
&nbsp;penServo.write(penZDown);  
&nbsp;delay(penDelay);  
&nbsp;Zpos = Zmin;  
&nbsp;digitalWrite(15, HIGH);  
&nbsp;digitalWrite(16, LOW);  
&nbsp;if (verbose)  
&nbsp;{  
&nbsp; &nbsp;Serial.println("Pen down.");  
&nbsp;}  
}

> ```arduino
> 
> ```

I'm trying to compile this code. I've installed libraries this way: Sketch-\>include library-\>add ZIP library

> jimLee:  
> After all that.. Now I'm dying of curiosity to see the avatar.
> 
> -jim lee

Google "potężny gej" if your curious

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [March 4, 2021, 3:15pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/17 "2021-03-04T15:15:02Z")

</div>

It doesn't take much to update to the latest Adafruit\_Motor\_Shield\_V2\_Library:

```cpp
#include <Adafruit_MotorShield.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();

#include <Servo.h>

#define LINE_BUFFER_LENGTH 512
char STEP = MICROSTEP ;
const int penZUp = 120;
const int penZDown = 50;
const int penServoPin = 10 ;
const int stepsPerRevolution = 48;
Servo penServo;
Adafruit_StepperMotor *myStepperY = AFMS.getStepper(stepsPerRevolution, 1);
Adafruit_StepperMotor *myStepperX = AFMS.getStepper(stepsPerRevolution, 2);
struct point
{
  float x;
  float y;
  float z;
};
struct point actuatorPos;
float StepInc = 1;
int StepDelay = 1;
int LineDelay = 0;
int penDelay = 50;
float StepsPerMillimeterX = 100.0;
float StepsPerMillimeterY = 100.0;
float Xmin = 0;
float Xmax = 40;
float Ymin = 0;
float Ymax = 40;
float Zmin = 0;
float Zmax = 1;
float Xpos = Xmin;
float Ypos = Ymin;
float Zpos = Zmax;
boolean verbose = false;

void setup()
{
  Serial.begin( 9600 );
  penServo.attach(penServoPin);
  penServo.write(penZUp);
  delay(100);
  // Decrease if necessary
  myStepperX->setSpeed(600);
  myStepperY->setSpeed(600);
  Serial.println("Mini CNC Plotter alive and kicking!");
  Serial.print("X range is from ");
  Serial.print(Xmin);
  Serial.print(" to ");
  Serial.print(Xmax);
  Serial.println(" mm.");
  Serial.print("Y range is from ");
  Serial.print(Ymin);
  Serial.print(" to ");
  Serial.print(Ymax);
  Serial.println(" mm.");
}

void loop()
{
  delay(100);
  char line[LINE_BUFFER_LENGTH];
  char c;
  int lineIndex;
  bool lineIsComment, lineSemiColon;
  lineIndex = 0;
  lineSemiColon = false;
  lineIsComment = false;

  while (1)
  {
    // Serial reception - Mostly from Grbl, added semicolon support
    while ( Serial.available() > 0 )
    {
      c = Serial.read();
      if (( c == '\n') || (c == '\r') )
      {
        // End of line reached
        if ( lineIndex > 0 )
        {
          line[lineIndex] = '\0';
          if (verbose)
          {
            Serial.print( "Received : ");
            Serial.println( line );
          }
          processIncomingLine( line, lineIndex );
          lineIndex = 0;
        }
        lineIsComment = false;
        lineSemiColon = false;
        Serial.println("ok");
      }
      else
      {
        if ( (lineIsComment) || (lineSemiColon) )
        {
          if ( c == ')' )  lineIsComment = false;
        }
        else
        {
          if ( c <= ' ' )
          {}
          else if ( c == '/' )
          {}
          else if ( c == '(' )
          {
            lineIsComment = true;
          }
          else if ( c == ';' )
          {
            lineSemiColon = true;
          }
          else if ( lineIndex >= LINE_BUFFER_LENGTH - 1 )
          {
            Serial.println( "ERROR - lineBuffer overflow" );
            lineIsComment = false;
            lineSemiColon = false;
          }
          else if ( c >= 'a' && c <= 'z' )
          {
            line[lineIndex++] = c - 'a' + 'A';
          }
          else
          {
            line[lineIndex++] = c;
          }
        }
      }
    }
  }
}

void processIncomingLine( char* line, int charNB )
{
  int currentIndex = 0;
  char buffer[64];
  struct point newPos;
  newPos.x = 0.0;
  newPos.y = 0.0;

  while ( currentIndex < charNB )
  {
    switch ( line[currentIndex++] )
    {
      case 'U':
        penUp();
        break;
      case 'D':
        penDown();
        break;
      case 'G':
        buffer[0] = line[currentIndex++];          // /!\ Dirty - Only works with 2 digit commands
        //      buffer[1] = line[currentIndex++];
        //      buffer[2] = '\0';
        buffer[1] = '\0';

        switch ( atoi( buffer ) )                    // Select G command
        {
          case 0:                                   // G00 & G01 - Movement or fast movement. Same here
          case 1:
            {
              // /!\ Dirty - Suppose that X is before Y
              char* indexX = strchr( line + currentIndex, 'X' ); // Get X/Y position in the string (if any)
              char* indexY = strchr( line + currentIndex, 'Y' );
              if ( indexY == NULL )
              {
                newPos.x = atof( indexX + 1);
                newPos.y = actuatorPos.y;
              }
              else if ( indexX == NULL )
              {
                newPos.y = atof( indexY + 1);
                newPos.x = actuatorPos.x;
              }
              else
              {
                newPos.y = atof( indexY + 1);
                *indexY = '\0';
                newPos.x = atof( indexX + 1);
              }
              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[1] = line[currentIndex++];
        buffer[2] = line[currentIndex++];
        buffer[3] = '\0';
        switch ( atoi( buffer ) )
        {
          case 300:
            {
              char* indexS = strchr( line + currentIndex, 'S' );
              float Spos = atof( indexS + 1);
              //         Serial.println("ok");
              if (Spos == 30)
              {
                penDown();
              }
              if (Spos == 50)
              {
                penUp();
              }
              break;
            }
            
          case 114:                                // M114 - Repport position
            Serial.print( "Absolute position : X = " );
            Serial.print( actuatorPos.x );
            Serial.print( "  -  Y = " );
            Serial.println( actuatorPos.y );
            break;
            
          default:
            Serial.print( "Command not recognized : M");
            Serial.println( buffer );
        }
    }
  }
}

void drawLine(float x1, float y1)
{
  if (verbose)
  {
    Serial.print("fx1, fy1: ");
    Serial.print(x1);
    Serial.print(",");
    Serial.print(y1);
    Serial.println("");
  }
  if (x1 >= Xmax)
  {
    x1 = Xmax;
  }
  if (x1 <= Xmin)
  {
    x1 = Xmin;
  }
  if (y1 >= Ymax)
  {
    y1 = Ymax;
  }
  if (y1 <= Ymin)
  {
    y1 = Ymin;
  }
  if (verbose)
  {
    Serial.print("Xpos, Ypos: ");
    Serial.print(Xpos);
    Serial.print(",");
    Serial.print(Ypos);
    Serial.println("");
  }
  if (verbose)
  {
    Serial.print("x1, y1: ");
    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);
  int sx = x0 < x1 ? StepInc : -StepInc;
  int sy = y0 < y1 ? StepInc : -StepInc;
  long i;
  long over = 0;
  if (dx > dy)
  {
    for (i = 0; i < dx; ++i)
    {
      myStepperX->onestep(sx, STEP);
      over += dy;
      if (over >= dx)
      {
        over -= dx;
        myStepperY->onestep(sy, STEP);
      }
      delay(StepDelay);
    }
  }
  else
  {
    for (i = 0; i < dy; ++i)
    {
      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("");
    Serial.print("Going to (");
    Serial.print(x0);
    Serial.print(",");
    Serial.print(y0);
    Serial.println(")");
  }
  delay(LineDelay);
  Xpos = x1;
  Ypos = y1;
}

void penUp()
{
  penServo.write(penZUp);
  delay(penDelay);
  Zpos = Zmax;
  digitalWrite(15, LOW);
  digitalWrite(16, HIGH);
  if (verbose)
  {
    Serial.println("Pen up!");
  }
}

void penDown()
{
  penServo.write(penZDown);
  delay(penDelay);
  Zpos = Zmin;
  digitalWrite(15, HIGH);
  digitalWrite(16, LOW);
  if (verbose)
  {
    Serial.println("Pen down.");
  }
}

```

---

<div class="post-metadata">

**Author:** ![smoothg](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@smoothg](https://forum.arduino.cc/u/smoothg)\
**Post date:** [March 4, 2021, 7:18pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/18 "2021-03-04T19:18:52Z")

</div>

I got similar errors even after updating library

```cpp
Arduino:1.8.13 (Windows 10), Płytka:"Arduino Uno"

Using board 'uno' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr

Using core 'arduino' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr

Detecting libraries used...

Alternatives for Adafruit_MotorShield.h: [Adafruit_Motor_Shield_V2_Library-master@1.0.11]

ResolveLibrary(Adafruit_MotorShield.h)

  -> candidates: [Adafruit_Motor_Shield_V2_Library-master@1.0.11]

Alternatives for Wire.h: [Wire@1.0]

ResolveLibrary(Wire.h)

  -> candidates: [Wire@1.0]

Alternatives for Servo.h: [Servo@1.1.6 Servo@1.1.7]

ResolveLibrary(Servo.h)

  -> candidates: [Servo@1.1.6 Servo@1.1.7]

sketch_cnc_plotter:45:1: error: a function-definition is not allowed here before '{' token

 {

 ^

sketch_cnc_plotter:68:1: error: a function-definition is not allowed here before '{' token

 {

 ^

sketch_cnc_plotter:145:1: error: a function-definition is not allowed here before '{' token

 {

 ^

sketch_cnc_plotter:243:1: error: a function-definition is not allowed here before '{' token

 {

 ^

sketch_cnc_plotter:342:1: error: a function-definition is not allowed here before '{' token

 {

 ^

sketch_cnc_plotter:356:1: error: a function-definition is not allowed here before '{' token

 {

 ^

sketch_cnc_plotter:366:1: error: expected '}' at end of input

 }

 ^

exit status 1

a function-definition is not allowed here before '{' token

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [March 4, 2021, 10:56pm UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/19 "2021-03-04T22:56:21Z")

</div>

The errors you are getting are at the beginning of EVERY function. It sounds like you have an extra '{' or missing '}' near the top of the file.

---

<div class="post-metadata">

**Author:** ![Ballscrewbob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ballscrewbob/32/950458_2.png) [@Ballscrewbob](https://forum.arduino.cc/u/Ballscrewbob)\
**Post date:** [March 5, 2021, 1:23am UTC](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392/20 "2021-03-05T01:23:04Z")

</div>

@smoothg

Banned until you can confirm that avatar is fixed.

Please PM a moderator to get the ban lifted once it is done.

[Next page](https://forum.arduino.cc/t/problem-with-sketch-for-mini-cnc-plotter/698392.md?page=2)
