#include <Servo.h> // add servo library
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
Servo myservo;
int val;
int pos = 0;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int i = 0; // variable for changing functions
void setup() {
// initialize the LED pin as an output:
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(8, OUTPUT);
myservo.attach(9);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
delay(1000);
i += 1;
}
if (i == 1) {
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(8, LOW);
val = analogRead(3);
val = map(val, 0, 1023, 0, 180);
myservo.write(val);
delay(15);
}
else if (i == 2) {
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(8, LOW);
// goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(90); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
else if (i == 3) {
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(8, HIGH);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
i = 0;
}
}