Using push button to control servo/motor

Hi,

This is my first post and I am just starting to learn arduino.

I’m playing around with some ideas for a project I am working on and am trying to figure out how to get a servo to run off a short loop once a button gets pressed.

Can anyone help?

Thanks in advance

Dan

Well there is the ‘knob’ example, which uses a potentiometer http://arduino.cc/en/Tutorial/Knob. The potentiometer returns a value from 0 to 1023, depending on the position of the shaft or slide, and then the code tells the servo to go from 0 to 127 (via map) based on the value of the potentiometer. So you twist the knob, and the servo goes forward or backward.

Here is some code I wrote some time ago that is attempting to fire a camera using a servo to push down the shutter. I’ve been busy doing other things, and haven’t gotten back to playing with this. So it is more just an example of how to use a button and servo.

// Adapted from the Sweep and Button example code
// Adaption by Michael Meissner, arduino@the-meissners.org

#include <Servo.h> 
 
// Servo's black wire is connected to ground, red wire to 5 volts
// and white wire to button 9

Servo shutter;                                  // servo to fire shutter release
const int pin_shutter	= 9;		// pin servo is on
const int pos_reset		= 0;		// position to fire
const int pos_fire		= 90;	// position to fire

// Push button has ground connected to 10k resistor, and then button 12 and 5 volts
const int pin_button		= 12;	// pin button is on
int state;						// current button state

// Standard on-board led
const int pin_led		= 13;	// pin LED is on

void setup()
{ 
  // set up the button
  pinMode (pin_button, INPUT);
  state = digitalRead (pin_button);

  // set up the LED, turning it off initially
  pinMode (pin_led, OUTPUT);
  digitalWrite (pin_led, LOW);

  // set up the servo, resetting it to the initial position
  shutter.attach (pin_shutter);
  shutter.write (pos_reset);
} 
 
 
void loop() 
{
  int new_state = digitalRead (pin_button);
  if (new_state != state)
    {
      state = new_state;

      // If the button is HIGH, turn on the LED and set the servo to fire.
      // Otherwise turn off the LED and reset the servo
      digitalWrite (pin_led, state);
      shutter.write ((state == HIGH) ? pos_fire : pos_reset);
    }
}

That's great MM, thanks so much for this.

I can now play around with this code and see what else I can make it do.

Thank you! :slight_smile:

Have fun! :smiley:

Some old servo code that might be of interest:

//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);
    delay(2000);
    servo1.write(20);
  }
}

Thanks for this!

Keep em coming!

:slight_smile: