Simple Coding for a Servo: Is this correct format?

Is this the correct format to tell a servo to go to a certain degree mark and hold?

  {    
     pos = 91;    
    myservo.write(pos);          
    delay(23);

Right now I think... that's telling the servo to go to 91 degrees and hold. I believe it's working but it seems like I hear the servo chattering (position holding I assume). Am I doing something wrong or is this normal behavior?

Is there a way to tell it to not hold position? The reason being that I leave the circuit on and I'd rather not burn the motor out while it's resting since there's obviously no need to leave it holding position.

thanks in advance! :)

Is this the correct format to tell a servo to go to a certain degree mark and hold?

Yes.

I believe it's working but it seems like I hear the servo chattering (position holding I assume)

A servo is an electric motor with some control hardware to turn it off at a specific position. If the servo drifts from that position, it gets driven back to that position.

Is there a way to tell it to not hold position?

Servo.detach();

The reason being that I leave the circuit on and I'd rather not burn the motor out while it's resting since there's obviously no need to leave it holding position.

Really? It's not obvious to me. Usually servos DO something. That means holding a specific position.

Perfect and thanks! Is there a way to reattach the servo? I can detach it but then reattaching it I have to restart the program to get it to reattach. I’d like to reattach it within the main void loop.

Just call attach again

Hmmm, I called the attach again in the main loop:

void loop()  
{
 myservo.attach(9);

...

and when I compiled it gave an error. to give a background, there were no errors in compiling before I put this in the main loop. So something’s not being liked with this code.

So something’s not being liked with this code.

void loop()
{
 myservo.attach(9);

[glow]...[/glow]

The compiler doesn’t like the highlighted bit.

That was just representing the ... (or etc etc) worth of functional code that rambles on from there.

That was just representing the ... (or etc etc) worth of functional code that rambles on from there.

It has a hint, too. But, since you missed it, I'll spell it out. Post all of your code.

It has a hint

It has indeed*, thanks PaulS.
There was nothing syntactically wrong with the “attach”, so the problem does indeed lie with the “…”

*Sometimes I think I’m too damned subtle for my own (and others’) good.

Well I guess I need the hint spelled out for me. I'm not a programmer. Actually this is my first program that has actually done anything real or useful.

Sorry for my lack of knowledge here.

ok… lol. Sorry.

the … you’re joking about being my program. I’m reading too far into things. :slight_smile:

I'm reading too far into things.

Or too little.

Where's your code?

Got it figured out. Turns out it was a semicolon missing as best as I can tell. It now works.

Here is the code:

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

void loop()  // Program begins here.  
{
while (buttonState == HIGH)   
{     
   
    
  // read the state of the Relay value:
  buttonState = digitalRead(buttonPin);
  myservo.attach(9);

  // read the state of the Relay value:
  buttonState = digitalRead(buttonPin);

  // if relay is NOT high exit program
if (buttonState != HIGH) 
{ 
break;
}

//Main Program 
for (q = 0; q <=3; q += 1) 
{
  for(pos = slowleft; pos >= slowright; pos -= 1)  
  {                                  
    myservo.write(pos);             
    delay(23);                       
  } 
  
  for(pos = slowright; pos < slowleft; pos+=1)  
  {                                
    myservo.write(pos);          
    delay(23);                      
  }
}

exit:

  {    
     pos = 91;    
    myservo.write(pos);          
    delay(23);    
    myservo.detach();
   }

}
}

}

Here is the code:

Are you somehow allergic to posting the whole code?

buttonState = digitalRead(buttonPin);
  myservo.attach(9);

  // read the state of the Relay value:
  buttonState = digitalRead(buttonPin);

I can't imagine the state of "buttonPin" has changed much in that short time.