Declaring of an object in setup does not work in void loop

Hello everyone,

I have a programing problem with a smart servo "Herkulex dsr 0101". I want to use this object in void loop while I have initialize it in setup function. I have post the simplest version of my code to give you the idea of the problem. Servo is working in the setup if I want without a problem. I have tried several ways to declare things as global variables but since the same function initializing the serial communication I have problem. Does anyone sees what am I missing here?

#include <HkxPosControl.h>
uint16_t inputVoltage;
int16_t position;
uint16_t temperature;
int16_t velocity;
int16_t PWM;
int16_t goalPosition;
int16_t trajectoryPosition;
int16_t trajectoryVelocity;

void setup() {

  delay(1000); // wait 10 seconds to open the serial monitor
  HkxPrint printout = HkxPrint(Serial, 9600);  // Printout errors on the serial monitor
  HkxCommunication communication = HkxCommunication(HKX_115200, Serial1, printout);
  HkxPosControl servo(1, communication, printout);

 servo.movePosition(0, 1000, HKX_LED_BLUE, true);


}

void loop() {

  if (Serial.find("#")) {
    int Position = Serial.parseInt();
    int Speed = Serial.parseInt();
    servo.movePosition(Position, Speed, HKX_LED_BLUE, true);
    servo.getBehaviour(&inputVoltage, &temperature, &position, &velocity, &PWM, &goalPosition, &trajectoryPosition, &trajectoryVelocity);
    prints();
  }

}

Thanks in advance
A.

Variables and objects declared in setup() will not be available to use in any other function.

Declare the object outside of any function and it will be globally available to any function in the program

Hello,

Unfortunately I already knew that. So my question does not change. But I am 100% sure that I was unclear. The library that I use first initialize serial communication between user/pc and arduino mega. Then initialize the communication between arduino mega and smart servo. At the end, declares the servo function.

If I move these commands out of any function then serial ports are not responding but the function is globally available.

I am trying to found a way that I will have available the servo in every function and at the same time serial ports working.

I am not reposting my code but I add the library that I used .

Greetings

HerkuleXLib_v1.2.zip (59.9 KB)

So my question does not change.

Your question was how to use a variable declared in setup() in loop(). The answer is still "You can't".

You must make the variables that you want to use in two functions global, or pass the variable declared in one function to the other one.

Since you don't control the call to, or signature of, loop(), you can not go that route. So, your ONLY choice is to make them global.

Variables do NOT have to be value when they are declared. You can declare a global variable, with no value, and then initialize it in setup() and use it in loop().

The key, in your case, may require that the variables be pointers, not objects. This requires the use of new to create the actual instance, and pointer dereferencing notation (->, not .) to use the instance.

I know and that is the reason I said I was not clear, I do not know if there is a point to repeat it every time. it will not change the past. I am still having the same problem. I have tried many different things I will post some of them.

I tried by using pointers but it kept returning me inside the setup function after every time I was sending a command. But that was the closest to fixing the problem because I manage to upload the code at least.

#include <HkxPosControl.h>

HkxPosControl* servoptr;
void setup() {
  delay(2000);
  HkxPrint printout = HkxPrint(Serial, 9600);  // Printout errors on the serial monitor
  HkxCommunication communication = HkxCommunication(HKX_115200, Serial1, printout);
  HkxPosControl servo(1, communication, printout);
  Serial.println("Ready");

}
void loop() {
  if (Serial.find("#")) {
    int  Pos = Serial.parseInt();
    int  Spe = Serial.parseInt();
    servoptr->movePosition(Pos, Spe, HKX_LED_BLUE, true);
  }
}

I tried also like this by declaring a pointer to a function but when I was trying to link them I was getting error
cannot convert 'HkxPosControl*' to 'HkxPosControl (*)(unsigned char, HkxCommunication, HkxPrint)' in assignment servoptr=&servo;

#include <HkxPosControl.h>

HkxPosControl (*servoptr)(unsigned char, HkxCommunication, HkxPrint);

void setup() {
  delay(1000);
  HkxPrint printout = HkxPrint(Serial, 9600);  // Printout errors on the serial monitor
  HkxCommunication communication = HkxCommunication(HKX_115200, Serial1, printout);
  HkxPosControl servo(1, communication, printout);
  servoptr=&servo;
  Serial.println("Ready");
}
void loop() {
  if (Serial.find("#")) {
    int  Pos = Serial.parseInt();
    int  Spe = Serial.parseInt();
    servoptr.movePosition(Pos, Spe, HKX_LED_BLUE, true);
  }
}

How I should fix it? Because even after I repeat reading for the pointers I did not manage to solve it.

I tried by using pointers but it kept returning me inside the setup function after every time I was sending a command. But that was the closest to fixing the problem because I manage to upload the code at least.

But, you are not using them correctly.

HkxPosControl *servoptr = NULL; // ALWAYS initialize pointers to NULL

void setup()
{
   // Snippity-do-dah

   servoptr = new HkxPosControl(1, communication, printout);
}

void someOtherFunction()
{
   servoptr->someMethod(someData);
}