Arduino and servo control

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
slurry bowl
 
Posts: 156
Joined: Sun Nov 11, 2012 6:37 pm

Arduino and servo control

Post by slurry bowl »

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

#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); 
}

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: Arduino and servo control

Post by Franklin97355 »

Not reading all your code but

Code: Select all

 outputValue = j;
shouldn't this be j = outputValue; ?

User avatar
slurry bowl
 
Posts: 156
Joined: Sun Nov 11, 2012 6:37 pm

Re: Arduino and servo control

Post by slurry bowl »

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

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino and servo control

Post by adafruit_support_bill »

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

Code: Select all

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
slurry bowl
 
Posts: 156
Joined: Sun Nov 11, 2012 6:37 pm

Re: Arduino and servo control

Post by slurry bowl »

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

#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); 
}

User avatar
slurry bowl
 
Posts: 156
Joined: Sun Nov 11, 2012 6:37 pm

Re: Arduino and servo control

Post by slurry bowl »

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

#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); 
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino and servo control

Post by adafruit_support_bill »

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

servo1.write(angle);
servo2.write(180 - angle);

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”