Pages: [1] 2   Go Down
Author Topic: millis() as delay()  (Read 1398 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

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

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

any help would be greatly appreciated.. thanks^^


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25626
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read the blink without delay example
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

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

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 3937
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

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

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

Code:

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

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12518
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

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

Quote
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

Code:
// 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?
« Last Edit: February 06, 2012, 08:19:16 pm by snapshots » Logged

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

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



Code:
/ 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?
Logged

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

anyone?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25626
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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

Code:
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).
Logged


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

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

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

Code:
/ 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);
  }
}



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25626
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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()?

Code:
for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
Comment should match the code, or should be omitted altogether.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

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

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

Code:
Why not use delay()?

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

Pages: [1] 2   Go Up
Jump to: