Control servo motor by using 2 push button.

Hi,

Im newbie in learning Arduino. Hopefully you can do me a favour.

I have some idea which servo motor with 2 push button. how i modify the coding below to make button 1 to start the tuning of servo and will stop a while in every angle based on the coding and the servo will fully stop at last. then, the servo will get back to original position (0 degree) after button 2 is pressed.

Can anyone help?

Thanks in advance

tRY1.ino (729 Bytes)

this is my coding.

#include <Servo.h>

Servo myservo; 


const int up = 2;     // the number of the pushbutton pin

// variables will change:
int pressup;         // variable for reading the pushbutton status

void setup() {
  
  myservo.attach(9);
  
  pinMode(up, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  pressup = digitalRead(up);
  
  if (pressup == HIGH) {     
    // Servo move   
    myservo.write(10); 
    delay(5000);
    myservo.write(13); 
    delay(5000);
    myservo.write(18); 
    delay(5000);
    myservo.write(26); 
    delay(5000);
    myservo.write(30); 
    delay(5000);
    myservo.write(36); 
    delay(5000);
    
      } 
 
  else {
    // Servo off
    myservo.write(0); 
   
    } 
}

Perhaps if you change the else part of your code

 else {
    // Servo off
    myservo.write(0); 
   
    }

to something like this

 if (homeRequested == HIGH) {
    // Servo off
    myservo.write(0); 
   
    }

and add the line

homeRequested = digitalRead(homeButtonPin);

after the line

pressup = digitalRead(up);

...R

Servo button code in which you might include the incremental moves.

//zoomkat servo button test 12-29-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;
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);
  }
}

zoomkat:
Servo button code in which you might include the incremental moves.

//zoomkat servo button test 12-29-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;
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);
  }
}

Zoomkat, i had try your coding and connect button pin to 4 and 5 respectively, and servo signal connect to 7. but i cnt get it, the servo keep rotate without control.