Go Down

Topic: Pause for loop without delay (Read 1 time) previous topic - next topic

hellsinki

Hi everybody,
the code below resets a floppy drive.
I want to reset individual drives without slowing down all the other routines going on at the same time.
Any suggestions how I could pause this for loop without the use of a delay?

Quote

byte  dTime; //Speed of drive's reset

void resetOne(){
   for(byte s=0;s<MaxTracks;s++){ // Loop through all 80 tracks of the floppy drive
      digitalWrite(pin+1,HIGH);  // Go in reverse
      digitalWrite(pin,HIGH);
      digitalWrite(pin,LOW);
      delay(dTime);  }
  
      currentPosition[pin] = 0; // We're reset.
      digitalWrite(pin+1,LOW);
      currentState[pin+1] = 0; // Ready to go forward.   
}
  


UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


hellsinki

Thanks for the replies.
Yes, I am familiar with the blink without delay example, but unfortunately this method does not work in a for loop.

If we work with the millis() timer in void loop() then the if condition will be triggered constantly, because  void loop() is constantly looping.
Quote

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}



However, what happens with the same method in the for loop below is that the for loop will enter once in the if condition and then loop very very quickly through the remaining 79 times, never entering the if condition again.

Quote

byte dTime; // Speed of drive's reset in milliseconds
unsigned long previousMillis;

void resetOne(){
   digitalWrite(pin+1,HIGH);  // Go in reverse
   
   for(byte s = 0;s < 80; s++){ // Loop through all tracks
     
     unsigned long currentMillis = millis();
     
      if (currentMillis - previousMillis > (dTime*1000)){
         previousMillis = millis();
         
         digitalWrite(pin,HIGH);           
         digitalWrite(pin,LOW);  }
    }
  
      currentPosition[pin] = 0; // We're reset.
      digitalWrite(pin+1,LOW);
      currentState[pin+1] = 0; // Ready to go forward. 
}
  



Anybody an idea how it is possible to pause a for loop without a delay?

Thanks!

Arrch


Anybody an idea how it is possible to pause a for loop without a delay?

Thanks!


Yeah, don't use one. When the action occurs that would cause you to start the for loop, set your index variable to zero. Increment the variable within your interval check statement and add an additional check for the upper limit. Since you've refused to post anything but snip-its of your code, I can't really give specific advice.

Go Up