Skip to content

Instantly share code, notes, and snippets.

@giantmolecules
Created April 23, 2019 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giantmolecules/6db3efa483c6c37488cabee6ad009f40 to your computer and use it in GitHub Desktop.
Save giantmolecules/6db3efa483c6c37488cabee6ad009f40 to your computer and use it in GitHub Desktop.
#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