pushbuttons to control servo position

Hi everybody,

I'm a newbie in Arduino programming and I'm trying to do my first project.

I tried to control servo with potentiometer and RF 433Mhz, but I had problem with timer between Virtualwire and Servo Librairies... So I decided to start from scratch with pushbuttons.

So, I wrote this:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

const int leftpin = 2; // pin 2 used to connect the left pushbutton
const int rightpin = 3; // pin 3 used to connect the right pushbutton
const int centerpin = 4; // pin 4 used to connect the center pushbutton
int varl; // variable to read the value from the left pin
int varr; // variable to read the value from the right pin
int varc; // variable to read the value from the center pin

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(leftpin, INPUT); // initialize the left pushbutton
pinMode(rightpin, INPUT); // initialize the right pushbutton
pinMode(centerpin, INPUT); // initialize the center pushbutton
}

void loop()
{
varl = digitalRead(leftpin); // reads the value of the leftpin
varr = digitalRead(rightpin); // reads the value of the rightpin
varc = digitalRead(centerpin); // reads the value of the centerpin
if (varl == HIGH) {
myservo.write(180); // sets the servo left position
delay(2000);// waits for the servo to get there
}
else if (varr == HIGH) {
myservo.write(0); // sets the servo right position
delay(2000);// waits for the servo to get there
}
else if (varc == HIGH) {
myservo.write(90); // sets the servo center position
delay(1000);// waits for the servo to get there
}
else {
}
}

.

Now I'm trying to stop servo in position to have this
1- push left button => servo goes left slowly (2 second to reach position)
2- stop pushing button after 1 sec => servo stop in position (if it start from 90 deg. to reach 180 deg. in 2 seconds, it should stop around 135 deg. after 1 sec.)

But I'm stuck here. I can't find how to do this...

Do you have idea?

Thanks

But I'm stuck here. I can't find how to do this...

If the button is pressed move the servo a little, if not don't move the servo. Get rid of the delay()s and use millis() instead. Look at Several things at the same time for examples.

Or, keep the delay, but if the switch is pressed, move the servo one degree, delay() a little but (not two seconds), then end loop(). Next time around, repeat the process. All that is needed is a global variable to hold the position of the servo, incremented or decremented when the left and/or right switches are pressed.

Thank you both for these tips!

I'll probably try the two solutions and tell you who is the wisest of you ! :slight_smile:

thank you guys!

Using millis() is a good technique to use in general but small delay()s will work for your simple requirements.

Some servo code from another discussion that might be close.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

const int leftpin = 2;        // pin 2 used to connect the left pushbutton
const int rightpin = 3;       // pin 3 used to connect the right pushbutton
const int centerpin = 4;      // pin 4 used to connect the center pushbutton
int varl;                     // variable to read the value from the left pin
int varr;                     // variable to read the value from the right pin
int varc;                     // variable to read the value from the center pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(leftpin, INPUT);    // initialize the left pushbutton
  pinMode(rightpin, INPUT);   // initialize the right pushbutton
  pinMode(centerpin, INPUT);   // initialize the center pushbutton
}

void loop()
{
varl = digitalRead(leftpin);    // reads the value of the leftpin
varr = digitalRead(rightpin);    // reads the value of the rightpin
varc = digitalRead(centerpin);    // reads the value of the centerpin
if (varl == HIGH) { 
  myservo.write(180);                  // sets the servo left position
  delay(2000);// waits for the servo to get there
  }
  else if  (varr == HIGH) { 
  myservo.write(0);                  // sets the servo right position
  delay(2000);// waits for the servo to get there
  }
    else if  (varc == HIGH) { 
  myservo.write(90);                  // sets the servo center position
  delay(1000);// waits for the servo to get there
  }
  else {
    }
}