Help with programming Servos!

I only know the basics from programming on C, so, i need some help. It's simple, when i press a button, servo goes to 180º, when i press the SAME button again, it goes back to 0º. I have no idea of how to do that, please help!

thanks :)

First thing you should do is look at the examples that come with the Arduuino IDE. Some deal with servos and others with buttons.

If you know a little C (or even if you don't) they should enable you to make a reasonable attempt at your project.

...R

The below code might work for you.

[/
//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}
code]

I only know the basics from programming on C, so, i need some help. It's simple, when i press a button, servo goes to 180º, when i press the SAME button again, it goes back to 0º. I have no idea of how to do that, please help!

thanks :)

It almost worked, servo moves form 0 to 180 constantly, when i press the button it stops

FORGET, i modyfied a bit your program, and now it works, thanks :)

I want to make a program like this: when i hold a button, a servo goes to the right AS i hold the button.And when i press another button, servo goes to the left AS i hold the button

Go ahead. I hate to break it to you, but even those bad at programming could do this.

Look at the button and servo examples.

First get the LED to light when you push the button. Take the rest step by step.

So you need a variable to hold a value between 0 and 180, and you need two buttons to increment and decrement that variable. That's simple, but were not going to write the code for you.

Smug: I want to make a program like this: when i hold a button, a servo goes to the right AS i hold the button.And when i press another button, servo goes to the left AS i hold the button

And what should it do when you hold both buttons, and when you hold neither button? Two buttons have four possible digiatal combinations and your sketch must account for all four states.

@smug, Tell me what you understand by the term "cross-posting"

Go ahead. I hate to break it to you, but even those bad at programming could do this.

Somebody having a bad day? :roll_eyes:

I want to make a program like this: when i hold a button, a servo goes to the right AS i hold the button. And when i press another button, servo goes to the left AS i hold the button

The below test code might be close to what you are looking for.

//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;
int pos = 90; // variable to store and set the servo position 

void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  servo1.write(pos); //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)
  {
    pos=(pos+1);
    if(pos>180) pos=180; //limit upper value
    Serial.println(pos); //for serial monitor debug
    servo1.write(pos); // tell servo to go to position in variable 'pos' 
    delay(150); // waits 150ms to slow servo movement 
  }    

  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    pos=(pos-1);
    if(pos<0) pos=0; //limit lower value
    Serial.println(pos); //for serial monitor debug
    servo1.write(pos); // tell servo to go to position in variable 'pos' 
    delay(150); // waits 150ms to slow servo movement
  }
}

zoomkat:

Go ahead. I hate to break it to you, but even those bad at programming could do this.

Somebody having a bad day? :roll_eyes:

No, but he asked for someone good at programming. I was just pointing out that this is an elementary program, that even those with little experience could create.

Smug: I only know the basics from programming on C, so, i need some help. It's simple, when i press a button, servo goes to 180º, when i press the SAME button again, it goes back to 0º. I have no idea of how to do that, please help!

thanks :)

If you want to learn how to solve a problem like that yourself, you need to elaborate every piece of the puzzle; even if it is simple. It is a good exercise, no matter the level of your skill.

For example, you need to consider how the program interacts with the components. What components are to be considered the "input" of your program? And what components are to be considered the "output" of your program? In this case, it is clear that the button is the input, so you need to determine the current state of the button [either pressed or not pressed], by reading its signal and interpreting its meaning. In the case of a button, when you connect a button to a pin and read the pin, the signal will be LOW when the button is pressed and HIGH when it is not pressed.

Then you process the input and finally produce some output. In your situation, a servo is the output of your program and it can be in two different states: either at 0 degrees or at 180 degrees [according to your specifications]. You want it to change its state to the other state once the button is pressed.

Here is the difficult part -- sort of. What does it mean for the button to be pressed? Or to be pressed "again"? Basically, you only know that the button is being pressed or is not being pressed. When I would check the button's state at two different points in time, I cannot know for sure whether the button's state changed in the meantime. So I cannot know - at least not immediately - whether the button is pressed "again", since I cannot know whether the button has been released in the meantime. That is, I cannot know without checking the button's state all the time and remembering that the button was pressed previously. When I introduce a variable for remembering that the button was pressed, I can see whether the being pressed of the button is a button just now being pressed or a button still being pressed. Once the button is no longer pressed, you can reset the variable, so that when the button is pressed "again", you will know.

Something like that. I wrote this rather quickly, but you get the idea.