Servo rotation help

Ok here is the code I am using, its by Zoomkat

    //zoomkat servo button test 12-29-2011

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

    void setup()
    {
      pinMode(button1, INPUT);
      pinMode(button2, INPUT);
      servo1.attach(9);
      digitalWrite(4, LOW); //to make pin low
      digitalWrite(5, LOW); // to make pin low
    }

    void loop()
    {
      press1 = digitalRead(button1);
      if (press1 == HIGH)
      {
        for (pos =0; pos <= 90; pos +=1)
        {
          servo1.write(pos);
          delay(20);
        }
      }   
     
      press2 = digitalRead(button2);
     
      if (press2 == HIGH)
      {
        for (pos =90; pos >= 0; pos-=1)
        {
          servo1.write(pos);
          delay(20);
        }
      }
    }

The servo rotation works, but I would like the sketch to perform like this:
Switch 1 is pushed and the servo rotates 90 degrees (12 o’clock to 3 o’clock) if s1 is pushed again nothing happens until servo is returned back to the 12 o’clock position by s2
Switch 2 is pushed and the servo rotates -90 degrees (3 o’clock to 12 o’clock) if s1 was pushed to rotate the servo to 3’oclock. If s2 is pushed again nothing happens until servo is moved to the 3 o’clock position by s1.
As the code is now, when the switch is pushed the first time it rotates like I want it to, but if you hit s1 again it jerks back to 12 o’clock and down to 3 o’clock, s2 behaves the same way but going in the opposite direction.
I have an arduino for Dummies book, and was wondering if a function would work. I have almost zero experience with Arduino, other than uploading and playing around with the examples in the IDE.

It sounds like you need 2 states. The servo is either at 12 o'clock waiting for S1 or at 3 o'clock waiting for S2
So, something like this pseudo code is needed

set servoPos = 12
start of loop
  if (servoPos = 12 and S1 becomes pressed)
  {
     move servo +90 degrees
     set servoPos = 3
  }
  else 
  if (servoPos = 3 and S2 becomes pressed)
  {
     move servo -90 degrees
     set servoPos = 12  
  }
end of loop

I never thought of that, great idea and is helpful. So I can have a Boolean statement inside the if, I didn't know that. I may have to try to write some code tomorrow night and see what happens. I'm sure whatever I come up with isn't going to work, so I will be asking more questions soon.

Write some code and if you have problems post it here for comment and help.

 //zoomkat servo button test 12-29-2011

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

    void setup()
    {
      pinMode(button1, INPUT);
      pinMode(button2, INPUT);
      servo1.attach(9);
      digitalWrite(4, LOW); //to make pin low
      digitalWrite(5, LOW); // to make pin low
    }

    void loop()
    {
      if (digitalRead(button1) == HIGH && pos == 0)
      {
        for (pos =0; pos <= 90; pos +=1)
        {
          servo1.write(pos);
          delay(20);
        }
      }   
      else
      if (digitalRead(button2)== HIGH && pos == 90)
      {
        for (pos =90; pos >= 0; pos-=1)
        {
          servo1.write(pos);
          delay(20);
        }
      }
    }

Ok, I put in the Boolean and symbol along with the other requirement for it and all the servo does now is rotate to 3 o’clock and sit there regardless of which switch I hit and how many times. I have a simple Arduino app on my phone so I looked up the AND function and wrote the new code just like the example had it. I also deleted the int press1 = 0 and the int press2 = 0 since I didn’t think that I would need it. Do I need a separate digitalRead for each switch placed before the if (…) lines? I think I know what I am trying to tell the Uno to do; if the position is 0 degrees and s1 is pressed, move 90 degrees, and the same for s2. With the &&, that should eliminate the extra movement of the servo since the position and button push won’t match if the servo position hasn’t been reset to 0 degrees. I’m sure its probably something simple, but I’ still trying to wrap my head around coding in general so bare with me and my many questions.

        for (pos =0; pos <= 90; pos +=1)
        {
          servo1.write(pos);
          delay(20);
        }

Print the value of pos after this for loop and you will see what the problem is.

Do a serial print on the IDE? I didn't think of that. I do need to see what's happening because something happened to my servo, the motor inside is locked up, I can't rotate it at all even by hand with the power removed. I've never used the serial print before so I will do a little reading up and maybe try again tonight.

Do a serial print on the IDE? I didn't think of that

It is just about the only debugging method available in the standard IDE unless you want to flash the LED on pin 13 to let you know that the program has got to a certain point in your program. Once you see the value of pos after the for loop you will see the problem immediately.

I'm sure it's something simple, but I'm just not seeing it. I will try it tonight. Would the error cause a servo to lock up?

Would the error cause a servo to lock up?

No
You may have driven it past its end point but not because of the mistake in your program. Servos are usually quite difficult to turn by hand (and are not really designed to be turned like that) because the motor drive is geared down to the output, so turning the output by hand is trying to work against the gearing up in the other direction. For the same reason it is also easy to strip teeth on the servo gears when turning it by hand.

Try the sweep example from the IDE on it and see what happens.

If the servo is mechanically jammed then a stripped tooth would be the most likely cause. I suggest you take the cover off and take a look at the gear train. If the servo has been damaged then anything else you do trying to drive it is a waste of time.

I'm going to check out the servo later today to see if the gears are worn. I may put power to it too to see what it does. Quick question about that like of code, is it something I should be seeing even without running the serial lines I the IDE?

It's not wear as such, but bits of tooth broken off that you need to be looking out for. When these gears strip they can tear the whole tooth off the gear, leaving it embedded in the meshing gear. This acts like a hard stop that jams the whole thing. In the past when this has happened to me, the symptoms have been that the servo would move in one direction until it hit the stop, and could then be moved in the reverse direction until that gear completed a full revolution and hit the stop again. Depending which gear it happened to I suppose it could leave you with a very small amount of travel, bu in my experience it tends to be the bigger gears which failed and this resulted in quite a big range of motion. So what you'd be looking for is one of those gears with a foreign object embedded between the teeth.

I found another servo I had laying around, so I'm not going to worry about the other one right now. I really want to get this code figured out. Quick question, am I doing the right thing by setting the uno input buttons to a digital low if I am actually using a wireless transmitter that will send momentary highs to those pins depending on which button I push on the transmitter fob? I am using a separate 5v power supply for the receiver and the servo.

The important thing is to keep the input pins in a known state at all times. This is usually done by wiring pull up or pull down resistors to the input pins or by setting the pinMode to INPUT_PULLUP which activates the internal pull up resistors.

This is usually done by wiring pull up or pull down resistors to the input pins or by setting the pinMode to INPUT_PULLUP which activates the internal pull up resistors.

Only external resistors are effective at keeping the pin in a known state across resets.

So I should wire in resistors to ground on those input pin, what size should they be 10k maybe?

what size should they be 10k maybe?

10K is good.

I should have stated this earlier but I'm using a wireless momentary receiver/transmitter for the switches. When I hit a button on the fob, the receiver outputs a momentary 5v signal on whichever pin is associated with that button and I have those outputs going to the Uno. So am I wrong in setting those pins to low since I will be sending a high signal to them?
Here is the receiver transmitter I am using:

If the pins are pulled HIGH when activated they need to be held LOW at other times. So, you need pull down resistors, and a common GND connection if separate power sources are used.