servo control

I am trying to program my board to control butterfly wings. I want the wings to start closed and then flash open one time like a real butterfly. My code thus far is below.

Questions:
(1)
Can I insert a line so that I can control how quickly/slowly the wings open?

(2)
Rather than using the delay(), can I insert a line to open the wings using a manual switch so that I can control exactly when to open the wings?

(3) With this code, the wings move slightly before it starts in the closed position. Is there a way to get rid of this initial movement?

// wings open

#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int input_p=0;
int myCounter = 0;

void setup()

{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(8, INPUT);
}

void loop()
{
input_p = digitalRead(8);

if(myCounter<1) { // only open wing one time
{

myservo.write(0); // 0=wings completely closed

delay(5000); //Wait 5 seconds before opening wing

myservo.write(100); // 100=wings completely open

myCounter = myCounter + 1;

}
}
}

Get rid of 8)
Attach your code using the </> icon on the left side of the posting menu.

If you use a loop and a counter 0-100 with BWD delay, you can increment the counter and position the servo to that counter.

A switch can be monitored, when it changes to a HIGH then you proceed to opening the wings.

Thanks for the response. I’m new to this type of coding so any detailed help would be appreciated.

I attached the code. I added in the HIGH line but that didn’t change the movement.

Also, what exactly do you suggest for incrementing the counter?

ButterflyOpening3_Code.ino (651 Bytes)

Some servo button test code previously posted you can try. You might change one the incremental sections to the usual direct positioning for fast return to an initial position.

#include <Servo.h> 
 
Servo myservo; 
#define leftPin 2    //Active Low
#define rightPin 3   //Active Low
int pos = 90;
int delayPeriod = 50;  // increasing this slows down the servo movement

 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo.write(pos); // center the servo
  pinMode(leftPin, HIGH);   // turn on pullup resistors
  pinMode(rightPin, HIGH);
} 
 
 
void loop() 
{ 
  if(digitalRead(leftPin) == LOW)  
  {                              
   // in steps of 1 degree 
   if( pos > 0)
      --pos;
    myservo.write(pos);  // tell servo to go to position in variable 'pos' 
    delay(delayPeriod);                      
  } 
  if(digitalRead(rightPin) == LOW)  
  {                              
   if( pos < 180)
       ++pos;
    myservo.write(pos); // tell servo to go to position in variable 'pos' 
    delay(delayPeriod);        
  }
}

Maybe you can get some ideas out of this sketch (you will have to add your servo code):

const byte LED               = 10;
const byte OpenSwitch        = 8;
const byte CloseSwitch       = 9;
unsigned long Millis13;
unsigned long SwitchMillis   = millis();
unsigned long DoorMillis    = 4294967295ul; // Start out at maximum, thanks CR
unsigned long currentMillis  = millis();
unsigned long debounceMillis = 100;
boolean lastOpenSwitchState  = false;
boolean lastCloseSwitchState = false;
boolean openCMD   = false; // Open switch was pushed
boolean closeCMD  = false; // Close switch was pressed
boolean DoorState = false; //indicates if wings are closed = false


void setup() 
{ 
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(LED,LOW);
  pinMode(OpenSwitch, INPUT_PULLUP);  //when switch is pressed a read gives LOW
  pinMode(CloseSwitch, INPUT_PULLUP);

  //Your servo close code
  //delay(5000);
} 

void loop() 
{ 
  //leave this line of code at the top of loop()
  currentMillis = millis();

  //***************************
  //Check to see if sketch is blocking
  //toggle pin 13 every 200mS
  if (CheckTime(Millis13, 200UL, true))
  {
    //toggle pin 13
    digitalWrite(13,!digitalRead(13));
  }

  //***************************
  //is it time to check the switches?
  if (CheckTime(SwitchMillis, debounceMillis, true))
  {
    checkSwitches();      
  } 

  //***************************
  //Is the door/servo moving. If you get a true the it has stopped
  if(CheckTime(DoorMillis,10000UL, false)) // 10 seconds
  {
    //***************************
    if(openCMD && !DoorState)
    {
      //Your servo open code

      DoorState = true;
      DoorMillis = millis();
      Serial.println("Door is Opening");
      digitalWrite(LED,HIGH);
    }

    //***************************
    if(closeCMD && DoorState)
    {
      //Your servo close code

      DoorState = false;
      DoorMillis = millis();
      Serial.println("Door is Closing");
      digitalWrite(LED,LOW);
    }
    
  } //END of, Is the door/servo moving. If you get a true the it has stopped

} //END of loop()

//**********************************************************************
//Delay time expired function
//lastMillis = time we started, wait = delay in mS, restart = do we start again  
boolean CheckTime(unsigned long &lastMillis, unsigned long wait,boolean restart) 
{
  //is the time up for this task?
  if (currentMillis - lastMillis >= wait) 
  {
    //should this start again? 
    if(restart)
    {
      //get ready for the next iteration
      lastMillis += wait;  //get ready for the next iteration
    }
    return true;
  }
  return false;

} // END of CheckTime()

//**********************************************************************
//switches are checked every debounceValue milli seconds 
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches()  
{
  //re-usable for all the switches  
  boolean thisState;    

  //***************************    
  //check if this switch has changed state
  thisState = digitalRead(OpenSwitch);
  if (thisState != lastOpenSwitchState)
  {  
    //update the switch state
    lastOpenSwitchState = thisState;  

    //this switch position has changed so do some stuff

    //"HIGH condition code"
    //switch goes from LOW to HIGH
    if(thisState == HIGH)        
    {
      openCMD = false; // the switch was released
    }

    //"LOW condition code"
    //switch goes from HIGH to LOW
    else                          
    {
      openCMD = true; // the switch was pressed
    }

  } //END of OpenSwitch

  //***************************    
  //check if this switch has changed state
  thisState = digitalRead(CloseSwitch);
  if (thisState != lastCloseSwitchState)
  {  
    //update the switch state
    lastCloseSwitchState = thisState;  

    //this switch position has changed so do some stuff

    //"HIGH condition code"
    //switch goes from LOW to HIGH
    if(thisState == HIGH)        
    {
      closeCMD = false; // the switch was released
    }

    //"LOW condition code"
    //switch goes from HIGH to LOW
    else                          
    {
      closeCMD = true; // the switch was pressed
    }

  } //END of CloseSwitch

} //END of checkSwitches()

The Threads planning and implementing a program and several things at a time include code for moving a servo at a controlled speed.

I strongly recommend breaking your code into small functions as illustrated in planning and implementing ...

...R