millis() as delay()

Hi im working on with different motors and found out that delay pauses everything turning the other motor idle. now i need to change all delays into millis() so all the motors can function well..

can u teach me regarding millis()?.. say for example the sweep function in arduino.. i googled about it and everyone is pointing at blinkwithoutdelay sketch but i still don't get it and don't know how to use it.

from this code

 for(pos = 0; pos < 180; pos += 1)  
  {                                  
    myservo.write(pos);             
    delay(15);       
  } 
  for(pos = 180; pos>=1; pos-=1)    
  {                                
    myservo.write(pos);             
    delay(15);                     
  }

to

//sweep servo from 0 - 180 degrees and wait 15 ms using delay --- it's ok to use delay here

//wait for 2 seconds here using millis --- a must here to use millis

//sweep servo from 180 - 0 degrees and wait 15 ms using delay --- it's ok to use delay here

//wait for 2 seconds here using millis --- a must here to use millis

i tried for led and it works but in motors no.. here's a sample..

if (digitalRead(buttonPin)==HIGH){
  timer = millis();
  digitalWrite(ledPin, HIGH);
}
 if(millis()-timer >= 2000){
   digitalWrite(ledPin,LOW);
  
  
}

any help would be greatly appreciated.. thanks^^

Read the blink without delay example

i already read it and i don't quite understand it.. can u show me a sample of how can i make use of it as a delay?

PaulS' analogy of using a stopwatch is a very good one:

millis() is your stopwatch. Assigning a value to a variable is like writing a number down so it can be referenced later.

PaulS' analogy of using a stopwatch is a very good one:
http://arduino.cc/forum/index.php/topic,53951.msg386055.html#msg386055

millis() is your stopwatch. Assigning a value to a variable is like writing a number down so it can be referenced later.

thanks for the reference.. i already made changes to the sweep function..

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#include <Servo.h> 
 
long intervalmotor = 1500;
long previousmillis = 0;
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
if(millis()-previousmillis > intervalmotor)
 {
  
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {     
   
  
     previousmillis = millis();
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                    // waits 15ms for the servo to reach the position 
  } 
  }
  
if(millis()-previousmillis > intervalmotor)
 {
  
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {          

     previousmillis = millis();
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
  }             // waits 15ms for the servo to reach the position 
  } 
}

it works but not perfect.. when it reaches 180 it jitters a little before moving back to 0.. so as when it reaches 0 it jitters before moving to 180 degrees.. how can i enhance it so to remove the jittery?.. thanks

snapshots:
it works but not perfect.. when it reaches 180 it jitters a little before moving back to 0.. so as when it reaches 0 it jitters before moving to 180 degrees.. how can i enhance it so to remove the jittery?.. thanks

I'm surprised it works. I'd expect both those for loops to spin as fast as the code can execute and slam the servo to the end position.

If you're trying to move the servo by a small amount and then delay briefly before moving it again, ditch the for loops and make the loop counter a global variable, define your interval to be the delay between steps of the servo, and use that same basic approach to increment the servo by one position each time the interval has passed.

If you're trying to move the servo by a small amount and then delay briefly before moving it again, ditch the for loops and make the loop counter a global variable, define your interval to be the delay between steps of the servo, and use that same basic approach to increment the servo by one position each time the interval has passed.

thanks here's what i've come up.. it now moves but it does not pause for 1 second when it reaches 125 and 55.. it must first pause for 1 second when it reaches the max angle 125..

what i want to achieve..
pseudo code:

//increment until it reaches 125
//pause for a second
//decrement until it reaches 55
//pause for a second

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#include <Servo.h> 

int move = 0;
long stepsinterval = 10;
long timepause = 1500;
long previousmillis = 0;
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
  
  if(millis()-previousmillis >= stepsinterval)
 {
  previousmillis = millis();
      
      if (move ==  1)
      { 
        pos++;
        if (pos >= 125) 
        move = 0; 
        myservo.write(pos);
        }
        if (move == 0)
        { 
        pos --;
        if (pos <= 55)  
        move = 1; 
        myservo.write(pos);   
        }
  }
}

how can i ehnance more my code so it will pause for a second when it reaches 125 or 55?

Here's the update.. i added a pause function so it will waste some time before moving again.

/ Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#include <Servo.h> 

int move = 0;
long stepsinterval = 10;
long timepause = 1500;
long previousmillis = 0;
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);
Serial.begin(9600);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
  
  if(millis()-previousmillis >= stepsinterval)
 {
  previousmillis = millis();
      
      if (move ==  1)
      { 
        pos++;
        if (pos >= 125) 
        move = 0; 
        myservo.write(pos);
        if (pos==125) pause();
        }
        if (move == 0)
        { 
        pos --;
        if (pos <= 55)  
        move = 1; 
        myservo.write(pos);
         if (pos==55) pause();   
        }
  }
}
void pause(){
  if(millis()-previousmillis >= timepause)
  previousmillis = millis();
  for(int x= 0; x<=100; x++){
    Serial.println(x,DEC);
}
}

Or is there another way so i won't need to make a pause function?

anyone?

Consistent indentation would help to show what's going on.
Try using the auto format tool before posting code.

What is the point of the "pause()" function?

The way I do it, which may make more sense than the blink example :

 if (time >= loop1 ){ 
    loop1 = time + 250;  // come back in a quarter second
    if (time > 60000){  // only do this bit if its been running more than a minute
      touchpadvaluemin = min(touchpadvaluemin,analogRead(tpPin));
    };
    if (led1){ 
      digitalWrite(powerled, HIGH); 
      led1 = false; 
      blinksdone ++;
    }
    else
    { 
      digitalWrite(powerled, LOW); 
      led1 = true;
    };
    if (blinksdone >= blinks){
      if (led1){
        loop1 = time + 1250;  // after requisite number of  blinks quarter a second apart, leave a longer pause before starting back on the quarter seconds
        blinksdone = 0;
      }
    }
  }

At the beginning of the main loop is the line

time=millis();

All the variables (time and loop1) involved in the timing are of type unsigned long;

You can have any number of sub processes like the above running at all different intervals (use say loop2 etc etc for the second etc etc sub process).

Consistent indentation would help to show what's going on.
Try using the auto format tool before posting code.

ok sorry about that...

I made my own pause function.. it pauses the servo for sometime before moving again just like delay..

like this..

modified sweep code..

for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 

delay(1000);  // pause for 1 second before executing the code below


  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 

delay(1000);  // pause for 1 second

instead of using delay i can substitute it with the function i created.. pause();
i just don't know if i did it the right way..

here's the code with indention..

/ Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#include <Servo.h> 

int move = 0;
long stepsinterval = 10;
long timepause = 1500;
long previousmillis = 0;
Servo myservo;  // create servo object to control a servo 
// a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(9);
  Serial.begin(9600);  // attaches the servo on pin 9 to the servo object 
} 


void loop() 
{ 

  if(millis()-previousmillis >= stepsinterval)
  {
    previousmillis = millis();

    if (move ==  1)
    { 
      pos++;
      if (pos >= 125) 
        move = 0; 
      myservo.write(pos);
      if (pos==125) pause();
    }
    if (move == 0)
    { 
      pos --;
      if (pos <= 55)  
        move = 1; 
      myservo.write(pos);
      if (pos==55) pause();   
    }
  }
}
void pause(){
  if(millis()-previousmillis >= timepause)
    previousmillis = millis();
  for(int x= 0; x<=100; x++){
    Serial.println(x,DEC);
  }
}

I made my own pause function.. it pauses the servo for sometime before moving again just like delay..

Does it?
The only pause is the one generated by the time it takes to transmit a stream of characters via serial.
Why not use delay()?

for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees

Comment should match the code, or should be omitted altogether.

Thanks Pluggy gotta try that out and post the code later if i can get it to work..

Why not use delay()?

Bcoz it will stop everything.. so what im doing is changing all delays to millis()..

Bcoz it will stop everything..

So will sending a long stream of characters via the serial port.

So will sending a long stream of characters via the serial port.

yeah i've notice that.. any other way for me to do it?

Hi, Take a look at Simon Monk's timer library: http://srmonk.blogspot.com/