Servos and Buttons

Hey guys, im new to this whole arduino thing XD, i was just wondering if theres a way where an analog input button can
run a servo for just one loop, for example i want to run this just once when button 1 is pressed:

#include <Servo.h>

Servo myServo;

void setup() {
myServo.write(180); // Start at 180 degrees instead of 90
myServo.attach(9); // Attaches servo for turning
myServo.attach(10); // Attaches servo for grabbing
myServo.attach(11); // Attaches servo to raise arm

}

void loop() {
myServo.attach(9); // Attaches and apply power
myServo.write(90); // Position servo
delay(300); // Allow transit time
myServo.detach(); // Detach and remove power
delay(300);
}
.....................

and this when button two is pressed:

myServo.attach(11); // Attaches and apply power
myServo.write(90); // Position servo
delay(300); // Allow transit time
myServo.detach(); // Detach and remove power
delay(300);
}

is this even possible? any helpful thoughts would be much appreciated! Thanks!! XD

Hi,
Yes, its possible, but you have a few mistakes in the way you are using the servo.

You need to create more servos, you cannot attach the single servo myServo to three different output pins, you need three servos to do that.

Servos do not work the way you may be expecting, if you call detatch the servo will not try to hold its current position and may move under the weight of whatever it is your building so its better to keep them attached.

Other than that its easy enough.

Duane B
rcarduino.blogspot.com

Simple servo button code. Stick a wire in the board ground pin socket, and then touch the other end of the wire to pin 4 and pin 5.

//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 a lot zoomkat! ill give it a try! XD

Would I be able to apply this when I just want to use one button? For example, if I were to press Button 1, it will attach servo 9 turn it 90 degrees then detach it
then attach servo 11 turn it 90 degrees then detach it then stop? This seems really complicated for me :cold_sweat:

Why do you want to attach and detach the servos? When the switch is pressed (buttons are for shirts), move each servo as much or as little as you want. Do not detach them when done moving them.

I think what you are trying to achieve is having a button (switch) press make a certain set of servo actions happen. First start by setting up the servos by attaching them and setting the start positions [in setup()]. Then set the actions (say a switch press) to a set of actions using a function such as:

void servoAction1(){
   myServo1.write(90);
   myServo2.write(100);
}

and call it by putting:

...
void loop(){
   switch1State = digitalRead(switch1);
   if(switch1State == 1){
      servoAction1();
      delay(500);
   }
}

Look up switch polling to read inputs without 'blocking' other code. Also read up on how to write functions as they make programming in Arduino language/C++ much nicer.

Hope this helps XD

Hope this helps

Let's see. Was HIGH 1 or 0? Names are a heck of a lot easier to remember.

well, what I really want to happen is when I push button 1, servo 1 rotates to 90 deg. stays there for 200 milliseconds, then rotates to 50 deg. stays there for 200 milliseconds
then rotates to 180 deg. then finally stops there. Then when i press button 2, it does the same thing to servo 2 except the angles and timing are different. XD

You just need to write those actions into functions:

void servoAction1(){
   myServo1.write(90);
   delay(200);
   myServo1.write(50);
   delay(200);
   myServo1.write(180);
}

void servoAction2(){
   myServo1.write(angle1);
   delay(200);
   myServo1.write(angle2);
   delay(200);
   myServo1.write(angle3);
}

Read the states of the switches. Test them and then call the appropriate function.