Pages: [1]   Go Down
Author Topic: Servos and Buttons  (Read 1708 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys, im new to this whole arduino thing smiley-lol, 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!!  smiley-lol


  
« Last Edit: November 13, 2012, 09:31:24 pm by ramiboi » Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged


0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
//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);
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot zoomkat! ill give it a try! smiley-lol

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 smiley-roll-sweat
« Last Edit: November 14, 2012, 01:28:12 am by ramiboi » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Keynsham, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 224
I like for loops and walks on the beach,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

and call it by putting:

Code:
...
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  smiley-lol
Logged

"C++ : Where friends have access to your private members." - Gavin Russell Baker

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-lol
Logged

Keynsham, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 224
I like for loops and walks on the beach,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You just need to write those actions into functions:

Code:
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.
Logged

"C++ : Where friends have access to your private members." - Gavin Russell Baker

Pages: [1]   Go Up
Jump to: