Servo Library Reattach Servo in void look()

Hey guys,
I am working on a project with a servo and want to detach it at certain stages in the programm in order to manually move the servo. The detach function works but when I use myservo.attach() in the void loop it wont work.
What am I doing wrong? Does the attach function not work in loop?

What am I doing wrong?

How the Hell do we know?
You didn't post any code.

When you do, use code tags.

I am sorry, here is the code. I am using it just for testing right now to figure out why the servo wont work

#include <Servo.h> 
#include <Adafruit_NeoPixel.h>
#define PIN 10


Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);

Servo myservo;

int PWI = 2; //POwer Connection Indicator
int PWS = 3; //is Mainboard turned on Indicator
boolean  windowsbooted = 0;
int uppos = 30;
int downpos = 0;
boolean servosetback = 0;

void setup() {
  myservo.attach(5);
   pinMode(PWI, INPUT);
   pinMode(PWS, INPUT);
  Serial.begin(9600);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  delay (50);
   myservo.write(0);
    delay (1000);
}

void loop() {

  myservo.write(uppos);
  Serial.println("Servo Uppos");
  delay(200);
  myservo.detach();
  Serial.println("Servo Detached");
  myservo.write(downpos);
  delay(2000);
  myservo.attach(5);
  Serial.println("Servo reatached");
  delay(2000);
  myservo.write(downpos);
  Serial.println("Servo Downpos");
  delay(2000);
//  
//  if(digitalRead(PWS) == HIGH && windowsbooted == 0){
//     //myservo.write(uppos);
//  colorWipe(strip.Color(60, 30, 0), 50);   
//  }
//  
//    if(digitalRead(PWS) == HIGH && windowsbooted == 0){
//  myservo.write(uppos);
//    servosetback = 0;
//  colorWipe(strip.Color(0,0 , 30), 50); 
//  }
//      if(digitalRead(PWS) == HIGH && windowsbooted == 0){
//   myservo.write(uppos);
//     servosetback = 0;
//  colorWipe(strip.Color(60, 30, 0), 50);   
// 
//  }  
//    if(digitalRead(PWS) == HIGH && windowsbooted == 0){
//  myservo.write(uppos);
//    servosetback = 0;
//  colorWipe(strip.Color(0,0 , 30), 50); 
// windowsbooted = 1;
// colorWipe(strip.Color(60, 30, 0), 50);  
//  }
// 
//  
//  if(digitalRead(PWS) == HIGH && windowsbooted == 1){
//  myservo.write(uppos);
//    servosetback = 0;
//
//  }
//
//  
//  
//  if(digitalRead(PWS) == LOW && servosetback == 0){
//  windowsbooted = 0; 
//  myservo.write(0);
//  colorWipe(strip.Color(0,0 , 0),10 );
//  servosetback = 1;  
//  }
//  
//  
//
//  
// 
}













void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

So, what happens?
What doesn't happen that you expect to happen?

Like I said the attach function for some reason wont work.

I am calling it in void loop after I detached the servo. I figured after calling attach I would be able to move the servo again but that doesnt work. The serial communication is just for debugging, so I see where I am at in the programm.

How do you know the attach() doesn't work? It doesn't do anything of itself.
What are the symptoms?
What actually happens?
And what should happen?

...R

After I call attach I am moving the servo to its zero position. However that doesn't work.
It just stays there and does nothing.

You only allow 1/5th of a second for the servo to reach "uppos" - is that enough?

Yes that is enough. I am Using a retrct servo for certain reasons and it only allows for either going to 0° or 180° nothing inbetween.
So i am only letting it move 1/5th of a second in order to move it around 45 degrees. That works pretty well so far.

I modified your code a little so it is (hopefully) clearer what is happening.

I think your "problem" is that the write(downpos) that is sent while the servo is detached is acted upon immediately it is reattached.

#include <Servo.h> 

#define PIN 10

Servo myservo;


int uppos = 120;
int downpos = 20;
boolean servosetback = 0;

void setup() {
  myservo.attach(5);
 
  Serial.begin(9600);

  delay (50);
   myservo.write(0);
    delay (1000);
}

void loop() {

  Serial.println("Servo Uppos");
  myservo.write(uppos);
  
  delay(2000);
  myservo.detach();
  Serial.println("Servo Detached");
  Serial.println("Try Downpos");
  myservo.write(downpos);
  delay(2000);
  myservo.attach(5);
  Serial.println("Servo reattached");
  delay(2000);
  myservo.write(downpos);
  Serial.println("Servo Downpos");
  delay(2000);
  myservo.write(uppos);
  Serial.println("Servo Uppos");
  delay(2000);
}

...R

I don't think servo position can be controlled reliably by limiting execution time.

I think you need to explore other ways to get 45 degrees

http://playground.arduino.cc/ComponentLib/servo

Some servo test code that includes the attach/detach function.

// zoomkat 12-25-13 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// Send an a to attach servo or d to detach servo
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h> 
String readString; //String captured from serial port
Servo myservo;  // create servo object to control a servo 
int n; //value to write to servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo all-in-one test code 12-25-13"); // so I can keep track of what is loaded
  Serial.println();
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 

      // attach or detach servo if desired
    if (readString == "d") { 
      myservo.detach(); //detach servo
      Serial.println("servo detached");
      goto bailout; //jump over writing to servo
    }
    if (readString == "a") {
      myservo.attach(7); //reattach servo to pin 7
      Serial.println("servo attached");
      goto bailout;
    }    

    n = readString.toInt();  //convert readString into a number

    // auto select appropriate value
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n); 
    }

bailout: //reenter code loop
    Serial.print("Last servo command position: ");    
    Serial.println(myservo.read());
    Serial.println();
    readString=""; //empty for next input
  }
}