Go Down

Topic: Push button servo controller sketch. Help, ideas, hand-holding Please? (Read 2 times) previous topic - next topic

ih8mud

I'm looking for a little help, guidance, ideas.

I found a sketch on a closed forum http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1246742084 here that looked to be just about exactly what I needed. It was originally written as a two button, one servo controller with a  few minor errors that were easily corrected. I can attach the original sketch if anyone wants to see it.

After correcting the errors and testing the sketch as I went, I started to add an additional servo and an additional separate button set to control that servo. Testing as I went along. It works great! EXCEPT! And here is where I need a little help:

If I use myservo1 going either forward or reverse by pushing one of its two attached buttons and then do the same for myservo2, that servo slews about 20°. The same thing happens if I to this in the reverse; the second servo slews or jumps 20° sometimes forward, sometimes backwards.

Here is the sketch:
(Thanks mem)

Code: [Select]
#include <Servo.h>

  Servo myservo1;          // define servo aliases
  Servo myservo2;
  #define leftPin 2        // define buttons attached to pins
  #define rightPin 3
  #define upPin 4
  #define downPin 5
  int pos = 90;          // set angle when inialitized
  int delayPeriod = 150;   // increasing this slows down the servo slew movement
                          // 1000 too much 300 jumpy 100 smooth slew 80 fast 50 too fast 70 testing
void setup()
{
  myservo1.attach(10);    // attaches the servo on pin 9 to the servo object
  myservo2.attach(9);     // attaches the servo on pin 10 to the servo object
  myservo1.write(pos);    // center the servo from inialitization
  myservo2.write(pos);    // center the servo from inialitization
  pinMode(leftPin, INPUT);   
  pinMode(rightPin, INPUT); 
  pinMode(upPin, INPUT);     
  pinMode(downPin, INPUT);   

  digitalWrite(leftPin, HIGH);    // turn on pullup resistor
  digitalWrite(rightPin, HIGH);   // turn on pullup resistor
  digitalWrite(upPin, HIGH);      // turn on pullup resistor
  digitalWrite(downPin, HIGH);    // turn on pullup resistor

   // left/right servo control
}
void loop()
{
  if(digitalRead(leftPin) == HIGH)        // left button instructions 
  {
   // in steps of 1 degree
   if( pos > 0)
--pos;
    myservo1.write(pos);   // tell servo to go to position in variable 'pos'
    delay(delayPeriod);    
  }
  if(digitalRead(rightPin) == HIGH)       // right button instructions 
  {
   if( pos < 180)
++pos;
    myservo1.write(pos);   // tell servo to go to position in variable 'pos'
    delay(delayPeriod);
  }

   // up/down servo control 
   {
   if(digitalRead(upPin) == HIGH)         // up button instructions 
  {
   // in steps of 1 degree
   if( pos > 0)
--pos;
    myservo2.write(pos);   // tell servo to go to position in variable 'pos'
    delay(delayPeriod);    
  }
  if(digitalRead(downPin) == HIGH)        // down button instructions 
  {
   if( pos < 180)
++pos;
    myservo2.write(pos);   // tell servo to go to position in variable 'pos'
    delay(delayPeriod);
  }
} }


Any ideas?
Am I missing something?
This is my first real sketch and I am a veteran of Arduino for a total of six days!

I have tried tons of things! namely changing the parameters of the pinMode, adding and deleting digitalWrite, Playing with the delayPeriod timing.
I have also tried to play with the pull-down resistors only to find out that if i use a 10k, 4.7k or even a 220?  the buttons do not respond. I'm currently using a 270? pull-down and the servos no longer chatter.

Again, any ideas? 
I'm using it to control the x-y axis of a security camera via a switched joy stick controller.

I would really like to solve this puzzle. But if anybody has a different sketch that accomplish this I would really appreciate taking a gander at it.

Thank you all in advance.


Peace.







mem

Hi ih8mud

Your sketch enables internal pull-ups in setup but the loop code is checking for a HIGH value when it should be checking for LOW.  Try removing the external resistors and changing the four button test lines from:
  if(digitalRead(___Pin) == HIGH)
to
  if(digitalRead(___Pin) == LOW)   

This code only handles one button press at a time, are you wanting to hold more than one button down to control multiple servos?

AgeingHippy

Hey dude

I am only learning servo control myself at the moment, so may be missing something...

BUT

You are using the same variable, pos, to hold the position of your up/down servo and your left/right servo, so moving one servo is going to affect the position of the other.

I would create 2 pos variables, say servo1Pos and servo2Pos and use each of these where relevant.

cheers

ih8mud

Thanks for the replies!

Quote
Try removing the external resistors and changing the four button test lines from:
  if(digitalRead(___Pin) == HIGH)
to
  if(digitalRead(___Pin) == LOW)   

I tried this and the servos still chatter without the resistors.

Quote
are you wanting to hold more than one button down to control multiple servos?


It would be ideal to control each servo at the same time. What would I need to do in order to make this sketch handle more than one servo?

Quote
I would create 2 pos variables, say servo1Pos and servo2Pos and use each of these where relevant.


Thanks for the idea. now I need to figure out how to make the two variables work. Any ideas?

Peace.

ih8mud

Hey All!

Ah! I think I fingered it out! It seems to work now without having the servos jump and I had to keep the external resistord (270?) in place otherwise the servos get the jitters.

Thanks AgeingHippy for putting me on the correct path!

Here's the sketch so far:

Code: [Select]
 
// test sketch a.2
#include <Servo.h>

  Servo servo1;          // define servo aliases
  Servo servo2;
  #define leftPin 2        // define buttons attached to pins
  #define rightPin 3
  #define upPin 4
  #define downPin 5
  int pos1 = 45;          // set angle when inialitized
  int pos2 = 90;
 
  int delayPeriod = 80;   // increasing this slows down the servo slew movement
                          // 1000 too much 300 jumpy 100 smooth slew 80 fast 50
                          // too fast 70 testing
                          // Project addition idea:
                          // add variable to control slew rate via potentiometer
                          // connected to analog input pin
                         
void setup()
{
  //test
  servo1.write(pos1);  // Put servo1 at home position
  servo2.write(pos2);  // Put servo1 at home position
   
  servo1.attach(10);    // attaches the servo on pin 9 to the servo object
  servo2.attach(9);     // attaches the servo on pin 10 to the servo object

  pinMode(leftPin, INPUT);   
  pinMode(rightPin, INPUT); 
  pinMode(upPin, INPUT);     
  pinMode(downPin, INPUT);   

//  digitalWrite(leftPin, HIGH);    // turn on pullup resistor
//  digitalWrite(rightPin, HIGH);   // turn on pullup resistor
//  digitalWrite(upPin, HIGH);      // turn on pullup resistor
//  digitalWrite(downPin, HIGH);    // turn on pullup resistor
//test
  if(digitalRead(leftPin) == LOW);    // turn on pulldown resistor
  if(digitalRead(rightPin) == LOW);   // turn on pulldown resistor
  if(digitalRead(upPin) == LOW);      // turn on pulldown resistor
  if(digitalRead(downPin) == LOW);    // turn on pulldown resistor

   // left/right servo control
}
void loop()
{
  if(digitalRead(leftPin) == HIGH)        // left button instructions 
  {
   // in steps of 1 degree
   if( pos1 > 0)
--pos1;
    servo1.write(pos1);   // tell servo to go to position in variable 'pos1'
    delay(delayPeriod);    
  }
  if(digitalRead(rightPin) == HIGH)       // right button instructions 
  {
   if( pos1 < 180)
++pos1;
    servo1.write(pos1);   // tell servo to go to position in variable 'pos1'
    delay(delayPeriod);
  }

   // up/down servo control 
   {
   if(digitalRead(upPin) == HIGH)         // up button instructions 
  {
   // in steps of 1 degree
   if( pos2 > 0)
--pos2;
    servo2.write(pos2);   // tell servo to go to position in variable 'pos2'
    delay(delayPeriod);    
  }
  if(digitalRead(downPin) == HIGH)        // down button instructions 
  {
   if( pos2 < 180)
++pos2;
    servo2.write(pos2);   // tell servo to go to position in variable 'pos2'
    delay(delayPeriod);
  }
} }


I know it's a bit unorganized and very verbose. Any suggestions on maybe cleaning it up a bit? Like I said, I'm very green at this.

Thanks all for helping me think this through.

I'm still very open to suggestions and comments on improving this sketch.

Peace



Go Up