Servo Motor Control

Hello Friends,

I have a project where I have some externally powered servos. The arduino is powered separately and I am using it to control the position of the motors, which I already understand to do. I'm trying to make a wired controller connected to the arduino so that when a click button is pressed, it would execute lines of code to control the servos. I'm not sure what I'm looking for code-wise or how the click button should be wired to the arduino.

Any suggestions?

I also, want to eventually make the controller wireless. Maybe just over RF or bluetooth. Any suggestions?

Thanks!

Start with the basic servo examples, knob and sweep, and maybe the button examples and go from there.

If you get stuck, come back and we'll help you.

Test code for two buttons controling a servo.

//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;

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

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

Thanks!

In this programming scheme, do I need to to run the power through the button?

tjdichoso:
Thanks! In this programming scheme, do I need to to run the power through the button?

No, power is supplied to the buttons from the pins by making the pins high.

I'm having trouble getting this to work still..

Here is my wiring setup:

Pin 7 to servo motor, motor powered separately

pin 5 to push button (Momentary Pushbutton Switch - 12mm Square - COM-09190 - SparkFun Electronics) to ground

Pin 4 to push button to ground

Is the problem the type of button I'm trying to use?

Ok, when I hold the button wired to pin 4, the motor rapidly wiggles back and forth. When I press and hold the other button, nothing happens at all… I’m trying to get it so that once, say button four is triggered, I can use the sweep function to have the servo move to various positions without having to hold the button down.

What code are you running? Have you got Serial.print statements in place so that you can confirm your button presses are being detected correctly?