Value within a struct suddenly changes after several runs through a for loop
I've run into a problem in this function, specifically involving a struct and a for loop.
Whenever loop 1 reaches i = 27 and reaches line 29 (L293D_step), the value within struct STEPPERZ changes from 6 to some value in the ten milions.
bool readSurfaceData()
{
directionX = -1 * RESET_DIRECTION;
for(uint8_t i = 0; i < stepper_Z_steps[servoStatus] / step_length; i++)
{
for (uint8_t j = 0; j < (round(stepper_X_steps[servoStatus] / step_length)+1); j++)
{
L293D_step(STEPPERX, step_length * directionX);
x = step_length * directionX;
fillMatrixSlot_V2(tooth, i, j);
}
if (directionX < 0)
{
x += step_length;
}
else
{
x -= step_length;
}
directionX *=(-1);
HAL_Delay(Z_delay);
L293D_step(STEPPERZ, step_length * RESET_DIRECTION * -1);
z+=step_length * RESET_DIRECTION * -1;
HAL_Delay(scan_delay);
}
if(!sendData())
{
return false;
}
return true;
}Here's L293D_step:
void L293D_step(struct Stepper_Driver *driver, int steps_to_move) {
int steps_left = abs(steps_to_move);
int this_step = 0;
if (steps_to_move > 0) {
driver->direction = 1;
}
if (steps_to_move < 0) {
driver->direction = 0;
}
while (steps_left > 0) {
unsigned long now = HAL_GetTick();
if ((now - driver->last_step_time) >= (driver->step_delay)) {
driver->last_step_time = now;
if (driver->direction == 1) {
driver->step_number++;
if (driver->step_number == driver->number_of_steps) {
driver->step_number = 0;
}
} else {
if (driver->step_number == 0) {
driver->step_number = driver->number_of_steps;
}
driver->step_number--;
}
steps_left--;
this_step = driver ->step_number % 4;
L293D_stepMotor(driver, this_step);
}
}
}and the Stepper_Driver struct:
struct Stepper_Driver
{
uint16_t c1;
uint16_t c2;
uint16_t c3;
uint16_t c4;
GPIO_TypeDef *p1;
GPIO_TypeDef *p2;
GPIO_TypeDef *p3;
GPIO_TypeDef *p4;
uint8_t direction;
unsigned long step_delay;
uint8_t number_of_steps;
int step_number;
unsigned long last_step_time;
};I checked the functions in a separate project, and they worked perfectly fine, and I've been checking for inconsistencies between the two, but can't find any so far. If anyone has any idea what might be causing this glitch, any help is appreciated.
Edit: I just remembered that I had been playing around with clock configurations in MX to get a better feel for how it worked, and that it's running on a lower sysclk than the primary file. Not sure if that's the problem, but I'm going to adjust it to remove that inconsistency
