Some questions and help on servos

Hello everyone!

I am trying to develop a system to control 2 servos in a very simple way.
I am using Mega 2560 and each time one of the analog inputs goes to GND, the servo moves.
Below is the code:

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
  
  /*else {
    servo1.write(90);
  }*/
}

What I need to do is to move the servo a little bit when the analog pin goes to GND and stops when this pin goes to HIGH level.

Any help will be appreciated!

What happens when you run the program?
What do you mean by "move the servo a little bit"?

Hello dear jremington.
When I run the program, the servo Works, but it turns till the end. And never stops in the middle.
And I would like to turn the servo axis in small steps. From 2 to 2 degrees for example, but I can´t do it.

Thank you.

Right now, the program is supposed to do the following:

  • when you push button 1, the program tells the servo arm to move to 170 degrees.

  • when you push button 2, the program tells the servo arm to move to 10 degrees.

  • when you release either button, nothing happens (the "else" is not functional, because it is turned into a comment by the characters "/" and "/").

From 2 to 2 degrees for example, but I can´t do it.

I don't understand the above sentence. Can you describe more clearly (like the way I did above) what you want the servo to do when you press and when you release a button?

What the the program is supposed to do is:
Push the button - the servo turns until I release it.
Push the other button - the servo turns in opposite direction.

And the other sentence is: I´d like the servo to move smoothly, not so fast!

Thanks!

Try this (untested) and modify as you like. Note that use of delay() is frowned upon by many on this forum!

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

int servo_position = 90;  //initialize to neutral position

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);

  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high

  servo1.attach(7);
  servo1.write(servo_position);  //send servo to neutral position

}

void loop()
{
  press1 = digitalRead(button1);

  if (press1 == LOW)
  {
    servo_position++; //add 1 degree
    if (servo_position > 170) servo_position = 170;  //safety limit

    servo1.write(servo_position);  //send servo command

    delay(100); //wait 1/10 second
  }   
 
  press2 = digitalRead(button2);

  if (press2 == LOW)
  {
    servo_position--; //subtract 1 degree
    if (servo_position < 10) servo_position = 10;  //safety limit

    servo1.write(servo_position); //send servo command

    delay(100);  //wait 1/10 second
  }

}

Some test code from a fire truck ladder discussion in the past.

//zoomkat servo button sweep test 12-23-2013
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 6; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
Servo servo2;
int extend = 5; // variable to store and set the servo position 
int lift = 5; // variable to store and set the servo position 

void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT); //out and down
  pinMode(button2, INPUT); //up and in
  servo1.attach(7); //extend servo
  servo2.attach(8); //lift  servo
  servo1.write(extend); //starting position
  servo2.write(lift); //starting position
  digitalWrite(6, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
  Serial.println("servo button sweep test 12-23-2013");
}
void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {  
    if (extend<175) 
      {  
          extend=(extend+1);
          if(extend>180) extend=180; //limit upper value
          Serial.println(extend); //for serial monitor debug
          servo1.write(extend); // tell servo to go to position in variable 'extend' 
          delay(100); // waits 100ms to slow servo movement   
      }        
    else
      {
      
          lift=(lift+1);
          if(lift>180) lift=180; //limit upper value
          Serial.println(lift); //for serial monitor debug
          servo2.write(lift); // tell servo to go to position in variable 'lift' 
          delay(100); // waits 100ms to slow servo movement   
      }
  }          
press2 = digitalRead(button2);
  if (press2 == LOW)
  {   
    if (lift>5) 
      {  
          lift=(lift-1);
          if(lift<0) lift=0; //limit upper value
          Serial.println(lift); //for serial monitor debug
          servo2.write(lift); // tell servo to go to position in variable 'lift' 
          delay(100); // waits 100ms to slow servo movement 
        
      }  
    else
      {
          extend=(extend-1);
          if(extend<0) extend=0; //limit upper value
          Serial.println(extend); //for serial monitor debug
          servo1.write(extend); // tell servo to go to position in variable 'extend' 
          delay(100); // waits 100ms to slow servo movement 
      }
   }     
}

Thank you, jremington and zoomkat.
The code provided by jremington worked fine!

Have a nice day.

claudiohi:
From 2 to 2 degrees for example

That doesn't make any sense!