hi, this is my first post in the forum, and i was wondering how i could have 4 servos controoled by 4 button switches, each servo controlled by one switch. i have one servo working like this, but haw do i get the other 3 running?
here's the code:
#include <Servo.h>
// Set digital pin numbers:
const int servoPin = 8; // The number of the Servo pin
const int buttonPin = 2; // The number of the Pushbutton pin
int buttonState = 0; // Variable for reading the pushbutton status
int directionState = 0; // Variable for reading direction of the servo
Servo servo1; // Create servo object to control a servo
int pos = 0; // Variable to store the servo position
void setup() {
servo1.attach(9); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (directionState == 0){
//The button is pushed
if (buttonState == HIGH) {
directionState = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 90 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 50ms for the servo to reach the position
}
}
} else if (directionState == 1) {
// The button is pushed
if (buttonState == HIGH) {
directionState = 1; // The direction for the servo is anti-clockwise
// goes from 90 degrees to 0 degrees in steps of 1 degree
for(pos = 90; pos>=1; pos=pos-1)
{
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 50ms for the servo to reach the position
}
}
}
}
Assign pin numbers for the other three switches.
Assign pin numbers for the other three servos.
Create direction state variables for the other three servos.
Create servo instances for the other three servos.
Copy all the code in loop for reading one switch and moving one servo, and paste it three more times. In each copy, change the pin number and servo instance, and other stuff, as required, to work with the next switch/servo.
Test that each switch makes the appropriate servo move.
Notice that the next switch does nothing until the previous servo finishes moving.
If this is a problem, you'll need to re-think how to move the servos in small increments without using delay(). Look at the blink without delay example for inspiration.
#include <Servo.h>
// Set digital pin numbers:
const int servoPin1 = 8; // The number of the Servo pin
const int buttonPin1 = 2; // The number of the Pushbutton pin
const int servoPin2 = 9; // The number of the Servo pin
const int buttonPin2 = 3; // The number of the Pushbutton pin
const int servoPin3 = 10; // The number of the Servo pin
const int buttonPin3 = 4; // The number of the Pushbutton pin
const int servoPin4 = 11; // The number of the Servo pin
const int buttonPin4 = 5; // The number of the Pushbutton pin
int buttonState1 = 0; // Variable for reading the pushbutton status
int directionState1 = 0; // Variable for reading direction of the servo
int buttonState2 = 0; // Variable for reading the pushbutton status
int directionState2 = 0; // Variable for reading direction of the servo
int buttonState3 = 0; // Variable for reading the pushbutton status
int directionState3 = 0; // Variable for reading direction of the servo
int buttonState4 = 0; // Variable for reading the pushbutton status
int directionState4 = 0; // Variable for reading direction of the servo
Servo servo1; // Create servo object to control a servo
Servo servo2; // Create servo object to control a servo
Servo servo3; // Create servo object to control a servo
Servo servo4; // Create servo object to control a servo
int pos = 0; // Variable to store the servo position
void setup() {
servo1.attach(8); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input
servo2.attach(9); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin2, INPUT); // initialize the pushbutton pin as an input
servo3.attach(10); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin3, INPUT); // initialize the pushbutton pin as an input
servo4.attach(11); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin4, INPUT); // initialize the pushbutton pin as an input
}
void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
if (directionState1 == 0){
//The button is pushed
if (buttonState1 == HIGH) {
directionState1 = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
} else if (directionState1 == 1) {
// The button is pushed
if (buttonState1 == HIGH) {
directionState1 = 1; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 90; pos>=1; pos=pos-1)
{
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
}
buttonState2 = digitalRead(buttonPin2);
if (directionState2 == 0){
//The button is pushed
if (buttonState2 == HIGH) {
directionState2 = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
servo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
} else if (directionState2 == 1) {
// The button is pushed
if (buttonState2 == HIGH) {
directionState2 = 1; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 90; pos>=1; pos=pos-1)
{
servo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
}
buttonState3 = digitalRead(buttonPin3);
if (directionState3 == 0){
//The button is pushed
if (buttonState3 == HIGH) {
directionState3 = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
servo3.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
} else if (directionState3 == 1) {
// The button is pushed
if (buttonState3 == HIGH) {
directionState3 = 1; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 90; pos>=1; pos=pos-1)
{
servo3.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
}
buttonState4 = digitalRead(buttonPin4);
if (directionState4 == 0){
//The button is pushed
if (buttonState4 == HIGH) {
directionState4 = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
servo4.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
} else if (directionState4 == 1) {
// The button is pushed
if (buttonState4 == HIGH) {
directionState4 = 1; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 90; pos>=1; pos=pos-1)
{
servo4.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
}
}
that didnt work. once uploaded, the servos just "take turns" rotating 90 degrees and coming back. this is done in a loop. is there any way i can avoid this?
You are not activating the internal pullup resistors for the switches. That means that you have external resistors with the switches, right? How are the switches wired?
Add some Serial.print() statements, to print out the values of the switch states. If they are not what you think they should be, you need external or internal pullup or pulldown resistors. (Really, they are not optional).
hi, this is my first post in the forum, and i was wondering how i could have 4 servos controoled by 4 button switches, each servo controlled by one switch.
specifically, what do you want a servo to do when a button is pushed/not pushed?
specifically, what do you want a servo to do when a button is pushed/not pushed?
when a button is pushed, i want a corresponding servo to rotate 90 degrees. when no buttons are pushed, i just want them to stay stationery. right now, they're all working like this, except for one that just keeps going if no buttons are pushed.
If you write the code for one button and one servo, then some simple loops and a couple of arrays should ensure four work as well as one.
The key is writing the code for one with more than one in mind.
Think array.
This is my code. Show me where i should use the arrays and loops. i'm seriously an incredible n00b when it comes to this.
#include <Servo.h>
// Set digital pin numbers:
const int servoPina = 11; // The number of the Servo pin
const int servoPinb = 10;
const int servoPinc = 9;
const int servoPind = 8;
const int buttonPina = 2; // The number of the Pushbutton pin
const int buttonPinb = 3;
const int buttonPinc = 4;
const int buttonPind = 5;
int buttonState = 0; // Variable for reading the pushbutton status
int directionState = 0; // Variable for reading direction of the servo
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;// Create servo object to control a servo
int pos = 0; // Variable to store the servo position
void setup() {
servo1.attach(11);
servo2.attach(10);
servo3.attach(9);
servo4.attach(8); // attaches the servo on pin 8 to the servo object
pinMode(buttonPina, INPUT);
pinMode(buttonPinb, INPUT);
pinMode(buttonPinc, INPUT);
pinMode(buttonPind, INPUT); // initialize the pushbutton pin as an input
}
void loop(){
// read the state of the pushbutton value:
digitalRead(buttonPina);
digitalRead(buttonPinb);
digitalRead(buttonPinc);
digitalRead(buttonPind);
if (directionState == 0){
//The button is pushed
if (digitalRead(buttonPina) == HIGH) {
directionState = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
} else if (directionState == 1) {
// The button is pushed
if (digitalRead(buttonPina) == HIGH) {
directionState = 1; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 90; pos>=1; pos=pos-1)
{
servo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
}
if (directionState == 0){
//The button is pushed
if (digitalRead(buttonPinb) == HIGH) {
directionState = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
servo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
} else if (directionState == 1) {
// The button is pushed
if (digitalRead(buttonPinb) == HIGH) {
directionState = 1; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 90; pos>=1; pos=pos-1)
{
servo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
}
if (directionState == 0){
//The button is pushed
if (digitalRead(buttonPinc) == HIGH) {
directionState = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
servo3.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
} else if (directionState == 1) {
// The button is pushed
if (digitalRead(buttonPinc) == HIGH) {
directionState = 1; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 90; pos>=1; pos=pos-1)
{
servo3.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
}
if (directionState == 0){
//The button is pushed
if (digitalRead(buttonPind) == HIGH) {
directionState = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 90; pos=pos+1)
{
servo4.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
} else if (directionState == 1) {
// The button is pushed
if (digitalRead(buttonPind) == HIGH) {
directionState = 1; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 90; pos>=1; pos=pos-1)
{
servo4.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position
}
}
}
}
when a button is pushed, i want a corresponding servo to rotate 90 degrees. when no buttons are pushed, i just want them to stay stationery.
So, you want a servo to rotate 90 deg when a button is pushed, and when the button is released (not pushed), it stays stationary at 90 deg. Just a technical writing clarification.
sorry, i should have been more clear. i need them to rotate 90 degrees and go back to zero in one button push. if the buttons aren't pushed, then all servos stay stationery at 0 degrees.
Some simple code I tested just touching pin 4 wire to ground. Should be easy to add three more servos. You may need to debounce the button pushes with a sligh delay or similar.
//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);
}
else {
servo1.write(20);
}
}