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