Created
April 23, 2019 19:54
-
-
Save giantmolecules/6db3efa483c6c37488cabee6ad009f40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
#define POT_PIN 0 | |
Servo myServo; | |
int val = 0; | |
int servoSetPoint = 90; | |
int servoIncrement = 0; | |
int servoPosition = 90; | |
int servoWidth = 10; | |
bool direction = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
myServo.attach(9); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
val = analogRead(POT_PIN); | |
servoSetPoint = map(val, 0, 1023, 0, 180); | |
updateServo(); | |
delay(10); | |
} | |
void updateServo(){ | |
if(direction == 1){ | |
servoIncrement++; | |
} | |
if(direction == 0){ | |
servoIncrement--; | |
} | |
servoPosition = servoSetPoint + servoIncrement; | |
if(servoPosition >= servoSetPoint + servoWidth){ | |
direction = 0; | |
} | |
if(servoPosition <= servoSetPoint - servoWidth){ | |
direction = 1; | |
} | |
if(servoPosition > 180){ | |
servoPosition = 180; | |
} | |
if(servoPosition < 0){ | |
servoPosition = 0; | |
} | |
myServo.write(servoPosition); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment