#include <Servo.h>
const int ledPin = 13;
const int buttonPin = 12;
const int servoPin = 11;
// Button vars
int buttonState = 0;
int toggle = 0;
// Servo vars
Servo myservo;
int servoVal = 0;
int direction = 0;
// Potentiometer vars
int potentVal = 0;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
myservo.attach(servoPin);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && toggle == 0) {
toggle = 1;
digitalWrite(ledPin, HIGH);
delay(200);
} else if (buttonState == HIGH && toggle == 1) {
toggle = 0;
digitalWrite(ledPin, LOW);
delay(200);
}
if (toggle >= 1) {
potentVal = analogRead(A0);
potentVal = map(potentVal, 5, 777, 0, 180);
servoVal = potentVal;
myservo.write(servoVal);
delay(1);
} else if (toggle <= 0) {
if (direction == 0) {
servoVal += 2;
delay(15);
} else {
servoVal -= 2;
delay(15);
}
if (servoVal >= 180) {
direction = 1;
} else if (servoVal <= 0) {
direction = 0;
}
myservo.write(servoVal);
delay(15);
}
}