Assigning structure objects to an array

Ok so I am trying to create a good method for feeding an ISR the data it needs to generate a move profile for multiple stepper motors. I have all the parameters in a struct and created objects of the struct. later when trying to cycle through the calculation of the individual values for those object I used a for loop to go through the objects by putting the objects in an array called “joints”, I want to print the values to the serial monitor so I can verify it is working correctly however, the code compiles but when I run it the serial monitor prints “starting” and then 1600 for c0 which tells me it is getting to the void function “targetPosition” but then it gets carried away and the data on the monitor speeds up and just starts spitting out half of starting before reprinting starting. I verified my monitor was working with a simple program running a serial.print s through a for loop and all was well so its my code somehow. Would be great if someone can point out my error of which I assuming it has to do with the assigning of the struct objects. below is the code and an image of what the monitor is doing.

//------------------------------------------code----------------------------//

#define x_step   2
#define x_dir    3
#define x_enable 4

#define y_step   5
#define y_dir    6
#define y_enable 7

#define z_step   8
#define z_dir    9
#define z_enable 10


#define TIMER1_INTERRUPTS_ON    TIMSK1 |=  (1 << OCIE1A);
#define TIMER1_INTERRUPTS_OFF   TIMSK1 &= ~(1 << OCIE1A);
 

struct motorParameters {
  float accel;
  volatile unsigned long runspeed = 80 ;
  volatile long stepcount;
  volatile int dir;
  volatile unsigned int totalSteps;
  volatile unsigned int n;
  volatile long steps;
  volatile long actualPosition = 0;
  volatile long rampPhase = 0;
  
};


  motorParameters joints[3];





//----------------------------pin modes and standard setups------------//
void setup() {

   pinMode(x_step, OUTPUT);
   pinMode(x_enable, OUTPUT);
   pinMode(x_dir, OUTPUT);
   pinMode(y_step, OUTPUT);
   pinMode(y_enable, OUTPUT);
   pinMode(y_dir, OUTPUT);

   digitalWrite(x_enable, HIGH);
   digitalWrite(y_enable, HIGH);
   digitalWrite(z_enable, HIGH);

   noInterrupts();
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;

  OCR1A = 1000;                             // compare value
  TCCR1B |= (1 << WGM12);                   // CTC mode
  TCCR1B |= ((1 << CS11) | (1 << CS10));    // 64 prescaler
  interrupts();


  Serial.begin(9600);


}

//----------------------------------------------------Main loop that calls for desired final position---------------------//
void loop() {

 

  Serial.print("starting");
  Serial.println(" ");
  delay(5000);

  targetPosition(400, -1600, 800);
  targetPosition(-550, 380, 200);

  delay(5000);

  

}

//-----------------------------------------------------TargetPosition function---------------------------------------------//
/* the target position fuction will take in the desired position determine the 
 *  direction the joint needs to go and how many steps from the current position
 *  it needs for each motor which will then pass along the parameters to the isr 
 *  it will also reset the count values and establish the initial conditions for 
 *  for each move.
 */
void targetPosition (int x, int y, int z ){

  
  int c0 = 1600;
  Serial.print(c0);
  delay(1000);
  Serial.println(" ");
  delay(1000);
  Serial.println(x);
  delay(1000);
  Serial.println(y);
  delay(1000);
  Serial.println(z);
    
  
  int axis[3] = {x , y, z};

  for (int i = 0; i < 3; i++){                                  //this is the main loop that will interpret the desired location and derive 
    joints[i].steps = (joints[i].actualPosition - axis[i] );    // the move parameters to feed to the isr as well as reset the counts to 0
    joints[i].dir = (joints[i].steps < 0 ? 1 : -1 );
    joints[i].totalSteps = (abs(joints[i].steps));
    joints[i].accel = c0 ;
    OCR1A = joints[i].accel;
    joints[i].stepcount = 0;
    joints[i].n = 0;  
    joints[i].actualPosition = axis[i];

    Serial.print(joints[i].steps);
    Serial.print(" ");
    Serial.print(joints[i].dir);
    Serial.print(" ");
    Serial.print(joints[i].totalSteps);
    Serial.println(" ");
  }
  
}

Where is the ISR?

struct motorParameters {
  float accel;
  volatile unsigned long runspeed = 80 ;
  volatile long stepcount;
  volatile int dir;
  volatile unsigned int totalSteps;
  volatile unsigned int n;
  volatile long steps;
  volatile long actualPosition;
  volatile long rampPhase = 0;

} j1 , j2, j3 ;


motorParameters joints[3] = { j1, j2, j3 };

That does NOT "put the objects into an array". It makes any array using copies of j1, j2, j3 (via default copy constructor). That is wasteful and doesn't really make any sense because you didn't initialize j1, j2, j3 to be begin with.

Just do:

motorParameters joints[3];

@johnwasser the ISR is not coded yet to avoid the complication since right now I just want to make sure I am getting the values right that will feed into it.

@gfvalvo so I implemented your recommendation but the serial monitor is still doing what is was before any ideas on what would be causing that issue in the attached picture?

We can't see your current code.

CLaHue:
@johnwasser the ISR is not coded yet to avoid the complication since right now I just want to make sure I am getting the values right that will feed into it.

If you don't implement the ISR you should probably not enable the interrupt.

@johnwasser yes your right that's what was causing the serial monitor runaway symptoms. after that I cleaned the structure up and got it working however it is not saving the values of the struct after each iteration it dumps the memory once the targetPosition function completes. I need to get the struct to maintain the data from each iteration of the for loop so the next time it is called the parameters will be valid to the devices actual location. For this do I need to write a return method or is there a way to save that data to the heap verses like the stack which is what I guess I'm doing now since it dumps the memory once completed? ps not sure how to put code in the replies so I just edited the original post and updated the code there thanks.

//------------------------------------------code----------------------------//

Variables that are declared inside a function are local to the function. Space for them is allocated then the function starts and is released for re-use when the function ends. That memory space is NOT initialized.

void loop()
{
  int uninitializedLocalVariable;  //  Uninitialized value
  uninitializedLocalVariable += 1;  // Still uninitialized, but bigger.  :)
}

If you put an explicit initialization on the variable it gets initialized each time the function starts:

void loop()
{
  int initializedLocalVariable = 99;  //  Always set to 99
  initializedLocalVariable += 1;  // Now it's always 100
}

If you want it to keep a value across function calls, use the keyword 'static':

void loop()
{
  static int staticLocalVariable = 99;  //  Set to 99 only the first time
  staticLocalVariable += 1;  // The first time it changes to 100.  The second time, 101...
}

@johnwasser ok I see what you mean and I think I did initialize them globally at the top, except for the axis array which can be discarded whose the function completes, I found a bone head mistake in my code, I never updated the actual position that I used to start the targetPosition function with so it was always subtracting 0 an hence looked like it was discarding the struct variables when completing the function. I added
//----------- joints_.actualPosition = axis*;------------------------------------------------------------------------// at the end of the function to up date that variable so the next time it came through is was subtracting from the correct value. thanks for the help though if you had not mentioned the initialization I wouldn't have looked at that variable. It is working as it should now so its on to the ISR! (next set of major problems haha )*_