I have code to have two buttons control a single servo. I need to do this using 3 servos and 6 buttons! Please help! Can someone edit this and make it so i I can do this for 3 servos?!?! Please help!
#include <Servo.h>
Servo myservo;
#define leftPin 1
#define rightPin 2
int pos = 90;
int delayPeriod = 9; // increasing this slows down the servo movement
void setup()
{
myservo.attach(2); // attaches the servo on pin 9 to the servo object
myservo.write(pos); // center the servo
pinMode(leftPin, HIGH); // turn on pullup resistors
pinMode(rightPin, HIGH);
}
void loop()
{
if(digitalRead(leftPin) == LOW)
{
// in steps of 1 degree
if( pos > 0)
--pos;
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(delayPeriod);
}
if(digitalRead(rightPin) == LOW)
{
if( pos < 180)
++pos;
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(delayPeriod);
}
}
Im a little iffy about these, "pinMode(leftPin, HIGH); pinMode(rightPin, HIGH);" but other than that, I dont see the problem. You got one servo to work so, add the rest of the buttons and add more IF statements. Better yet, use arrays, they will shorten your code.
Did you actually write this, or copy it and some extra features?
You got one servo to work so, add the rest of the buttons and add more IF statements.
Can someone edit it so I can use 3 servos and 6 buttons. I don't know how to do that!
Well give it a bit of thought yourself, along these lines maybe.
Name your servos A, B and C, then copy / paste and edit the appropriate lines to refer to the servos A, B and C.
For instance...
Servo myservoA;
Servo myservoB;
Servo myservoC;
#define leftPinA 1
#define rightPinA 2
#define leftPinB 3
#define rightPinB 4
// etc etc to copy and edit all the lines that drive the original servo
This exact code works, I doubt it. According to this code, your servo and rightpin are on the same pin. Unless your using your analog pins, then you should clairify that they are in fact the analog pins with A1 and A2.
Were not going to just give you the code. You need to make an effort of trying it yourself first, post it, and then we will go through it and tell you what needs to be changed.
Why do all of my servos move at the same time when i press a button?!?
#include <Servo.h>
Servo myservo2;
Servo myservo5;
Servo myservo9;
#define leftPin 0
#define rightPin 1
#define lefttPin 4
#define righttPin 3
#define leftttPin 7
#define rightttPin 6
int pos = 90;
int delayPeriod = 9; // increasing this slows down the servo movement
void setup()
{
myservo2.attach(2); // attaches the servo on pin 2 to the servo object
myservo2.write(pos); // center the servo
pinMode(leftPin, HIGH); // turn on pullup resistors
pinMode(rightPin, HIGH);
myservo5.attach(5); // attaches the servo on pin 5 to the servo object
myservo5.write(pos); // center the servo
pinMode(lefttPin, HIGH); // turn on pullup resistors
pinMode(righttPin, HIGH);
myservo9.attach(9); // attaches the servo on pin 9 to the servo object
myservo9.write(pos); // center the servo
pinMode(leftttPin, HIGH); // turn on pullup resistors
pinMode(rightttPin, HIGH);
}
void loop()
{
if(digitalRead(leftPin) == LOW)
{
// in steps of 1 degree
if( pos > 0)
--pos;
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(delayPeriod);
}
if(digitalRead(rightPin) == LOW)
{
if( pos < 180)
++pos;
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(delayPeriod);
}if(digitalRead(lefttPin) == LOW)
{
// in steps of 1 degree
if( pos > 0)
--pos;
myservo5.write(pos); // tell servo to go to position in variable 'pos'
delay(delayPeriod);
}
if(digitalRead(righttPin) == LOW)
{
if( pos < 180)
++pos;
myservo5.write(pos); // tell servo to go to position in variable 'pos'
delay(delayPeriod);
}if(digitalRead(leftttPin) == LOW)
{
// in steps of 1 degree
if( pos > 0)
--pos;
myservo9.write(pos); // tell servo to go to position in variable 'pos'
delay(delayPeriod);
}
if(digitalRead(rightttPin) == LOW)
{
if( pos < 180)
++pos;
myservo9.write(pos); // tell servo to go to position in variable 'pos'
delay(delayPeriod);
}
}/code]
All your IF statements are using the same variable "pos". If you want to move each servo independently, they have to be different. pos1 for servo1, pos2, servo2 ...
You are still using pinMode() incorrectly. The pinMode() statement sets the MODE of the pin - INPUT or OUTPUT. It does NOT turn on or off the internal pullup resistor.
Use Serial.begin() and Serial.print() learn what your program is doing.
That means you need to get your switches off of pins 0 and 1.
Use suffixes, not extttttra letters in the middle of a name to distinguish names. A, B, and C are much easier to see that t, tt, and ttt.