Pages: [1]   Go Down
Author Topic: Pause for loop without delay  (Read 756 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
turtle gum
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.   
}
  

Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 89
Posts: 3453
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have a look at the blink without delay tutorial http://arduino.cc/en/Tutorial/BlinkWithoutDelay
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or this from the playground: http://playground.arduino.cc//Code/AvoidDelay
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
turtle gum
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: