Go Down

Topic: Servo definition problem (Read 485 times) previous topic - next topic

norooya

Hi, i am beginner for the aduino board and I want to control 3 servos and 6 switches(one servo for 2 switches).I can control one servo for two switches but couldn't figure out how to add more servos and switches in definition.

Please help us!

Here are the definition I used for one servo for 2 switches.
=========================================================================================================================
#include <Servo.h> // import servo control functions
Servo myservo; // create servo object
//create boolean variables to store the values of the 2 switches
boolean switchValue1;
boolean switchValue2;
void setup() {
pinMode(3, INPUT);
pinMode(7, INPUT);
myservo.attach(9);
}
void loop() {
switchValue1 = digitalRead(3);
switchValue2 = digitalRead(7);
//2 switches can simulate 4 conditions
//so we use the if()...else if() function to determine
//the output action according to the values of switches
//-------------------------------------------------------
// switch1 switch2 output
// LOW LOW action 1
// LOW HIGH action 2
// HIGH LOW action 3
// HIGH HIGH action 4
if(switchValue1==LOW && switchValue2==LOW) {
// action 1
myservo.write(90);
} else if(switchValue1==LOW && switchValue2==HIGH) {
// action 2
myservo.write(10);
} else if(switchValue1==HIGH && switchValue2==LOW) {
// action 3
myservo.write(170);
} else if(switchValue1==HIGH && switchValue2==HIGH) {
// action 4
myservo.write(90);
}
}

retrolefty

You just create more servo objects like:

#include <Servo.h> // import servo control functions
Servo myservo1; // create servo object #1
Servo myservo2; // create servo object #2
Servo myservo3; // create servo object #3


//create boolean variables to store the values of the 2 switches
boolean switchValue1;
boolean switchValue2;
void setup() {
pinMode(3, INPUT);
pinMode(7, INPUT);
myservo1.attach(9);
myservo2.attach(10);
myservo3.attach(11);

And so on. That make sense?

Lefty



Go Up