Servo control: extremely confused noob

Hello everyone,
I am an absolute newbie, so please forgive me if
a. this has been asked before (see below)
b. it’s in the wrong sub-forum

I have been trying to get a mini servo working for some time. What I want to do is to get it to rotate 60 degrees on a button push and stay there. When the button is pushed again, I want it come back to zero.

I have tried searching and found similar (though not identical) problems. I have tried using the suggestions, but keep getting stuck at the same point.

What happens is that when I run my sketch and press the button, the servo rotates a few degrees (about 5, I guess) and comes back to zero. If I long press the push button, the servo rotates in 5 degree steps and then after 4 or 5 presses swings back to zero. I tested the servo using the sweep sketch in the examples and the servo rotates without a problem.

Since it is a mini servo, I am powering it off the Arduino board. Once I get the basic structure right and use multiple servos I will be using a separate power supply. I have put in a 10k resistor in the circuit. The rest is just the pushbutton and the servo.

Please take a look at my code and tell me what I am doing wrong. Also would a regular toggle switch work better for what I want to do?

Thank you for any guidance and pointers.

Ashok

#include <Servo.h>

int servoPin = 9; //servo on pin  9
int buttPin = 3; //push button on pin 3

Servo servo01; //plan to eventually have many servos

int pos = 0; //servo position
int buttonState = 1; //reading the button state

void setup()
{
  servo01.attach(9); //servo atached
  pinMode(buttPin, INPUT); //button initialised
  pinMode(servoPin, OUTPUT); 
}

void loop()
{
  buttonState = digitalRead(buttPin);
  if (buttonState == HIGH)
  {
    for(pos = 0; pos < 60; pos = pos+1)
    {
    servo01.write(pos);
    delay(10);
    }
  }
  else if (buttonState == LOW)
  {
    for(pos < 60; pos = 0; pos = pos-1)
    servo01.write(pos);
    delay(10);
  }
}

Please show how your button is wired.

   for(pos < 60; pos = 0; pos = pos-1)

This is wrong. It should be

for(pos = 60; pos >=0; pos = pos - 1)

…R

Basic servo toggle test code.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Since it is a mini servo, I am powering it off the Arduino board.

No, servos need their own power supply of 6 to 7.2V capable of at least 1A. Keep
all motor and servo power separate from digital electronics power, or expect
problems.

All my servos take +5Vdc. I have a Sainsmart UNO R3 with a bunch of dedicated servo connectors and they all measure 5v on the power pins.

Thank you everyone.

@groundfungus. As soon as I figure out how to create a schematic drawing, I'll post it. But I followed other people who had connected one pin to ground (through a 10k resistor) and to pin 3 on the Uno; the other is connected to +5V.
@Robin2, I'll make the change and test.
@MarkT and raschemmel, mine is a 5v servo, but I'll put it on a separate supply asap.
@zoomkat, thanks for uploading this sketch again. I had found it once before but hadn't copied it at that time. Now I have! I'll try and figure out how to stop the servo at one position. At the moment the servo keeps oscillating and stops momentarily when I press the button, but begins oscillating again immediately. A long press keeps it stopped. So should I add a command to set the pin to HIGH and direction to '0' so it stays still? I'll post the adaptation of your sketch when I get it working.

Thank you all once again.

A

You can put your 5V servos on a separate supply but I never do. I think the biggest reason to do that is just to eliminate noise spikes generated by the motors from glitching your computer circuits. I just use really big filter caps on my power bus (500uf or larger).

I set up a breadboard and using zoomcat’s code the servo goes to 20 degrees on one push of the button and swings to 160 on the next button push. Attached is how the button is wired (a very common way to wire switches using internal pullup omit R1, connect upper switch contact direct to V+). The important thing, when using normally open switches, is that the input is never left floating It must be connected either to V+ or ground. I almost always use the cap across the switch. 90% of the switches that I have tested require no further debounceing.

At the moment the servo keeps oscillating and stops momentarily when I press the button, but begins oscillating again immediately.

I suspect you are using the wire between the input pin and ground incorrectly. To act as a normally open push button, the wire connected to the input pin is only momentarily touched to ground (for less than 500 ms) to have the servo toggle. If the wire is left connected between the input pin and ground, the servo will continuously swing every 500 ms.

What happens is that when I run my sketch and press the button, the servo rotates a few degrees (about 5, I guess) and comes back to zero.

If I long press the push button, the servo rotates in 5 degree steps and then after 4 or 5 presses swings back to zero.

I tested the servo using the sweep sketch in the examples and the servo rotates without a problem.

Since it is a mini servo, I am powering it off the Arduino board.

Once I get the basic structure right and use multiple servos I will be using a separate power supply.

I have put in a 10k resistor in the circuit. The rest is just the pushbutton and the servo.

As soon as I figure out how to create a schematic drawing, I'll post it.

A schematic is something that should be posted with the initial post. Post-poning the schematic only leads to wasted time.
You should be able to hand draw the schematic and scan it to a file , either at home or at a fedex express office so you can post it.
If I were you I would have done that before the initial post. If it turns out that your push button circuit is not per the one posted by groundfungus, which is like a standard I might add, then you will have wasted a day for nothing.

Draw the schematic by hand and take a photo of it with a webcam or phone.

...R

Hey , great idea ! I forgot we can do that now . We don't need to scan drawings. I need to remember that.

Thank you for your advice and sorry I haven’t replied. Been down with the flu for two days.

I am uploading my schematic and I notice that is -um somewhat different :blush: - from what groundfungus posted. Will rewire it like that and report later today.

A

schematic01.jpg

Your switch will work that way. Your wiring fulfills the requirement that the pin is never left floating, It is LOW with the switch open and tied to V+ (HIGH) when pushed. The main reasons to wire the switch as I showed is to use the internal pull up and so save a resistor and also allow hardware debounce.

You seem to be powering the servo from the Arduino. That often (usually?) causes problems. If the servo draws more power than the Arduino can supply it will cause the Arduino to brown-out or reset which can look like software problems.

Its always a good idea to give the servo a separate power supply sharing a common ground with the Arduino.

…R

Thank you everyone.
I used zoomkat's sketch as well as following the circuit diagram that groundfungus had posted. It turned out I had been making another elementary mistake - I had connected the pin to ground so the pushbutton was in effect being bypassed. zoomkat's advice on the connecting the pin wrongly made the penny drop. :blush: :blush:
Now it is working fine, except that sometimes I need two/three presses of the button to work. This is probably because the only capacitor I have on hand is a 220 microfarad and that may be causing some delays. I'll get a smaller one and test tomorrow. And then run the servo off an independent supply, which I'll need when the servos multiply like gremlins.
Thank you all very much once again.

A

Yeah, I think using a 220uf in place of a 0.1uF might cause some differences.