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