problem in rotation of servo motor

hi all
i need to rotation of servo motor with two push button In two directions left and right ، i want when i push button(1) direction rotate in right and when i push button(2) direction rotate in left
But there is a problem ، Rotation occurs only once!!
where is my problem???

this is my code:

#include <Servo.h>

Servo myservo;  
int button1=30;
int button2=31;
int servo=37;
void setup() {
  myservo.attach(servo);  
  pinMode(button1,INPUT);
   pinMode(button2,INPUT);
}

void loop() {
  if (digitalRead(button1) == HIGH !=digitalRead(button2) ){
    digitalWrite(servo,HIGH);           
    delay(15);                    
    
   }

 else if (digitalRead(button2) == HIGH != digitalRead(button1) ){
    digitalWrite(servo,LOW);        
    delay(15);   
  }
}

(deleted)

Was not any errors

(deleted)

ashkan4818: Was not any errors

It is easy to write code that compiles but does not work. Have you looked at the Servo examples in the IDE as suggested ?

ashkan4818: Was not any errors

None that bothered the compiler. But you've certainly missed the mark on writing to the servos. See the examples and you'll see what you did wrong. digitalWrite is not the appropriate function to call. You need to call write on the servo object.

ashkan4818: Was not any errors

I can floor the gas pedal while my car's in neutral, and this won't break anything. It also won't make my car go forward either. What's your point?

Might break the water pump getting all that unloaded power.

CrossRoads: Might break the water pump getting all that unloaded power.

I'm not a car guy, even though all my employer's customers are automotive OEMs and Tier 1s. Don't spoil my quip with your facts.

HI all

I’m finally able to do it.
this is my new code May be useful:
also in this code you can see angle in serial monitor.
thanks all for Response.

#include <Servo.h>

Servo myservo;  
int button1=30;
int button2=31;
int servo=37;
int angle = 90;
void setup() {
  myservo.attach(servo);  
  pinMode(button1,INPUT_PULLUP);
   pinMode(button2,INPUT_PULLUP);
   Serial.begin(9600);
}

void loop() {
  if (digitalRead(button1) == LOW ){
    angle  ++;     
    delay(15);                    
    
   }

 else if (digitalRead(button2) == LOW ){
     angle  --;        
    delay(15);                    
       
  }
  angle = constrain (angle , 0 , 180);
  myservo.write(angle);
  Serial.println(angle);
}

Good that it works. Now improve it.

Suggestions : Reduce memory requirements by declaring variables that will never have a value larger than 255 as type byte Declare any variables that will never change in the program, such as pin numbers, as const Change the program so that it responds when the buttons [u]become[/u] pressed rather than when they [u]are[/u] pressed Remove the delay()s to keep the program responsive to keypresses Add code to deal with contact bounce in the switches.

All of the above may not be strictly necessary with your current program but will be useful in more complicated and demanding programs in the future.

Looks good.
I don’t think you need that ‘else’
and you can replace
‘angle++’
with
‘angle += 5’ or some number to make it move more each step/faster. Same with the –

Very useful Hints you said, thank you very much!!!