Controlling Servo Motor with 2 push buttons

Hi,

I wanted to know if someone could help us in our project, we need to control a servo motor with 2 push buttons and with an Arduino card but we need to move it from 3° to 5° with a movement of 1° for each press; one button move the servo motor toward 5 ° and the second move toward 3 °.
Actually we have this code :

#include<Servo.h>
int pos = 0;
Servo servo;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
servo.attach(9);
}
void loop() {

while (digitalRead(2) == HIGH && pos < 5) {
pos++;
servo.write(pos);
delay(15);
}
while (digitalRead(3) == HIGH && pos > 3 ) {
pos--;
servo.write(pos);
delay(15);
}
}

This program move the servo motor but not a °
Can someone help us ?

that is a fricking small range. Do note a lot of servo's have some hysteresis so don't expect exact results on that small range.

Some hints/qurestions:

  • Edit you post to include code tags! See How to use the forum. While you're there, read the rest as well.
  • Do you know why we like to use variables?
  • Do you have pull down resistors connected?
  • DON'T power the servo from the Arduino directly. Get a proper supply for it, servo's are hungry beasts
  • Use state change detection to see if the button became pressed (which is not the same as is pressed
  • If you want easy, grab a library like Bounce2.

but we need to move it from 3° to 5° with a movement of 1° for each press

Then look at the freaking state change detection example. It does NOT use while anywhere.

You are moving the servo when the switch IS pressed, not when it BECOMES pressed.

How ARE the switches wired? You are not using internal pullup resistors (clueless, probably), so you MUST have external resistors. How are THEY wired?