Does my servo have a problem? or is it my code?

Hey guys this is a servo code I was given to work with from the help of this board.

Basically it’s purpose is to turn the servo 180 degrees when there is an input in pin 7, and back to 0 degrees when the input is zero. The servo is connected to pin 9.

The problem I am facing is that the it does not work all the time. Sometimes it works well, but sometimes it doesn’t do it at all, and there is almost always a HUGE delay after the input in pin 7 is switched between 1 or 0. Like it takes 20-30 seconds before it changes.

This is the code. Is it too complicated?

#include <Servo.h>

Servo myservo;

int pos = 0; // variable to store the servo position

void setup()
{
Serial.begin(9600); //start a serial port 9600 for debugging
pinMode(7, INPUT); //make pin 7 our input. This is port will determines power input or no power input (1 or 0)
}

void loop()
{

Serial.println(digitalRead(7)); //print the value from pin 7 to the serial monitor

if(digitalRead(7) == 1) //if the value of pin 7 is equal to 1 (power is applied)
{
myservo.attach(9); //attach the servo to pin 9

while(pos < 180 && digitalRead(7) == 1) // while’pos’ is less than 180 AND pin 9 still equals 1
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
pos += 1 ; //add 1 to ‘pos’
}

myservo.detach(); //stop the servo

}

How are you powering the servo? Does it have the full voltage it needs? It probably doesn't if you are powering it off the Arduino....

I see code to make the servo go to 180 degrees but I see no equivalent code to make it go to 0 degrees.

It would be more normal to put servo.attach() in setup() so it is only called once but I'm not sure why you are using servo.detach(). If you really need servo.detach then you obviously also need servo.attach() in loop(). Servo.detach is not usually needed.

...R

The code you've posted ought to work OK the very first time the button goes HIGH after a reset, but after that you're going to hit various bugs.

The most likely reason for the big delay you mention is that you have already pressed the button previously and the sketch is still trying to sweep the servo from the previous operation. The bugs in it will stop that doing anything useful most of the time, but it will still keep the sketch busy and stop it detecting further inputs for a long time.

Do you specifically want to move the servo [u]slowly[/u]?

Do you really need to detach the servo when it has reached the end position, and if so why?

Your code to return the servo to zero degrees is missing.

PeterH:
The code you’ve posted ought to work OK the very first time the button goes HIGH after a reset, but after that you’re going to hit various bugs.

The most likely reason for the big delay you mention is that you have already pressed the button previously and the sketch is still trying to sweep the servo from the previous operation. The bugs in it will stop that doing anything useful most of the time, but it will still keep the sketch busy and stop it detecting further inputs for a long time.

Do you specifically want to move the servo slowly?

Do you really need to detach the servo when it has reached the end position, and if so why?

Your code to return the servo to zero degrees is missing.

I want it to open and close quickly.

No I guess I don’t need it to detach.

and here is the full code lol sorry didn’t put the whole thing up

#include <Servo.h>

Servo myservo;

int pos = 0; // variable to store the servo position

void setup()
{
Serial.begin(9600); //start a serial port 9600 for debugging
pinMode(7, INPUT); //make pin 9 our input. This is port will determines power input or no power input (1 or 0)
}

void loop()
{

Serial.println(digitalRead(7)); //print the value from pin 7 to the serial monitor

if(digitalRead(7) == 1) //if the value of pin 7 is equal to 1 (power is applied)
{
myservo.attach(9); //attach the servo to pin 9

while(pos < 180 && digitalRead(7) == 1) // while’pos’ is less than 180 AND pin 9 still equals 1
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
pos += 1 ; //add 1 to ‘pos’
}

myservo.detach(); //stop the servo

}

else if(digitalRead(7) == 0) //else if pin 7 is equal to 0 (power is not applied)
{
myservo.attach(9); //re-attach the servo to pin 9

while(pos > 0 || pos < 5 && digitalRead(7) == 0) //while ‘pos’ is greater than zero OR less than 5 AND pin 7 still equals 0
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
pos -= 1; //subtract 1 from ‘pos’
}

myservo.detach(); //stop the servo
}
}

Put tags on that before AWOL sees it.

No one has yet asked you how the switch is wired. You aren't using the internal pullup resistor, so you must have an external pullup or pulldown resistor. Since you expect HIGH to mean pressed, that means a pulldown resistor.

So, how IS your switch wired?

  pinMode(7, INPUT);  //make pin 9 our input. This is port will determines power input or no power input (1 or 0)

No, it doesn't.

I want it to open and close quickly.

Some simple servo button code.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}