Declare an array of L298N

Im trying to declare an array of multiple L298N drivers but there is something missing

first I declare two drivers:

L298NX2 motor[2] = {
    {M_ENA, M_IN1, M_IN2, M_ENB, M_IN3, M_IN4},
    {M_ENA2, M_IN5, M_IN6, M_ENB2, M_IN7, M_IN8}};

I declare a function where I want to use it:
void driveMotor(uint8_t index, L298NX2 motor[]);

But when I am trying to use it as below I get error:

  for (int i = 0; i < 2; i++)
  {
    Serial.println(i);
    driveMotor(i, motor[i], val[i], driverPwm[i], threshold);
  }

no suitable conversion function from "L298NX2" to "L298NX2 *" existsC/C++(413)

Im not to familliar with c++ so maybe there is something I wont get in the code?

You promised to pass a pointer to a L298NX2, but you passed a copy of one element.

Why do you want to pass the array and an index, instead a reference to the selected element?

Why are these different (number and type of parameters)?

1 Like

Im still struggling with pointers. How do I this properly then? How do I pass a pointer to L298NX2?

You only presented a snippet of your code, I have no clue what you want to do,
so it's impossible for me to tell you how to do it right.

I can show you some things, that can be done.

struct Dummy {
  uint8_t val;
} someDummies[] = {
  1, 2, 3, 4, 5
};

void oneRef(Dummy& elm) {
  Serial.println(elm.val);
}
void onePtr(Dummy* pElm) {
  Serial.println(pElm->val);
}
void ptrAndCount(Dummy* pElm, uint8_t count) {
  for (uint8_t index = 0; index < count; index++) {
    Serial.println(pElm[index].val);
  }
  for (uint8_t index = 0; index < count; index++) {
    Serial.println((pElm + index)->val);
  }
}
void setup() {
  Serial.begin(115200);
  for (Dummy& elm : someDummies) {
    oneRef(elm);
    onePtr(&elm);
  }
  ptrAndCount(someDummies, sizeof(someDummies) / sizeof(someDummies[0]));
}
void loop() {}

maybe this helps..

// Define the stepper motor and the pins that is connected to
//construct steppers first..
AccelStepper stepper1(1, 2, 5); // (Typeof driver: with 2 pins, STEP, DIR)
AccelStepper stepper2(1, 3, 6);
//make an array of steppers and add pointers to the steppers..
AccelStepper *steppers[2] = {&stepper1,&stepper2};
//access stepper in array..
steppers[0]->setMaxSpeed(1200);

good luck.. ~q

Thanks yes I got it working.
So & is the address reference?
And * is to tell something to point to that variable name to be called later?

-> was a new syntax for me ... :slight_smile: what does it do exactly

1 Like

& takes the address of something, -> accesses members via pointers.

References are constant pointers that are automatically dereferencing, like [] dereferences.

ptr->element and (*ptr).element are the same, like &ptr[4] and ptr+4.

1 Like

yes,
the * mean the array is an array of pointers..
the & adds the pointer reference into the array..
glad you got it..

~q

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.