#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define servoPin 3 // ~
#define pushButtonPin 2
int angle = 0; // initial angle for servo (between 0 and 180)
int angleStep = 10;
const int minAngle = 0;
const int maxAngle = 190;
const int type = 2; // watch video for details. Link is at the top of this code (robojax)
int buttonPushed = 0;
void setup() {
Serial.begin(9600); // setup serial
myservo.attach(servoPin); // attaches the servo on pin 3 to the servo object
pinMode(pushButtonPin, INPUT_PULLUP);
Serial.println("Rotation Starting");
myservo.write(angle); // initial position
}
void loop() {
if (digitalRead(pushButtonPin) == LOW) {
buttonPushed = 1;
}
if (buttonPushed) {
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the movement at the ends of the angle:
if (angle >= maxAngle) {
delay(1500);
angleStep = -angleStep;
if (type == 1) {
buttonPushed = 0;
}
}
if (angle <= minAngle) {
angleStep = -angleStep;
if (type == 2) {
buttonPushed = 0;
}
}
myservo.write(angle); // move the servo to the desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
delay(30); // waits for the servo to get there
}
}