# how to write a loop inside a loop

I’m controlling a stepper motor with a software (Dragon stop motion) that sends commands for next step or stepping back. Now I have the problem that when I delete a frame (step back) it calculates two different results. One is from the ‘commandframe’ of the first function, the second from the ‘commandframe’ of the second function (below 'dsm.DRAGON_DELETE_MSG). I want to have a loop, which is only running when the software sends (dsm.DRAGON_DELETE_MSG) with the input ‘commandframe’.
Its difficult to explain, but I hope someone has a simple solution…

``````#include <DragonStopMotion.h>
#include <Stepper.h>
#define SMOOTHSTEP(x) ((x) * (x) * (3 - 2 * (x))) //SMOOTHSTEP expression.
#define STEPS 30

int j = 0;             //Just an Iterator.
int i = 0;             //Just another Iterator.
float A = 0.0;         //Input Min Value
float B = 150.0;       //Input Max Value
float X;               //final smoothstepped value
float v;               //smoothstep expression variable
float N = 100.0;       //number of frames
float S = 500.0;       //number of steps
float p;
float Y;
float z;

Stepper myStepper(STEPS, 8, 9, 12, 13);

DragonStopMotion dsm = DragonStopMotion();

void setup()
{
Serial.begin(57600);
myStepper.setSpeed(30);
}

void loop()
{
// read serial messages from DSM
int cmd = dsm.processSerial();

{
if (cmd == DRAGON_CC_MSG) //software sending a command through the serial: 'CC [FRAME] [EXPOSURE] [EXPOSURE NAME] [STEREO INDEX][\r\n]'
{
if (dsm.commandFrame < N);
{
v = dsm.commandFrame / N;                    // Iteration divided by the number of steps.
v = SMOOTHSTEP(v);            // Run the smoothstep expression on v.
X = (B * v) + (A * (1 - v));   // Run the linear interpolation expression using the current smoothstep result
for ( i=0; i < X ; i++);
p = (dsm.commandFrame-1) / N;                    // Iteration divided by the number of steps.
p = SMOOTHSTEP(p);            // Run the smoothstep expression on v.
Y = (B * p) + (A * (1 - p));   // Run the linear interpolation expression using the current smoothstep result
z = (((v-p)*S));
for ( i=0; i < Y ; i++);
{
myStepper.step(z);
}
Serial.println(z);
}
}
}
{
if ((cmd == DRAGON_DELETE_MSG)) //software sending a command through the serial: 'CC [FRAME] [EXPOSURE] [EXPOSURE NAME] [STEREO INDEX][\r\n]'
{
if (dsm.commandFrame < N);
{
myStepper.step(-z);
}
Serial.println(-z);

}
}
}
``````

for ( i=0; i < Y ; i++); // the ; is wrong
{
myStepper.step(z);
}

to

for ( i=0; i < Y ; i++)
{
myStepper.step(z);
}

Also the other for loop: for ( i=0; i < X ; i++); // it just counts from 0 to X, it can even be optimized away by the compiler.

It is often better to declare the iterator in the for loop

for (int i=0; i<Y; i++)
{
// do something;
}