How to control a servo with two push buttons?

Hi,

I am trying to control a servo motor with two microswitches. I want to be able to move the servo in increments through 5 different positions, starting at 180 degrees (without looping back):

  • As I hit one switch (button2) the motor needs to move 45 degrees anticlockwise
  • As I hit the other switch (button1) the motor needs to move 45 degrees clockwise (if pressed at starting position then nothing happens)

I have looked at many tutorials and am struggling to get anywhere with my code. This is not a sweep function as the buttons allow you to move between 5 (180/45) positions incrementally depending on the button pressed, rather than sweeping between positions back and forth. I hope that makes sense.

If anyone feels they could help it would be greatly appreciated! I am more than happy to send out my code of what I have so far.

Thank you !!

duplicate at http://forum.arduino.cc/index.php?topic=389262.msg2682148#msg2682148

One post per topic please.

Apologies, I originally posted under the wrong topic.

Here is what I got so far:

#include <Servo.h>

const int buttonPin1 = 10;
const int buttonPin2 = 8;
int buttonState1 = 0; //current state of button
int buttonState2 = 0; //current state of button

int buttonPushCounter1 = 0; //counter for the number of button presses
int buttonPushCounter2 = 0; //counter for the number of button presses
int lastButtonState1 = 0; //previous state of button
int lastButtonState2 = 0; //previous state of button

Servo myservo;

int pos = 0;
int directionState = 0;

void setup() {

Serial.begin(9600);

myservo.attach(9);
myservo.write(180); //Starting position is 160 degrees

pinMode(buttonPin1, INPUT);
pinMode(buttonPin2,INPUT);
}

void loop() {

buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);

//something to set buttonPushcount2 to count up one, unless buttonPushCount1 is pressed in which case count down one

if (buttonState2 != lastButtonState2) {

if (buttonState2 == HIGH) {

//lastpos = pos;

directionState = 0; //direction of servo is anitclockwise
buttonPushCounter2++;
Serial.println("on");
Serial.print("position: ");
Serial.println(buttonPushCounter2);

myservo.read();
myservo.write(+35); // move anticlockwise by 35 degrees from last position

delay(60);

}
else {

Serial.println("off");
}
delay(50);
}

if (buttonState1 != lastButtonState1) {

if (buttonState1 == HIGH) {

//lastpos = pos;

directionState = 1; //direction of servo is clockwise
buttonPushCounter2--;
Serial.println("on");
Serial.print("position: ");
Serial.println(buttonPushCounter1);

myservo.read();
myservo.write(-35); // move clockwise by 35 degrees from last position

delay(60);

}
else {

Serial.println("off");
}
delay(50);
}

}

You have not said what happens when you run the program.

myservo.write(+35);  // move anticlockwise by 35 degrees from last positionThe comment does not describe what will happen. The parameter in servo.write() tells the servo to move to that position not how far to move from its current position. You need to update the target position and write that to the servo.

Servo.read() does not return where the servo is, servo.read() returns the last value sent to the servo with servo.write().