Changing KeyWords withing program

Hello, i am working with "Janelia-Arduino, TMC2209" Library and one of the keywords for setting speed of motor is:

stepper_driver_0.moveAtVelocity(speed1);

i am controlling 4 mottors with this command

  stepper_driver_0.moveAtVelocity(Speed%);
  stepper_driver_1.moveAtVelocity(Speed%);
  stepper_driver_2.moveAtVelocity(Speed%);
  stepper_driver_3.moveAtVelocity(Speed%);

is there a way to change the number in the library keyword? something like:

  stepper_driver_x.moveAtVelocity(Speed%);
  x++;
  stepper_driver_x.moveAtVelocity(Speed%);
  x++;
  stepper_driver_x.moveAtVelocity(Speed%);
  x++;
  stepper_driver_x.moveAtVelocity(Speed%);

i am not so much expirienced to change the library.
This is just simple example ... but my code gets realy complicated without this. Its not just about speed.

Thank you alot.

It sounds like you need an array of stepper_driver objects so that you can refer to them as stepper_driver[0], stepper_driver[1] etc

Where did you get the library from ?

library is from here:
https://github.com/janelia-arduino/TMC2209?fbclid=IwAR0qEPXv39OxA_Q3iBJ50b1QMBQN9P-HP5OvWtm1gtGrow4hRXtxhhcfdc4

i have Array for each speed and direction

int velocity_motor[3];
int direction_motor[3];

but now i need to do something like

x = 0
  stepper_driver_(velocity_motor[x]).moveAtVelocity(velocity_motor[x]);
x++;

insted of

  stepper_driver_1.moveAtVelocity(velocity_motor[0]);
  stepper_driver_1.moveAtVelocity(velocity_motor[1]);
  stepper_driver_2.moveAtVelocity(velocity_motor[2]);
  stepper_driver_3.moveAtVelocity(velocity_motor[3]);

I am not familiar with the library and do not have it installed

Can you do something like

TMC2209 stepper_driver[4];

No, the keywords for library are final.

i just need to know if there is way for me to edit the code this way.

thank you :slight_smile:

There's absolutely no reason to do that.
You need an array of object of the class defined in the library. You need to share the library, we can't see your computer screen.

Also, stop calling them "Key Words". They're not.

But stepper_driver is the name of an object, not a keyword so you can choose any name for it that you like

Once you have an array of objects you could do something like this

stepper_driver[0].moveAtVelocity(velocity_motor[0]);

Thank you,
Library is here as posted before:
https://github.com/janelia-arduino/TMC2209?fbclid=IwAR0qEPXv39OxA_Q3iBJ50b1QMBQN9P-HP5OvWtm1gtGrow4hRXtxhhcfdc4

sorry, how should i call them then?

Sorry, how do i create this array of objects?
Thank you

See reply #4

no i cant do that, sorry i just dont get it.

Why can't you do it ?

i just dont understand what to do ... if there is something more to know ... can you please direct me in direction what should i learn? ... now i am just confused what should i do.

Does the library TestSetup sketch work on you system ? If so I will modify it to use an array of objects and post it here but I can't test it myself

sorry, but i dont know what TestSetup library is and i dont know whats are you planing to test ... thank you for your time ... seems i am not even on the level of knowlege to be helped ... thank you
bye bye

Hey Nic, don't give up !

The TestSetup sketch comes with the library that you linked to

#include <Arduino.h>
#include <TMC2209.h>

HardwareSerial & serial_stream = Serial1;

const long SERIAL_BAUD_RATE = 115200;
const int DELAY = 3000;

// Instantiate TMC2209
TMC2209 stepper_driver;


void setup()
{
  Serial.begin(SERIAL_BAUD_RATE);

  stepper_driver.setup(serial_stream);
}

void loop()
{
  if (stepper_driver.isSetupAndCommunicating())
  {
    Serial.println("Stepper driver is setup and communicating!");
    Serial.println("Try turning driver power off to see what happens.");
  }
  else if (stepper_driver.isCommunicatingButNotSetup())
  {
    Serial.println("Stepper driver is communicating but not setup!");
    Serial.println("Running setup again...");
    stepper_driver.setup(serial_stream);
  }
  else
  {
    Serial.println("Stepper driver is not communicating!");
    Serial.println("Try turning driver power on to see what happens.");
  }
  Serial.println("");
  delay(DELAY);
}

oh ... i see,
yes it works ...

here is just core of my code for two mottors

#include <Arduino.h>
#include <TMC2209.h>

HardwareSerial & serial_stream = Serial1;
TMC2209 stepper_driver_0;
const TMC2209::SerialAddress SERIAL_ADDRESS_0 = TMC2209::SERIAL_ADDRESS_0;

TMC2209 stepper_driver_1;
const TMC2209::SerialAddress SERIAL_ADDRESS_1 = TMC2209::SERIAL_ADDRESS_1;

const long SERIAL_BAUD_RATE = 115200;
const byte RUN_CURRENT_PERCENT = 99;


int velocity_motor[4];
int direction_motor[4];


void setup(){
  Serial.begin(SERIAL_BAUD_RATE);
  stepper_driver_0.setup(Serial1,SERIAL_BAUD_RATE,SERIAL_ADDRESS_0);
  stepper_driver_1.setup(Serial1,SERIAL_BAUD_RATE,SERIAL_ADDRESS_1);


  stepper_driver_0.enableAutomaticCurrentScaling();
  stepper_driver_0.enableAutomaticGradientAdaptation();
  stepper_driver_0.setRunCurrent(100);
  stepper_driver_0.setMicrostepsPerStepPowerOfTwo(2);
  stepper_driver_0.enableCoolStep();
  stepper_driver_0.enableStealthChop();
  stepper_driver_0.enable();

  stepper_driver_1.enableAutomaticCurrentScaling();
  stepper_driver_1.enableAutomaticGradientAdaptation();
  stepper_driver_1.setRunCurrent(100);
  stepper_driver_1.setMicrostepsPerStepPowerOfTwo(2);
  stepper_driver_1.enableCoolStep();
  stepper_driver_1.enableStealthChop();
  stepper_driver_1.enable();

  stepper_driver_0.enableInverseMotorDirection();
  stepper_driver_1.disableInverseMotorDirection();

  stepper_driver_0.moveAtVelocity(1000);
  stepper_driver_1.moveAtVelocity(500);
}

void loop(){
}

Here is a version using an array of objects. In practice it uses only one of them so should behave the same way as the original

If it works for you then I will explain the changes that I made

#include <Arduino.h>
#include <TMC2209.h>

HardwareSerial & serial_stream = Serial1;

const long SERIAL_BAUD_RATE = 115200;
const int DELAY = 3000;

// Instantiate TMC2209
TMC2209 stepper_driver[2];


void setup()
{
  Serial.begin(SERIAL_BAUD_RATE);
  stepper_driver[0].setup(serial_stream);
}

void loop()
{
  if (stepper_driver[0].isSetupAndCommunicating())
  {
    Serial.println("Stepper driver is setup and communicating!");
    Serial.println("Try turning driver power off to see what happens.");
  }
  else if (stepper_driver[0].isCommunicatingButNotSetup())
  {
    Serial.println("Stepper driver is communicating but not setup!");
    Serial.println("Running setup again...");
    stepper_driver[0].setup(serial_stream);
  }
  else
  {
    Serial.println("Stepper driver is not communicating!");
    Serial.println("Try turning driver power on to see what happens.");
  }
  Serial.println("");
  delay(DELAY);
}

My GOD... i get it ... yea ... by comparing original and this one ...i get it ...

Thank you so much ... how do i send you all my money?

I assume that it worked then

Don't forget that you can use a variable as the index to the array of objects to cut down on repetitive code if it is appropriate to do so in your sketch

I don't want any money, but if you feel like it you could put a few crowns in a charity box when you next see one