SWOD: SweepWithOutDelay

Thought I'd share this delay()-less millis()-based servo sweep code.

Two independent servos are swept between setable limits, with setable degrees/increment, with setable ms between increment.

Includes #define for debugging.

All the user setable values are between 2 lines of code:

/////////   set the values below
/////////   set the values above
Rather grab newer code from Reply #1, very slight changes.

Slight changes on UKHeliBob’s suggestion. (Made the checking for the servos being at the limits into inequqlities.)

// Delay()-less millis()-based sweep for 2 servos, X and Y

// This example code is in the public domain.
// manor_royal Feb 2017

//idea is for two servos to go up and down ala sweep
//   BUT to interleave their motion

// in this one the servos can have diff ranges and speeds
// both of which hardcoded

//basically bwod thinking... "SWOD": SweepWithOutDelay


#include <Servo.h>

Servo servoX;
Servo servoY;

byte posX;
byte posY;

/////////   set the values below
// x range degrees
byte posXmin = 10;
byte posXmax = 170;
// y range degrees
byte posYmin = 50;
byte posYmax = 100;
//degrees per move.. gets *-1 immed for +1 first time
int stepX = -1;
int stepY = -1;
//millisec per update, larger is slower
int speedX = 25;
int speedY = 250;
//servo pins
byte servoXpin = 2;
byte servoYpin = 3;
/////////// set the values above

unsigned long currentMillis = 0;
unsigned long previousMillisX = 0; // the time when the servo was last moved
unsigned long previousMillisY = 0;


//  (un)-comment the following
//#define DEBUG //serial prints
#define DEBUG1 //led13
#define LCD_PRESENT_BUT_NOT_USED

void setup()
{
  posX = posXmin;
  posY = posYmin;

  servoX.write(posX);  //initialise
  servoY.write(posY);

  servoX.attach(servoXpin);
  servoY.attach(servoYpin);

#ifdef DEBUG
  Serial.begin(9600);
  Serial.println("setup " + String(posX) + " " + String(posY));
  delay(1000);
#endif

#ifdef DEBUG1
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
#endif

#ifdef LCD_PRESENT_BUT_NOT_USED
  pinMode(10, OUTPUT); //turn backlight off
#endif
}


void loop()
{
  currentMillis = millis();
  moveX();
  moveY();

#ifdef DEBUG
  delay(100);
#endif
}//loop

void moveX()
{
  if (currentMillis - previousMillisX >= speedX) //move if X time elapsed else nothing
  {
    previousMillisX = currentMillis;

    if ((posX <= posXmin) || (posX >= posXmax))
    {
      stepX = -1 * stepX; //change direction at limits
    }

    servoX.write(posX);
    posX = posX + stepX;

#ifdef DEBUG
    Serial.print("moveX " + String(posX) + " " + String(stepX));
#endif
  }//millis if
} //moveX

void moveY()
{
  if (currentMillis - previousMillisY >= speedY) //move if Y time elapsed else nothing
  {
    previousMillisY = currentMillis;

    if ((posY <= posYmin) || (posY >= posYmax))
    {
      stepY = -1 * stepY; //change direction at limits
    }

    servoY.write(posY);
    posY = posY + stepY;

#ifdef DEBUG
    Serial.println("  moveY " + String(posY) + " " + String(stepY));
#endif
  }//timing
}//moveY