#include <Servo.h>
Servo myservo;
#define servoPin 3 // servo signal wire pin A3
#define pushButtonPin 2 // switch wire pin A2
int angle = 1; // start angle
int angleStep = 89;
const int minAngle = 0;
const int maxAngle = 90;
const int type = 1;
int buttonPushed = 0;
void setup() {
Serial.begin(9600);
myservo.attach(servoPin);
pinMode(pushButtonPin, INPUT_PULLUP);
Serial.println("targets down");
myservo.write(angle);
}
void loop() {
if (digitalRead(pushButtonPin) == LOW) {
buttonPushed = 1;
delay(800);
}
if ( buttonPushed ) {
angle = angle + angleStep;
if (angle >= maxAngle) {
angleStep = -angleStep;
if (type == 1)
{
buttonPushed = 0;
}
}
if (angle <= minAngle) {
angleStep = -angleStep;
if (type == 2)
{
buttonPushed = 0;
}
}
myservo.write(angle);
Serial.print("Moved to: ");
Serial.print(angle);
Serial.println(" degree");
delay(200);
}
}