Why does including a library stops Arduino Uno from running?

Hello everybody,
For my bachelor degree thesis I’m building a 6 DOF robotic arm, similar to a Tinkerkit Braccio.
As an aerospace engineering student I’m not an expert coder but I have some experience with Arduino. Writing all the code is not the scope of the thesis.

Since the end-effector of the arm has to follow a G-Coded pattern so I found on GitHub a library that reads the code sent via serial from the PC and decode it.
The lib is available here: GitHub - cgxeiji/CGx-Gcode-RobotArm: Gcode decoder for Arduino for robotic arms. Works together with InverseK (https://github.com/cgxeiji/CGx-InverseK) library.
I then downloaded another lib from the same author that does the inverse kinematic of the arm: GitHub - cgxeiji/CGx-InverseK: Inverse Kinematic Library for Arduino for a three link-arm system with a rotating base.
In the “stock” version the servos are moved via Adafruit PWM servo driver but apparently there is the possibility to drive them attached directly to the board, by modifying the code in the example.
Adafruit PWM Servo Driver lib: GitHub - adafruit/Adafruit-PWM-Servo-Driver-Library: Adafruit PWM Servo Driver Library

The problem I found is that the example code is correctly compiled and uploaded to the Arduino Uno board but then nothing happens.

I tried to add some “troubleshooting lines” of the example code by adding a Serial.println(“I’m ready”)to both the setup and loop function, nothing appeared on screen.
I then added a LED to pin 12 and tried to turn it on and off both in setup and loop but nothing ever happened so I’m supposing something is going wrong and the setup doesn’t even start since it won’t turn on the LED even if that’s the first instruction of the setup function.

I’m not able to fully understand what exactly, step-by-step, happens in those libraries since they are a bit too difficult for me but I get the general meaning of every subroutine.

I also have to make note of several gcc errors while compiling the modified “troubleshooting” example code. That’s weird since the same code compiled after a reboot of my pc.

If anybody has any idea of what the problem might be or more methods to troubleshoot, please answer.
Thank you for your time.

EDIT3: I just found out that any sketch, even Blink, will stop working whenever CGx_GCode.h is included. Any idea why is that?

EDIT1:
At the moment there is no hardware connected to the board. May this be the problem?
I also forgot to post the “troubleshooting” example code with the LED. The “serial” troubleshooting" give no result either.

// Enable this definition if you don't have an Adafruit 16-Channel Servo Driver connected to the Arduino/Teensy
//#define CGX_GCODE_DEBUG

#include <InverseK.h>
#include <CGx_Gcode.h>
#define pin 12

void setup() {
  pinMode(pin, OUTPUT);
  digitalWrite(pin, HIGH);
  delay(500);
  Link base, upperarm, forearm, hand;

  // Braccio Robotic Arm
  base.init(0, b2a(0.0), b2a(180.0));
  upperarm.init(125, b2a(15.0), b2a(165.0));
  forearm.init(125, b2a(0.0), b2a(180.0));
  hand.init(190, b2a(0.0), b2a(180.0));

  // Black Servo Robotic Arm
  //base.init(75, b2a(0.0), b2a(180.0));
  //upperarm.init(105, b2a(15.0), b2a(165.0));
  //forearm.init(100, b2a(0.0), b2a(180.0));
  //hand.init(185, b2a(0.0), b2a(180.0));

  // Attach the links to the inverse kinematic model
  InverseK.attach(base, upperarm, forearm, hand);

  Serial.begin(115200);
  
  // Initialize the arm with the upright position
  Gcode.decode("M105");
  
}

float b2a(float b){ //braccio to angle in radians
  return b / 180.0 * PI - HALF_PI;
}

void loop() {
  // This is all the code necessary at the loop to control the robotic arm using Gcode
  digitalWrite(pin, LOW);
  if (Serial.available()) {
    String s = Serial.readStringUntil('\n');
    Gcode.decode(s);
  }
  Gcode.update();
}

EDIT2:
I tryied to comment all the lines in the example above except the #include lib and the troubleshooting lines. I then tryied to upload the same ‘active’ code in another sketch and it worked. I may suppose, than, that the problem is in the libraries, installed in the IDE by adding the provided .zip file from github.

Does the example 'Blink' sketch load and correctly blink the LED, fitted to most UNOs, on pin 13 ?

What happens if you change the 'Blink' sketch pin 13 definition to pin 12, does your LED on pin 12 now flash ?

Hi srnet, thank you for your reply

I uploaded the blink sketch both with pin 13 (BUILTIN_PIN) and 12 and it works just fine.

Check the edits, please.

I tryied to run this code

#define pin 12

void setup() {
  // put your setup code here, to run once:
  pinMode(pin, OUTPUT);
  digitalWrite(pin, HIGH);
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(pin, LOW);
}

and then I added one of the two libraries at the time, commenting one line at the time.

#include <InverseK.h>
#include <CGx_Gcode.h>
#define pin 12

void setup() {
  // put your setup code here, to run once:
  pinMode(pin, OUTPUT);
  digitalWrite(pin, HIGH);
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(pin, LOW);
}

The code works only if I comment the #include <CGx-GCode.h> line so I guess that’s whare the problem lies. This puzzles me, why including an header without using it is stopping the Arduino from running? Is the GCode lib waiting for some hardware input or feedback, maybe? In this case why isn’t setup working?

#define pin 12

Oh! Don’t do that! #define symbols are implemented by text substitution, and “pin” is used all over as the argument to various functions. Which will break horribly is mysterious ways.

There is a reason why there is a convention that #define symbols are all uppercase, and variable and argument names ... aren’t.

westfw:
Oh! Don’t do that! #define symbols are implemented by text substitution, and “pin” is used all over as the argument to various functions. Which will break horribly is mysterious ways.

There is a reason why there is a convention that #define symbols are all uppercase, and variable and argument names ... aren’t.

Thank you for your reply.
I did not know about this convenction but it makes a lot of sense, so thanks for the advice.

I just tried an older version of the GCode library and it seems to be working.
Here you can find the changelog

Do you have any idea of why those modifications might interfere with Arduino's work?
Thank you again.

After changing the #define pin statement, do you still have a problem? How did you change it?

I modifyied the code to this:

#include <InverseK.h>
#include <CGx_Gcode.h>
int PIN = 12;

void setup() {
  // put your setup code here, to run once:
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN, HIGH);
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(PIN, LOW);
}

If I use the latest release of GCode lib the code compile and upload succesfully but doesn’t work. If I use the before debug_fix version the code works.

I also checked in the changelog for any “pin” or pin-containing words, there’s any so #define pin was not the problem.

I also checked in the changelog for any "pin" or pin-containing words, there's any so #define pin was not the problem.

Does the library expect pin 12 to be used in a different way?

Add some Serial.print() statements, and a call to Serial.begin(), to see whether the setup() function starts and ends, and whether loop() gets called.

If setup() starts and ends, and loop() gets called, then you can NOT say that the code doesn't work.

I added the serial printing lines this way

#include <InverseK.h>
#include <CGx_Gcode.h>
int PIN = 8;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN, HIGH);
  delay(1000);

  
  Serial.println("Set up!");
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("I am working");
  digitalWrite(PIN, LOW);
}

Since I’m not sure if pin 12 is used by the library I switched to pin 8 which is not a PWM pin and should be free. Anyway I don’t think any pin is used by the libraries since they use the Adafruit PWM Servo Drive which works with I2C so SCA/SCL port should be used.

I ran the code and once again, nothing is shown in serial monitor if I include the new lib. If I comment it out the LED turns on and off and the “I am working” message is fed as expected.
Since the test LED never tuned on while the new GCode lib was included I’m guessing something in the new lib is stopping even the setup function from running.

I also have to note that the TX on-board LED is off when i run with the new lib so I’m guessing that not even the Serial.begin instruction is executed and surely no print is executed.

I took a quick look at the library, and it uses the dreaded String class. I suspect you've got a memory issue

It might be, I'm not an expert coder so I don't know why that class is dreaded. I wonder why none had this problem, maybe they used a Mega instead?
Do you think is there anything I can do to solve it or do I have to stick to the old version? (why does the old version work and the new one don't? I see nothing in the changelog involving string.).

Thanks for your help

_Gcode Gcode;

The library creates a global instance of the class, calling the constructor.

The constructor then contains:

#ifndef CGX_GCODE_DEBUG
	pwm = Adafruit_PWMServoDriver();
	
	// TODO: test the pwm
	pwm.begin();
	pwm.setPWMFreq(60);
#endif

I don't know whether CGX_GCODE_DEBUG is defined by default, or not, but begin() methods are typically used when the method must be called after the hardware is ready. The hardware is NOT ready when this constructor is called.

This is just more crap posted to the web by Adafruit without adequate peer-review.

Personally, I don't use ANY software developed by Adafruit, because of their crappy track record.

And because they assume that all the code they write will be running on Arduinos with several terabytes of memory.