Non-blocking code using millis to sweep servo more slowly

Hello,
I am adding electronics to my scratch-build models. I have an Arduino Every with a micro servo attached. I used sweep code to verify the hardware and connections work. The servo runs landing gear and with basic code they move too fast from 25 degrees to 100 and back. This code is my attempt at non-blocking code to just test the landing gear and see if I can get them to slow down. Then I'll add it to more code later with remote control as void () landing gear; That's a whole other ball of wax...
I set the servo to 25 degrees and start the following code and it moves quickly to 100 degrees and stops. I know I had it working once, but maybe that was different code. I have a thick skull when it comes to logic, so I was hoping someone could point me in the right direction. Thank you in advance. Here's the code I hope I added it correctly to the post:

#include <Servo.h>

  //some global variables available anywhere in the program
unsigned long currentMillis;
int interval = 50;
unsigned long previousMillis;
int current = 25;
int target = 100;
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

void update()
    {
      if (millis() - previousMillis > interval)
      {
        previousMillis = millis();
        if (target < current)
        {
          current--;
          myservo.write(current);
        }
        else if (target > current)
        {
          current++;
          myservo.write(current);
        }
      }
    }

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

}

void loop() {
     update;  
             
}

  • So everyone can get on the same page, show us your schematic for the above sketch.

update; //NO

  • This is the way you call a function.

update(); //YES

Are you also looking for a means to initiate extending and retracting, like a push-button, or will these extend and retract on power-up?

look this over.
can add more servos with different parameters

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

struct Ctl {
    const byte    PinServo;
    const byte    PinBut;

    const int     Pos0;
    const int     PosN;
    const int     MsecSweep;

    const char   *desc;

    unsigned long msecPeriod;

    int           pos;
    unsigned long msec;
    Servo         servo;
};

Ctl ctl [] = {
 //   servoPin, swPin, pos0, posN, msecSwp
    {        9,    A1,   25,   35,    3000, "servo 1" },
};
const int Nctl = sizeof(ctl) / sizeof(Ctl);

const byte PinBut = A1;

unsigned long msec;

// -----------------------------------------------------------------------------
void update (
    int  n)
{
    if (msec - ctl [n].msec >= ctl [n].msecPeriod)  {
        ctl [n].msec += ctl [n].msecPeriod;

        if (LOW == digitalRead (ctl [n].PinBut))  {
            if (ctl [n].pos > ctl [n].Pos0)  {
                ctl [n].servo.write (--ctl [n].pos);

                Serial.print   ("update: ");
                Serial.print   (n);
                Serial.print   (" ");
                Serial.print   (ctl [n].pos);
                Serial.print   (" ");
                Serial.println (ctl [n].desc);
            }
        }
        else {
            if (ctl [n].pos < ctl [n].PosN)  {
                ctl [n].servo.write (++ctl [n].pos);

                Serial.print   ("update: ");
                Serial.print   (n);
                Serial.print   (" ");
                Serial.print   (ctl [n].pos);
                Serial.print   (" ");
                Serial.println (ctl [n].desc);
            }
        }
    }
}

// -----------------------------------------------------------------------------
void loop () {
    msec = millis ();

    update (0);
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    for (int n = 0; n < Nctl; n++)  {
        ctl [n].msecPeriod  = ctl [n].MsecSweep / (ctl [n].PosN - ctl [n].Pos0);
        ctl [n].pos         = ctl [n].PosN;
        ctl [n].servo.attach (ctl [n].PinServo);
        ctl [n].servo.write  (ctl [n].pos);
        Serial.println (ctl [n].desc);
    }
}

What is the servo position for gear UP? For gear DOWN?
When the program starts, where should the gear be?

Hey gcjr,
I'm sure the code works great but you completely lost me! I don't know what struct is just for starters. Is Ctl ctl [ ] an array? I'll spend more time looking at it.

After fixing that call function error, it moves slowly like I want but still stops at 100 degrees.

Servo is 100 degrees for gear down, 25 for gear up. That's not the problem. Like I said, using the regular sweep code, and obviously putting in 100 and 25, works perfectly. It's just too fast.

Oh sorry, I want the gear up when it starts, which is 25 degrees.

xfpd,
I'm just trying to figure out how to slow the servo down. One step at a time. After I wrap my head around that I can use ir remote button and re-write. This is just a test code for slowing down servo.

Larry,
To post a schematic, do I attach a jpeg of sketch?

Hey everyone,
I'm an absent-minded dunderhead. I found the code that works I based on someone else's idea I had adapted for my test. I'm coming back to this after a couple months off and looked at my flawed first attempt. In case a newbie like me has the same problem, here's the final code that works.

#include <Servo.h>

Servo myservo;

int position = 25;
unsigned long ts = millis();
unsigned long interval = 50;
boolean forward = false;

void setup() 
{
  myservo.attach(9);
}

void loop()
{
    if (millis() - ts >= interval)
    {
      ts += interval;  //setup timestamp for next time
      if (forward)
      {
        myservo.write(-- position);  //progress servo
        if (position == 25)  //test for reverse
          forward = false;
      }
      else
      {
        myservo.write(++ position);  //progress servo
        if (position ==100)  //test for reverse
          forward = true;
      }
    }
}




  • Yes.

  • Sounds like things are under control; in the future it’s always best to show us a schematic and images of your project so we can be brought up to speed.

Have fun

instead of separate arrays for each thing, a struct defines a group of variables and you can have an array of that struct. all the values in an array of a struct can be initialized, one struct on each line