Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The pot at the speed pot pin determines the overall speed (forwards and backwards might be asymmetric! The pot at the direction pot pin determines the direction. * all the way down: full speed backward * down: backwards * middle: stop * up: forwards * all the way up: full speed forward
#include <Servo.h>
int servoPin = 9, speedPotPin = A0, directionPotPin = A1;
Servo servo;
void setup()
{
Serial.begin(9600);
pinMode(servoPin, OUTPUT);
pinMode(speedPotPin, INPUT);
pinMode(directionPotPin, INPUT);
servo.attach(servoPin);
}
void loop()
{
int dir = analogRead(directionPotPin);
int speedVal = analogRead(speedPotPin);
if(dir < 300) //BACKWARDS
{
servo.attach(servoPin);
int servoSetting = 0;
if(dir < 100)
{
servoSetting = 0;
}
else
servoSetting = map(speedVal, 0, 1024, 90, 0);
Serial.println(servoSetting);
servo.write(servoSetting);
}
else if( dir > 700) //FORWARDS
{
servo.attach(servoPin);
int servoSetting = 0;
if(dir > 900)
{
servoSetting = 180;
}
else
servoSetting = map(speedVal, 0, 1024, 90, 180);
Serial.println(servoSetting);
servo.write(servoSetting);
}
else //STOP
{
servo.detach();
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.