I am attempting to change the speed that my servo sweeps. from slowly going 180 one way then one 180 the other to fast, then faster, but still ........relatively slow, the fastest rotation being 4 seconds to complete one 180 sweep.
I am using the microseconds servo code.
I want to control the speed with a potentiometer. I can not get it to work and would appreciate any help.
Here is my code, any help THANK YOU
Code: Select all
#include <Servo.h>
const int analogInPin = A0;
int sensorValue = 0;
int j;
int outputValue = 0;
Servo myservo;
int pos = 0;
void setup()
{
Serial.begin(9600);
myservo.attach(9);
myservo.writeMicroseconds(1600); // set servo to mid-point
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 1023, 0, 1, 10);
outputValue = j;
Serial.print("sensorValue = " );
Serial.println(sensorValue);
for(pos = 700; pos < 2300; pos += (j)) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.writeMicroseconds(pos); // tell servo to go to position in variable 'pos'
delay(20); // waits 15ms for the servo to reach the position
}
for(pos = 2300; pos>=700; pos-=(j)) // goes from 180 degrees to 0 degrees
{
myservo.writeMicroseconds(pos); // tell servo to go to position in variable 'pos'
delay(20);
}
delay(2);
}