Control servo with SPDT


Sample code :

#include <Servo.h>
Servo myservo;
const int buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 4;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int pos = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
myservo.attach(9);
}

void loop(){
// read the state of the pushbutton value:
if (digitalRead(buttonPin1) == LOW) {
digitalWrite(ledPin, LOW); //turn LED off
{                                  // in steps of 1 degree
myservo.write(0);              // tell servo to go to position in variable ‘pos’
delay(15);
}
} else if (digitalRead(buttonPin2) == LOW) {
digitalWrite(ledPin, HIGH); //turn LED on
myservo.write(180);              // tell servo to go to position in variable ‘pos’
delay(15);
}
}

Ditunggu komennya ...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s