Error referencing instances of a class in web ide

Every time I try to set a variable for one motor in a motorInfo class I’ve defined it updates info for all of the instances of the class.

Here is some of the code from the .ino

#include "motorInfo.h"
#include "application.h"
#include "math.h"
// This #include statement was automatically added by the Particle IDE.
#include "Adafruit-MotorShield-V2/Adafruit-MotorShield-V2.h"
// Create the motor shield object with the default I2C address

#include <stdarg.h>

#define PRINTF_BUFFER_SIZE 128
#define MOTORS 6
#define SHIELD_COUNT 3

int i =0;

Adafruit_MotorShield afms[SHIELD_COUNT]; 
Adafruit_StepperMotor *steppers[MOTORS];
motorInfo *info[MOTORS];

void Serial_printf(const char* fmt, ...) {
   char buff[PRINTF_BUFFER_SIZE];
   va_list args;
   va_start(args, fmt);
   vsnprintf(buff, PRINTF_BUFFER_SIZE, fmt, args);
   va_end(args);
   Serial.println(buff);
}

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
  
  for(i = 0;i<SHIELD_COUNT;i++){
     String shieldNum = "0x6" + i;
    String endTest = "";
    afms[i]  = Adafruit_MotorShield(0x60+i); 
    //afms[i]  = Adafruit_MotorShield(0x6+i); 
     afms[i].begin(); 
    }

  /****this is the line that sets all of the motor info instances to have 45 steps *******/
  info[4]->setSteps(45);

  for(i = 0;i<MOTORS;i++){
      int f = floor(i/2);
      steppers[i] = afms[f].getStepper(200, ((i%2)+1));
      steppers[i]->setSpeed(255);
      info[i] = new motorInfo;
      Serial.print("Motor : ");
      Serial.println(i); 
      Serial.print(" State : ");
      Serial.println(info[i]->getState() );
      Serial.print(" Direction : ");
      Serial.println(info[i]->getDirection());
      Serial.print(" Style : ");
      Serial.println(info[i]->getStyle());
      Serial.print(" Steps : ");
      Serial.println(info[i]->getSteps());
  Serial_printf("address of motor info %d is %x", i, info[i]);
  Serial.println();
      delay(1000);
        }
}

I’ve ensured that the pointers are referencing different addresses in the particle cli ie but still all of the instances have 45 steps even though I only specified info[4] to have 45 steps.
Motor : 2
State : 0
Direction : 68
Style : 69
Steps : 45
address of motor info 2 is 20005368

Motor : 3
State : 0
Direction : 68
Style : 69
Steps : 45
address of motor info 3 is 20005358

Motor : 4
State : 0
Direction : 68
Style : 69
Steps : 45
address of motor info 4 is 20005490

Motor : 5
State : 0
Direction : 68
Style : 69
Steps : 45
address of motor info 5 is 20005458

here is the motorInfo.h file

#include "application.h"

#ifndef motorInfo_h
#define motorInfo_h

class motorInfo {

 public:

  motorInfo();
    
  void
    setState(bool state),
    setSteps(int steps),
    setDirection(uint8_t direction),
    setStyle(uint8_t style);
    
  int
    getSteps(void);
  bool 
    getState(void);
  uint8_t
    getDirection(void),
    getStyle(void);

  private:

};

#endif // motorInfo.h

and also the motorInfo.cpp

#include "motorInfo.h"
bool myState = false;
int mySteps = 0;
uint8_t myDirection = 'FORWARD';
uint8_t myStyle = 'SINGLE';

motorInfo::motorInfo(){

}

void motorInfo::setState(bool state){
    myState = state;    
}

void motorInfo::setSteps(int steps){
    mySteps = steps;   
}
void motorInfo::setDirection(uint8_t direction){
    myDirection = direction;    
}
void motorInfo::setStyle(uint8_t style){
    myStyle = style;    
}
    

int motorInfo::getSteps(){
    return mySteps;    
}

bool motorInfo::getState(){
    return myState;    
}

uint8_t motorInfo::getDirection(){
    return myDirection;    
}
uint8_t motorInfo::getStyle(){
    return myStyle;    
}

it seems simple enough but maybe I’m just missing something here. Any and all help would be much appreciated.
best
p

This
afms[i] = Adafruit_MotorShield(0x60+i);

confuses me: Adafruit_MotorShield
is a class name, not a class instance, unless I misread.

If you are trying to fill the array with N instances of Adafruit_MotorShield, you need to use the new operator in order to create a new instance each time.

Try:

afms[i] = new Adafruit_MotorShield(0x60+i);

I think I understand your problem, but I’m just now learning C++, so my knowledge is limited. Your variables mySteps, myStyle, myDirection, and myState should be instance variables defined inside the class definition not at the top of the file where they’ll be global.

class MotorInfo {
    
    bool myState;
    int mySteps;
    uint8_t myDirection;
    uint8_t myStyle;

 public:
...
...

You can give them default values in the constructor in the .cpp file,

MotorInfo::MotorInfo(){
    myState = false;
    mySteps = 0;
    myDirection = 'FORWARD';
    myStyle = 'SINGLE';
}

In your main file, you don’t create the MotorInfo objects until you’re in the for loop, so you need to change the value of mySteps for motor 4 after you do that creation.

for(i = 0;i<MOTORS;i++){
      int f = floor(i/2);
      steppers[i] = afms[f].getStepper(200, ((i%2)+1));
      steppers[i]->setSpeed(255);
      info[i] = new MotorInfo;
  }
  
  info[4]->setSteps(45);

  for(i = 0;i<MOTORS;i++){    
      Serial.print("Motor : ");
      Serial.println(i); 
      Serial.print(" State : ");
      Serial.println(info[i]->getState() );
      Serial.print(" Direction : ");
      Serial.println(info[i]->getDirection());
      Serial.print(" Style : ");
      Serial.println(info[i]->getStyle());
      Serial.print(" Steps : ");
      Serial.println(info[i]->getSteps());
      Serial_printf("address of motor info %d is %x", i, info[i]);
      Serial.println();
      delay(1000);
  }

Notice that I capitalized the name of the class also – I don’t know if that’s standard practice in C++, but it is in Objective-C which is where I have most of my experience.

Thanks ric for those useful tidbits so I got it working by in the .ino
making sure to declare new motorInfo before trying to set any aspect of motor info. If I initiate the variables from this point like so I get the desired results

for(i = 0;i<MOTORS;i++){
      int f = floor(i/2);
      steppers[i] = afms[f].getStepper(200, ((i%2)+1));
      steppers[i]->setSpeed(255);
      info[i] = new motorInfo;
      info[i]->setSteps(0);
      info[i]->setState(false);
      info[i]->setDirection(FORWARD);
      info[i]->setStyle(DOUBLE);
  }

Motor : 0
State : 0
Direction : 1
Style : 1
Steps : 0

but if I instead declare the variables in the motorInfo constructor like you said in the MotorInfo.cpp

MotorInfo::MotorInfo(){
    myState = false;
    mySteps = 0;
    myDirection = 'FORWARD';
    myStyle = 'SINGLE';
}

I get really weird results like so
Motor : 0
State : 0
Direction : 54
Style : 0
Steps : 12
Motor : 1
State : 236
Direction : 232
Style : 75
Steps : -3
Motor : 2
State : 80
Direction : 2
Style : 4
Steps : 18103
Motor : 3
State : 255
Direction : 52
Style : 0
Steps : 134217735
Motor : 4
State : 108
Direction : 69
Style : 0
Steps : 553703
Motor : 5
State : 1
Direction : 0
Style : 2
Steps : 234881504

just curious as to why this would be…

I don’t know why you would get those results. I tested the code that I posted, and got the expected results; all the MotorInfo objects had the default values I put in the constructor, except for the one value I changed afterwards (the mySteps 45).

hmm I’ve got to run but I’ll take a look later and see if theres something weird in my code