I have the following code for running 3 steppers at a time with different numbers of steps n1, n2 and n3. It does not run the n3 motor if used as follows, but if we remove the if condition then it runs.
int n1 = 0;
int n2 = 0;
int n3 = 100
while (n1 > 0 || n2 > 0 || n3 > 0) {
if (n3 > 0 && n1 == 0 && n2 == 0) {
current_port_value = XGpio_DiscreteRead(&GpioOutput, LED_CHANNEL);
new_port_value = (current_port_value & ~0X02);
XGpio_DiscreteWrite( & GpioOutput, LED_CHANNEL, new_port_value);
current_port_value = XGpio_DiscreteRead(&GpioOutput, LED_CHANNEL);
new_port_value = (current_port_value | 0X04);
XGpio_DiscreteWrite( & GpioOutput, LED_CHANNEL, new_port_value);
current_port_value = XGpio_DiscreteRead(&GpioOutput, LED_CHANNEL);
new_port_value = (current_port_value | 0X08);
XGpio_DiscreteWrite( & GpioOutput, LED_CHANNEL, new_port_value);
current_port_value = XGpio_DiscreteRead(&GpioOutput, LED_CHANNEL);
new_port_value = (current_port_value | 0X40);
XGpio_DiscreteWrite( & GpioOutput, LED_CHANNEL, new_port_value);
current_port_value = XGpio_DiscreteRead(&GpioOutput, LED_CHANNEL);
new_port_value = (current_port_value | 0X80);
XGpio_DiscreteWrite( & GpioOutput, LED_CHANNEL, new_port_value);
for (Delay = 0; Delay < LED_DELAY; Delay++);
current_port_value = XGpio_DiscreteRead(&GpioOutput, LED_CHANNEL);
new_port_value = (current_port_value & ~0X80);
XGpio_DiscreteWrite( & GpioOutput, LED_CHANNEL, new_port_value);
for (Delay = 0; Delay < LED_DELAY; Delay++);
}
n1--;
n2--;
n3--;
}
ifand examine the values in some debugger.ifblock will run exactly once as-is. First iteration of the loop,n1,n2, andn3will obviously have their initial values –0,0, and100respectively. Second iteration,n1,n2, andn3will have the values-1,-1, and99respectively because you are decrementing each of them after theifblock. Clearly it is not true thatn3 > 0 && n1 == 0 && n2 == 0on second iteration then.What do you want it to do?(Kevin) Theifblock will run exactly once as-is. First … Don't comment comments asking for additional information or clarification: edit you question. Have a look at How do I ask a good question? to make it answerable, even good.