Poll
Question: How can I connect 3 different servos to 6 different switches?
how to put additional servo - 1 (100%)
how to put additional switches - 0 (0%)
Total Voters: 1

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

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);
}
}
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Pages: [1]   Go Up
Jump to: