Arduino and servo control
Moderators: adafruit_support_bill, adafruit

Arduino and servo control

by slurry bowl on Tue Feb 18, 2014 11:42 pm

Hello Adafruit Arduino community.

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 :D :D 8)
Code: Select all | TOGGLE FULL SIZE
#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);
}
slurry bowl
 
Posts: 122
Joined: Sun Nov 11, 2012 6:37 pm

Re: Arduino and servo control

by franklin97355 on Wed Feb 19, 2014 1:26 am

Not reading all your code but
Code: Select all | TOGGLE FULL SIZE
 outputValue = j;
shouldn't this be j = outputValue; ?
User avatar
franklin97355
 
Posts: 5434
Joined: Mon Apr 21, 2008 2:33 pm

Re: Arduino and servo control

by slurry bowl on Thu Feb 20, 2014 1:18 am

yup so simple !

thanks.

Is there a way to have the code check the pot. reading at the end of each 180 turn?

Now it only checks at the end of a full back/forth..

:o
slurry bowl
 
Posts: 122
Joined: Sun Nov 11, 2012 6:37 pm

Re: Arduino and servo control

by adafruit_support_bill on Thu Feb 20, 2014 7:54 am

Sure. Just take the code that you have before the first "for' loop, and replicate it before the second 'for' loop:

Code: Select all | TOGGLE FULL SIZE
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
  }

  sensorValue = analogRead(analogInPin);
  outputValue = map(sensorValue, 1023, 0, 1, 10);
  outputValue = j;
  Serial.print("sensorValue = " ); 
  Serial.println(sensorValue);
  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);
}
User avatar
adafruit_support_bill
 
Posts: 30760
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino and servo control

by slurry bowl on Sat Mar 15, 2014 6:58 pm

thanks for all the help.

I am currently running two servos off the same pin and everything is working great.

If I wanted the servos to rotate in opposite directions, how would I adjust the code to use a second output pin so that each servo would have its own pin.

here is the current code:

Code: Select all | TOGGLE FULL SIZE
#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);
  j = outputValue;
  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
  }

  sensorValue = analogRead(analogInPin);
  outputValue = map(sensorValue, 1023, 0, 1, 10);
  outputValue = j;
  Serial.print("sensorValue = " ); 
  Serial.println(sensorValue);
  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);
}
slurry bowl
 
Posts: 122
Joined: Sun Nov 11, 2012 6:37 pm

Re: Arduino and servo control

by slurry bowl on Sat Mar 15, 2014 7:25 pm

How would I run two servos using this code that rotate in the opposite direction? On separate pins. Ive tried a few different ideas and nothing seems to work. the servos just freeze.

Here is current working code:

Code: Select all | TOGGLE FULL SIZE
#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);
  j = outputValue;
  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
  }

  sensorValue = analogRead(analogInPin);
  outputValue = map(sensorValue, 1023, 0, 1, 10);
  outputValue = j;
  Serial.print("sensorValue = " ); 
  Serial.println(sensorValue);
  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);
}
slurry bowl
 
Posts: 122
Joined: Sun Nov 11, 2012 6:37 pm

Re: Arduino and servo control

by adafruit_support_bill on Sun Mar 16, 2014 7:06 am

It is easier if you use myservo.write() instead of myservo.writemicroseconds().

Using write() you specify directly in degrees. To have two servos moving in opposite directions you would do:

Code: Select all | TOGGLE FULL SIZE
servo1.write(angle);
servo2.write(180 - angle);
User avatar
adafruit_support_bill
 
Posts: 30760
Joined: Sat Feb 07, 2009 10:11 am