Servos and push button

Hello.

I’m very new in this domain,but I’m eager to learn in time.
My first project has an Arduino Micro,two servos,one LED and one push button.
I want that when I push the button once the first servo motor to go in a position,after that the second servo motor to go to his position and when both of them reached their positions,the LED to turn on.When I press the button again,the whole process to reverse.

I want to do it myself,but for now it seems that I haven’t learned enough,and I need to do this project ASAP.

Could someone please help me with the basic code?

There are examples in the Arduino IDE that should cover most of what you need. Especially, look at the servo sweep example.

Planning and implementing a program may also be of interest.

...R

Basic servo button code.

//zoomkat servo button test 7-30-2011
//Powering a servo from the arduino usually *DOES NOT WORK*.

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

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

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

Thanks to both of you!
Yesterday I began the code.Hope to make it work.

Hello again.

I managed to get to the point in wich I everything works just fine,except that after the delay time everything goes back.
I want to push to momentary button once to move the servos in one position and stay there until I press again.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int pos = 0;
const byte led1 = 6;
Servo servo1;
Servo servo2;

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

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(30);
 
    delay(500);
    
    servo2.write(30);
 
    digitalWrite(6, LOW);
         
    delay(8000);
    
  }
  else {
     
    servo2.write(150);
    
    delay(500);
 
    servo1.write(150);
    
    delay(2000);
    
    digitalWrite(6, HIGH);

  }
}

As you have written your code the servo will stay in position for 8000msecs. After that it will do whatever the state of the button tells it.

If you want it to stay in place until the next button press you need to drop the ELSE part and change the code so that the angle of the servo changes when the button is pressed, but not when it is released.

Instead of servo.write(30) use servo.write(servoPos) and change the value of servoPos to 30 or 150 as required.

...R

In didn't really understand.Maybe is my english.Could you give me an example?

In didn't really understand.Maybe is my english

Your English seems pretty good. I wonder if you actually tried to follow the suggestions?

Something like this, perhaps? (Not complete, not tested)

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW and prevBtnState == HIGH) {
    if (servoPos == 30) {
       servoPos = 150;
    }
    else {
       servoPos = 30;
    }
  }
  prevBtnState = buttonState;
  servo.write(servoPos);
}

...R

Thanks,i'll test it.To be honest,I tryed it,but really didn't understand and perhaps that is why it didn't work.

P.S.I have readed your tutorial that you sugested in your first post,and got at chapter 5. Verry explicit!10+

buksy:
Thanks,i'll test it.To be honest,I tryed it,but really didn't understand and perhaps that is why it didn't work.

When you do that it is a good idea to post the code you have attempted. Then we can help with your learning rather than just give you code.

P.S.I have readed your tutorial that you sugested in your first post,and got at chapter 5. Verry explicit!10+

Thanks for your kind words

...R