Move Servo with 1 button

Hey guys.
After a lot of searching, I can not find an example of my specific issue with any clarity.

I'm simply wanting to move a servo from position A to B with a push of 1 button. Then from B back to A with push of same button.

Also, do I still need a 10K resistor for button to ground? I guess a schematic would be helpful.

Thanks in advance for any assistance.

In hind sight, my servos can be programmed without the Arduino library.
Would this be easier? Just set the push button to activate the servo both ways?

Here is a schematic for the most common way to wire a pushbutton switch.

A link to the servo would be helpful. Hobby servos that I am familiar with need the 1 to 2 mS pulse delivered at 50 Hz. This can be done without a library, but Servo library makes it much easier.

To make the sketch work like you want, you need to detect when the button is pushed (transition from HIGH to LOW or LOW to HIGH) not the level, LOW or HIGH. The state change detection example in the digital section of example code in the IDE shows how to detect the transition. The example includes a button push count variable so on the first transition send one value to the servo and on the second transition send a different value to the servo.

Note that in the state change detection example the switch is wired with a pull down. If the switch is wired like the schematic, reverse the logic.

Tell us what you thought about this. If there is some code,Please post id in the code tags </> .

-Malhar

Thats the particular servo. I used a servo programmer to set the rotation limit etc.

This is the code I'm using.
#include <Servo.h>
const int switchPin = 8; //active low
const int servoPin = 9;
Servo servo;

void setup() {
pinMode(switchPin, INPUT_PULLUP);
servo.attach(servoPin);
}

void loop() {
if (digitalRead(switchPin))
servo.write(50); // HIGH (switch is off)
else
servo.write(90); // LOW (switch is on)

delay(500); //delay for debounce
}

kinda works. But the servo is chattering. I may have to reprogram it, I think it's trying to zero beyond its capabilities.

Issue now is, I want it to move to point A and stop when i push button.
THen return to point B when button is pressed again.

Having a little better luck with this...

#include <Servo.h>

Servo myservo; // creating myservo object
int buttonPin = 8; // set button pin
int buttonState = 0; // set buttonState

void setup()
{
myservo.attach(9); // attach the 9 pin to servo
pinMode(buttonPin, INPUT); // set button to input
}

void loop()
{ buttonState = digitalRead(buttonPin); // read and save to the variable "buttonState" the actual state of button if (buttonState == HIGH)
if (buttonState == HIGH)
{
myservo.write(0);
}
else
{
myservo.write(90);
delay(500);
}
}

But still a lot of Jitter. I noticed some code it wont jitter as much. Plus with this code when I release the button it returns back to original spot.

Oh and incase this may be pertinent. What I'm trying to accomplish is a servo opening a small door with the push of a button then close it when I push it again.

one button servo

groundfungus:
one button servo

Thanks groundfungus. I have seen 2 different methods for wiring this. resistor to ground and power to ground.

Which method would be best for this code. Should I use the code for internal pullup resistor or not?

The internal pull up is free and is there just for this type of situation. There is the added benefit that you can use the hardware debounce.

This is the servo I'm using.
https://www.servocity.com/html/hs-5485hb_servo.html#.VwyOSlQrLIV

I enclosed the wiring diagram I'm using.
Using this code:
#include <Servo.h>
int button = 8; //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(9); //pin for servo control signal
digitalWrite(8, 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
}

It freaks out. just sweeps.

Yes it will. You need to do two things

  1. put your code in code tags, that is the </> icon.
  2. only do stuff, that is change your servo when the current state of the button is zero AND the last state you read was not zero.

Try this:

#include <Servo.h>
Servo myservo; // creating myservo object
#define buttonPin  8   // set button pin
byte buttonState=HIGH; // set buttonState
byte lastbuttonstate;
int myAngle, otherAngle;
void setup()
{ pinMode(9, OUTPUT);
  myservo.attach(9); // attach the 9 pin to servo
  pinMode(buttonPin, INPUT);// set button to input
}


void loop()
{ 
  buttonState = digitalRead(buttonPin); // read and save to the variable "buttonState" the actual state of button if (buttonState == HIGH)
  if (buttonState == LOW && lastbuttonstate == HIGH)
  {
    myservo.write(myAngle);
    lastbuttonstate = buttonState;
    delay(20);
  }
  else
  {
    myservo.write(otherAngle);
    delay(20);
  }
}

Try this code. It uses the example that I suggested in reply #1.

#include <Servo.h>

const int  buttonPin = 8;
const int ledPin = 13;
const byte servoPin = 9;

Servo servo;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  servo.write(90);
  servo.attach(servoPin);
  Serial.begin(9600);
}

void loop()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == LOW)
    {
      // if the current state is LOW then the button
      // went from off to on:
      buttonPushCounter++;      
      if(buttonPushCounter == 1)
      {
        servo.write(20);
      }
      if(buttonPushCounter == 2)
      {
        servo.write(140);
        buttonPushCounter = 0;
      }
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
}

MalharD, thanks man but yours didn't work. Probably my wiring not matching.
@Groundfungus You'rs works, I believe. I need to fix the dang jittering. I believe your code is working but the servo is randomly moving all over the place. I need to dig up a 0.1 capacitor for the hardware fix.

I'll let you know how it turns out. I really appreciate everyone's help. I know it must be frustrating dealing with noobs. I'll get better, I promise :wink:

I actually wired up the circuit and tested it before posting that code. I used internal pullup and hardware debounce. My servo does not jitter at all. How is the servo powered?

DeviceUnknown:
MalharD, thanks man but yours didn't work. Probably my wiring not matching.
@Groundfungus You'rs works, I believe. I need to fix the dang jittering. I believe your code is working but the servo is randomly moving all over the place. I need to dig up a 0.1 capacitor for the hardware fix.

I'll let you know how it turns out. I really appreciate everyone's help. I know it must be frustrating dealing with noobs. I'll get better, I promise :wink:

Sorry, maybe one or two mistakes,I was too overconfident.

-Malhar

groundfungus:
I actually wired up the circuit and tested it before posting that code. I used internal pullup and hardware debounce. My servo does not jitter at all. How is the servo powered?

I have the servo on a breadboard powered by a 5v 2A power supply. I also ran ground from breadboard to ground on arduino. Arduino is powered by my USB port.

I'm still pretty confused on the pin 7 thing. When i pull it out it does all kinds of weird things lol

I would like to remove that part of the code all together. No need for my application.It's either on, or off (the power) and when on it needs to run.

Are all of the grounds connected together? Servo power supply -, servo black wire, Arduino ground? Is your breadboard one with the power rails split in the middle as some are? Have you tried a 0.1 to 1 uf cap across the servo power? That's all I got, right now.

What pin 7 thing? I don't see pin 7 in any of these posts.

groundfungus:
What pin 7 thing? I don't see pin 7 in any of these posts.

He has two threads running. Pin 7 is used in the other thread/circuit.