user19005: I need have two stepping motors working at the same time each with a different number of steps. But loops (for, while) don't work with this task using type of interrupt above. What is wrong?

My code:
class Driver {
    int pinStep;
    long times;
    int state;
    unsigned long previousTime;

  public:
    Driver(int pin, long interval) {
      pinStep = pin;
      pinMode(pinStep, OUTPUT);
      times = interval;
      state = LOW;
      previousTime = 0;
    }

    void update(unsigned long currentTime) {
      int iteration;
      while ((iteration < 5) & (currentTime - previousTime >= times)) {
        previousTime = currentTime;
        if (state == LOW) {
          state = HIGH;
        } else {
          state = LOW;
        }
        digitalWrite(pinStep, state);
        iteration++;
      }
    }
};

Driver drv1(5, 2);
Driver drv2(7, 50);

// set timer behavior
void setup() {
  OCR0A = 0x80; // 128
  TIMSK0 |= _BV(OCIE0A);
}

// interrupt handler
SIGNAL(TIMER0_COMPA_vect) {
  unsigned long currentTime = millis();
  drv1.update(currentTime);
  drv2.update(currentTime);
}

void loop() {
}


user10794: void update(unsigned long currentTime) {
      int iteration;
      while ((iteration < 5)

Your code take a variable with an undefined value (iteration) and loops while it is less than 5. How is this useful? Perhaps set iteration to zero:
      int iteration = 0;



But cycles (for, while) don't work with this task using type of interrupt above. 

Please define what "don't work" means here. It does too much? Too little? Nothing at all?


And Serial.print prints out iteration value as "111111"

The only way that could happen is if currentTime - previousTime >= times is false. Which it would be.
  while ((iteration < 5) & (currentTime - previousTime >= times)) {

Do you mean: currentTime - previousTime < times?

19005: <p>I need have two stepping motors working at the same time each with a different number of steps. But loops (<code>for</code>, <code>while</code>) don't work with this task using type of interrupt above. What is wrong?</p>

<p><a href="https://i.stack.imgur.com/sr3Sg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sr3Sg.jpg" alt="Screenshot of update function"></a></p>

<p>My code:</p>

<pre><code>class Driver {
    int pinStep;
    long times;
    int state;
    unsigned long previousTime;

  public:
    Driver(int pin, long interval) {
      pinStep = pin;
      pinMode(pinStep, OUTPUT);
      times = interval;
      state = LOW;
      previousTime = 0;
    }

    void update(unsigned long currentTime) {
      int iteration;
      while ((iteration &lt; 5) &amp; (currentTime - previousTime &gt;= times)) {
        previousTime = currentTime;
        if (state == LOW) {
          state = HIGH;
        } else {
          state = LOW;
        }
        digitalWrite(pinStep, state);
        iteration++;
      }
    }
};

Driver drv1(5, 2);
Driver drv2(7, 50);

// set timer behavior
void setup() {
  OCR0A = 0x80; // 128
  TIMSK0 |= _BV(OCIE0A);
}

// interrupt handler
SIGNAL(TIMER0_COMPA_vect) {
  unsigned long currentTime = millis();
  drv1.update(currentTime);
  drv2.update(currentTime);
}

void loop() {
}
</code></pre>
 10794: <pre><code>void update(unsigned long currentTime) {
      int iteration;
      while ((iteration &lt; 5)
</code></pre>

<p>Your code take a variable with an undefined value (<code>iteration</code>) and loops while it is less than 5. How is this useful? Perhaps set iteration to zero:</p>

<pre><code>      int iteration = 0;
</code></pre>

<hr>

<blockquote>
  <p>But cycles (for, while) don't work with this task using type of interrupt above. </p>
</blockquote>

<p>Please define what "don't work" means here. It does too much? Too little? Nothing at all?</p>

<hr>

<blockquote>
  <p>And Serial.print prints out iteration value as "111111"</p>
</blockquote>

<p>The only way that could happen is if <code>currentTime - previousTime &gt;= times</code> is false. Which it would be.</p>

<pre><code>  while ((iteration &lt; 5) &amp; (currentTime - previousTime &gt;= times)) {
</code></pre>

<p>Do you mean: <code>currentTime - previousTime &lt; times</code>?</p>
