// ----------------------------------------------------------- // Servo w/ switch // // Setup: - Wire a switch between digital pin 5 and ground // - Connect a servo to +, gnd and digital pin 6 // ----------------------------------------------------------- int pos = 0; // variable to store the servo position boolean swState; // variable for "state" of switch int swPin = 5; // variable for switch pin number int servoPin = 6; // variable for servo pin number #include <Servo.h> Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created void setup() { pinMode(servoPin, OUTPUT); // Sets digital pin 6 for output pinMode(swPin, INPUT_PULLUP); // Sets digital pin 5 for input myservo.attach(servoPin); // attaches servo on pin 6 to servo object } void loop() { swState = digitalRead(swPin); // "read" the state of the switch pin if(swState == LOW){ myservo.write(175); // servo position can be between 0 and 180 } else { myservo.write(5); // servo position can be between 0 and 180 } } |