Servo Speed Control

Hello! I have a project I am just about wrapped up on, but I need a bit of help on the code.

Questions TL:DR at bottom

//Mosquito Girl Wing Program
// This code is designed to control two servos with the purpose of being used as cosplay wings.
// This system takes input from two (2) momentary buttons worn on the costume. 
// Upon pressing "button", the wings will toggle from a closed to open position, and back when pressed again.The default and startup position will be down
// Upon pressing "buttonFlutter", the servos will generate a flutter like action on the wings and will return to the position in which they started the flutter cycle.
// CAUTION: Improper activation of the wings can cause damage to the cosplay or those surrounding the wings. Before turning on the wings, ensure that the movement
//          path is clear and the wings are as close to the home position as possible. Wings will close on startup.

#include <Servo.h>

int button = 12; //Toggle button pin, connect to ground to move servo
int buttonFlutter = 8; // Flap Action button
int press = 0;
int pressFlap=0;
Servo servo1;
Servo servo2; //servo1 is left, servo2 is right
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); // Toggle button monitored
  pinMode(buttonFlutter, INPUT); // Flap activate monitored
  servo1.attach(9); // Left Servo
  servo2.attach(4); // Right Servo
  digitalWrite(12, HIGH); //enable pullups and set pin high
  digitalWrite(8, HIGH); 
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo1.write(180); // Toggle angle all the way open
      servo2.write(0);
      toggle = !toggle;
    }
    else
    {
      servo1.write(90); // Initial position, down
      toggle = !toggle;
    }
  }
    
  pressFlap = digitalRead(buttonFlutter); // flutter function
  if (pressFlap = LOW)
  {
    if(toggle) // If started down end down
    {
      servo1.write(180);
      servo2.write(0);
      delay(500);
      
      servo1.write(135);
      servo2.write(45);
      delay(500);
      
      servo1.write(180);
      servo2.write(0);
      delay(500);
      
      servo1.write(135);
      servo2.write(45);
      delay(500);
      
      servo1.write(90); // end down left
      servo2.write(90); // end down Right
      delay(500);
      
    }
    else // if started up, end up
    {
      servo1.write(135);
      servo2.write(45);
      delay(500);
      
      servo1.write(180);
      servo2.write(0);
      delay(500);
      
      servo1.write(135);
      servo2.write(45);
      delay(500);
      
      servo1.write(180);
      servo2.write(0);
      delay(500);
      
    }
  }
  delay(500);  //delay for debounce
}

As the comments in it said, there are two buttons, and two servos. One button "button" when pressed is just a simple toggle for the servos to move between two positions. The second button "buttonFlutter" when pressed will cause the servos to make a slow flapping motion as they are attached to a pair of large wings. As of right now, I have the toggle working just as I wanted it to, but need to program some kind of speed control to the flap motion. The wings look very weird flapping at full speed and can be easily damaged. How can I slow down the actual movement speed of these servos?

QUESTION TL;DR How do I slow down the speed of these servos on a teensy 3.2

Have you seen the servo sweep tutorial?

vinceherman:
Have you seen the servo sweep tutorial?

Yea, I believe you mean this one here

http://arduino.cc/en/Tutorial/Sweep

I might be reading that code wrong, so lemme see if I understand that correctly. Thats setting a for loop that drives the servo from 180 to 0 at a set speed. Does this work if the servo is not at 180? do I need to write a for loop for every write.servo in my code or in the initial for loop do I write in that constriction?

Check out the VarSpeedServo.h library. I'm not sure if it works on a Teensy but it does exactly what you need.

You can always do it yourself using the Sweep technique but you will need sweep-style loops for all the wing flapping servo moves (so I expect you'd write a function to do it for you).

Steve

slipstick:
Check out the VarSpeedServo.h library. I'm not sure if it works on a Teensy but it does exactly what you need.

You can always do it yourself using the Sweep technique but you will need sweep-style loops for all the wing flapping servo moves (so I expect you'd write a function to do it for you).

Steve

Yea, I will probably have to go with a function. I gave Var Speed a shot but it does not work with teensy much to my sadness. Any tips on how to write the function? That is a bit new for me

If you don't need it to run concurrently with anything else, then you can just do a while loop in the function and continually update the servo positions for the duration of the movement.

// Positions are in degrees 0-180, duration is in milliseconds
void SweepServos(int start1, int end1, int start2, int end2, int duration)
{
    int startTime = millis();
    int curTime = 0;

    while(curTime < duration)
    {
        servo1.write(start1 + (end1 - start1) * curTime / duration);
        servo2.write(start2 + (end2 - start2) * curTime / duration);
        curTime = millis() - startTime;
    }
    servo1.write(end1);
    servo2.write(end2);
}