need help controlling servos using millis in sequence.

i was wondering if anyone could give me an example sketch about how you would control 2 servos in sequence using millis so that other operations can continue to go on.

for example if it takes (servo.1)100ms to rotate 15degrees and (servo.2) 200ms to turn 30 degrees and i want to press a button for them to go off it would kind of be psuedo coded like

servo.2 rotate in 300ms on button = high
servo.1 rotate in 100ms on button = high

void setup(){
unsigned long Startservo1 = millis();
unsigned long Startservo2 = millis();
periodservo1 = 100;
periodservo2 = 300;
}

void loop(){
//do whatever starts servo1

if (millis() - Startservo1 =>periodservo1){
//stop servo 1
}
//do whatever starts servo2

if (millis() - Startservo2 =>periodservo2){
//stop servo 2
}

ty very much ill have to throw in a few numbers and edit it and get back to you how it works(eating dinner chinese food :slight_smile: )

const int buttonPin = 4; // the number of the pushbutton pin
const int servo1 = 6; // the number of servo1 pin
const int servo2 = 7; // the number of servo2 pin

void setup(){
// initialize the servo pins as outputs:
pinMode(servo1Pin, OUTPUT);
pinMode(servo2Pin, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

unsigned long Startservo1 = millis(//do i enter 100 here or leave blank?);
unsigned long Startservo2 = millis(//do i enter 200 here or leave blank?);
periodservo1 = 100;
periodservo2 = 300;
}

void loop(){
//do whatever starts servo1
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn servo.1 on:
digitalWrite(servo1Pin, HIGH);
}
else {
// turn servo1 off:
digitalWrite(servo1Pin, LOW);

if (millis(//is this where i enter 100?) - Startservo1 =>periodservo1){
//stop servo 1
}
//do whatever starts servo2

// turn servo2 on:
digitalWrite(servo2Pin, HIGH);
}
else {
// turn servo2 off:
digitalWrite(servo2Pin, LOW);

if (millis(//is this where i enter 300?) - Startservo2 =>periodservo2){
//stop servo 2
}

stated in some areas were im confused but how does this look so far? or am i doing something wrong(newbie here)

how does this look so far?

Does it work? The below site has servo code that uses millis.

my error message when i tried to verify the sketch.

sketch_jan03a.ino: In function 'void setup()':
sketch_jan03a:7: error: 'servo1Pin' was not declared in this scope
sketch_jan03a:8: error: 'servo2Pin' was not declared in this scope
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Arduino.h:105: error: too many arguments to function 'long unsigned int millis()'
sketch_jan03a:13: error: at this point in file
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Arduino.h:105: error: too many arguments to function 'long unsigned int millis()'
sketch_jan03a:14: error: at this point in file
sketch_jan03a:15: error: 'periodservo1' was not declared in this scope
sketch_jan03a:16: error: 'periodservo2' was not declared in this scope
sketch_jan03a.ino: In function 'void loop()':
sketch_jan03a:22: error: 'buttonState' was not declared in this scope
sketch_jan03a:28: error: 'servo1Pin' was not declared in this scope
sketch_jan03a:32: error: 'servo1Pin' was not declared in this scope
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Arduino.h:105: error: too many arguments to function 'long unsigned int millis()'
sketch_jan03a:34: error: at this point in file
sketch_jan03a:34: error: 'Startservo1' was not declared in this scope
sketch_jan03a:34: error: expected primary-expression before '>' token
sketch_jan03a:34: error: 'periodservo1' was not declared in this scope
sketch_jan03a:40: error: 'servo2Pin' was not declared in this scope
sketch_jan03a:42: error: 'else' without a previous 'if'
sketch_jan03a:44: error: 'servo2Pin' was not declared in this scope
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Arduino.h:105: error: too many arguments to function 'long unsigned int millis()'
sketch_jan03a:46: error: at this point in file
sketch_jan03a:46: error: 'Startservo2' was not declared in this scope
sketch_jan03a:46: error: expected primary-expression before '>' token
sketch_jan03a:46: error: 'periodservo2' was not declared in this scope
sketch_jan03a:48: error: expected `}' at end of input
sketch_jan03a:48: error: expected `}' at end of input

the sketch i tried to verify

const int buttonPin = 4;     // the number of the pushbutton pin
const int servo1 =  6;      // the number of servo1 pin
const int servo2 =  7;      // the number of servo2 pin

void setup(){
// initialize the servo pins as outputs:
  pinMode(servo1Pin, OUTPUT);   
  pinMode(servo2Pin, OUTPUT);

// initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 

unsigned long Startservo1 = millis(100);
unsigned long Startservo2 = millis(300);
periodservo1 = 100;
periodservo2 = 300;
}

void loop(){
//do whatever starts servo1
 // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn servo1 on:    
    digitalWrite(servo1Pin, HIGH);  
  } 
  else {
    // turn servo1 off:
    digitalWrite(servo1Pin, LOW); 

if (millis(100) - Startservo1 =>periodservo1){
//stop servo 1
}
//do whatever starts servo2

 // turn servo2 on:    
    digitalWrite(servo2Pin, HIGH);  
  } 
  else {
    // turn servo2 off:
    digitalWrite(servo2Pin, LOW); 

if (millis(300) - Startservo2 =>periodservo2){
//stop servo 2
}

the link you included uses potentiometers of which im not using so im not quite sure how to input a button instead, and change the millis to go off in sequence rather than all at once

i really am astonished i cant find an example sketch of sequenced servos controlled by a button....not a single search provider has found anything.

Maybe you can get some ideas from the attached code for the pushbutton stuff.

#include <Bounce.h>

// Debounce example http://playground.arduino.cc//Code/Bounce
// This code turns a led on/off through a debounced button
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button

#define BUTTON1 2  //pot1 up
#define BUTTON2 3  //pot1 down
#define BUTTON3 4  //pot2 up
#define BUTTON4 5  //pot2 down

#define LED 13

boolean lastState1 = LOW;
boolean lastState2 = LOW;
boolean lastState3 = LOW;
boolean lastState4 = LOW;

int pot1 = 0;
int pot2 = 0;

// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer1 = Bounce( BUTTON1,10 );
Bounce bouncer2 = Bounce( BUTTON2,10 );
Bounce bouncer3 = Bounce( BUTTON3,10 );
Bounce bouncer4 = Bounce( BUTTON4,10 );

//*********************************************
void setup()
{
  Serial.begin(9600);
  pinMode(BUTTON1,INPUT);
  digitalWrite(BUTTON1,HIGH);

  pinMode(BUTTON2,INPUT); 
  digitalWrite(BUTTON2,HIGH);

  pinMode(BUTTON3,INPUT);
  digitalWrite(BUTTON3,HIGH);

  pinMode(BUTTON4,INPUT); 
  digitalWrite(BUTTON4,HIGH);

  pinMode(LED,OUTPUT);

}          //END of setup()

//*********************************************
void loop()
{
  getSwitches();       // Check push buttons
  Serial.println(pot1);
  Serial.println(pot2);
  digitalWrite(LED, bouncer1.read());

}          //END of loop()

//*********************************************
void getSwitches()
{
  bouncer1.update ( );
  //******** 
  if(bouncer1.read() != lastState1)
  {
    lastState1 = bouncer1.read();
    pot1++;
    if(pot1 > 1023)
    {
      pot1 = 1023;
    }
  }
  bouncer2.update ( );
  //********  
  if(bouncer2.read() != lastState2)
  {
    lastState2 = bouncer2.read();
    pot1--;
    if(pot1 < 0)
    {
      pot1 = 0;
    }
  }
  bouncer3.update ( );
  //******** 
  if(bouncer3.read() != lastState3)
  {
    lastState3 = bouncer3.read();
    pot2++;
    if(pot2 > 1023)
    {
      pot2 = 1023;
    }
  }
  bouncer4.update ( );
  //********  
  if(bouncer4.read() != lastState4)
  {
    lastState4 = bouncer4.read();
    pot2--;
    if(pot2 < 0)
    {
      pot2 = 0;
    }
  }
}        //END of getSwitches()

stevethor7687:
i really am astonished i cant find an example sketch of sequenced servos controlled by a button....not a single search provider has found anything.

I'm really astonished you didn't read #7 below about code boxes. You probably will need to make your sketch of sequenced servos as it contains several different parts. You probably can start by making code with a button push response like below, then add additional code to slow the servo movement using incremental servo position commands (similar to the servo "sweep" example code).

http://forum.arduino.cc/index.php/topic,148850.0.html

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
Servo servo1;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  servo1.attach(8); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      servo1.write(20);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      servo1.write(160);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

zoomkat:

stevethor7687:
i really am astonished i cant find an example sketch of sequenced servos controlled by a button....not a single search provider has found anything.

I'm really astonished you didn't read #7 below about code boxes. You probably will need to make your sketch of sequenced servos as it contains several different parts. You probably can start by making code with a button push response like below, then add additional code to slow the servo movement using incremental servo position commands (similar to the servo "sweep" example code).

http://forum.arduino.cc/index.php/topic,148850.0.html

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
Servo servo1;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  servo1.attach(8); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      servo1.write(20);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      servo1.write(160);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

ive tried converting this sketch before into millis and i was unsuccessful, also i read the forum guidance over two weeks ago and didnt remember to use the code button as im new to using forums so im still getting use to all the setups

ive tried multipe different ways to input a button and i just cant get it to verify.

void setup(){
unsigned long Startservo1 = millis();
unsigned long Startservo2 = millis();
periodservo1 = 100;
periodservo2 = 300;
}

void loop(){
//do whatever starts servo1

if (millis() - Startservo1 =>periodservo1){
//stop servo 1
}
//do whatever starts servo2

if (millis() - Startservo2 =>periodservo2){
//stop servo 2
}
void setup(){
unsigned long Startservo1 = millis();
unsigned long Startservo2 = millis();
periodservo1 = 100;
periodservo2 = 300;
}

void loop(){
//do whatever starts servo1

if (millis() - Startservo1 =>periodservo1){
//stop servo 1
}
//do whatever starts servo2

if (millis() - Startservo2 =>periodservo2){
//stop servo 2
}

Startservo1 and Startservo2 only exist in setup()
They go out of scope when execution proceeds to the loop() function.

void setup(){
unsigned long servo1 = millis();
unsigned long servo2 = millis();
periodservo1 = 100;
periodservo2 = 300;
}

void loop(){
//do whatever starts servo1

if (millis() - servo1 =>periodservo1){
//stop servo 1
}
//do whatever starts servo2

if (millis() - servo2 =>periodservo2){
//stop servo 2
}

thats what i have now is this what you meant by the startservo error? im still unsure how to write a button in but ill post that example in a second too i guess also when ever i see work done with a servo i always see the statement "#include servo.h" at the top of the sketch is this needed for this sketch? what does that statement mean?

always see the statement "#include servo.h" at the top of the sketch is this needed for this sketch?

This is a required library you need to get your sketch to control your servo motor.

Let us do some basic review.

1.) Before you attempt to experiment with writing your own sketch, try this example:
http://arduino.cc/en/Tutorial/Sweep
Can you get your servo to sweep back and forth with the above sketch?
Do you understand how the sketch works and do you have any questions how it works?

2.) Below is a sketch that does not use the delay() function to do 2 things every now and then.
Do you have any questions how it works?

const unsigned long TaskAtime  = 100UL;  //Runs TaskA every 1/10 second
const unsigned long TaskBtime  = 1000UL; //Runs TaskB every 1 second

unsigned long TimeA;                     //Times up, Task A time
unsigned long TimeB;                     //Times up, Task B time
// etc.



//========================================================== 
void setup()
{
  TimeA = millis();                      //Initailize  
  TimeB = TimeA;                         //Initialize

  pinMode(13,OUTPUT);                    //
  pinMode(12,OUTPUT);                    //

} //        >>>>>>>>>>>>>> END OF setup() <<<<<<<<<<<<<<<<<


void loop()
{
  unsigned long millisNow = millis();

  //==================
  if (millisNow - TimeA >= TaskAtime) //Is it time to run Task A?
  {
    TimeA = millis();                 //Re-initialize
    TaskA();
  } 

  //==================
  if (millisNow - TimeB >= TaskBtime) //Is it time to run Task B?
  {
    TimeB = millis();                 //Re-initialize
    TaskB();
  }  

   //==================

  //Other stuff goes here


} //        >>>>>>>>>>>>>> END OF loop() <<<<<<<<<<<<<<<<<



// FUNCTIONS
//========================================================== 

void TaskA()
{
  digitalWrite(13,!digitalRead(13));   //Toggle pin 13
  //Other stuff

}

//==================
void TaskB()
{
  digitalWrite(12,!digitalRead(12));   //Toggle pin 12
  //Other stuff

}


//======================================================================
//                             END OF CODE
//======================================================================

3.) Have you run the Button sketch that comes with the Arduino IDE: