Help to make a servo motor run once.

Hi guys can you please help me to make my servo motor run ONCE from 0 to 180 degree if the push button is pressed once. here is my code and I don’t know what’s the problem about it

#include <Servo.h> // (sorry but youtube not support explicit angle bracket) include the Arduino's libray necessary to command the servo

Servo myservo; // creating myservo object
int buttonPin = 8; // set button pin
int buttonState = 0; // set buttonState 

void setup()
{
myservo.attach(2); // attach the 2 pin to servo
pinMode(buttonPin, INPUT); // set button to input
}


void loop()
{ buttonState = digitalRead(buttonPin); // read and save to the variable "buttonState" the actual state of button 
for (int i = 0 ; i <1 ;i++){
if (buttonState == HIGH){
myservo.write(175); 
delay(0);
}
else{
myservo.write(0);
delay(500);
}
}
}

thanks guys I really need your help :slight_smile:

void loop() {
    while (digitalRead(buttonPin) == LOW); // do nothing until button is pressed
    myservo.write(175); 
    while (1) ;  // Do nothing forever after
}

thanks for the reply man.. i'm sorry i did failed to write what I really meant.. sorry for my grammar because english is my 2nd language.. what i'm trying to do is that I want the servo to run only once every time that my alarm code is activated. because my concern is that my alarm code runs for example for 10minutes and if i only use the sweep function for the servo it could run multiple times if the button is pressed.. what i'm trying to do is to limit each functionality of the servo to only one every time the alarm is triggered and automatically turns off the alarm if the button is pressed. I hope you can help me with that here is my code and i'm really having a hard time please help me :frowning:

// Set alarm to activate two times a day
byte HOUR1 = 19, MINUTE1 = 54; 
byte HOUR2 = 19, MINUTE2 = 55;
byte HOUR3 = 19, MINUTE3 = 56;
byte HOUR4 = 19, MINUTE4 = 57;





if(((now.hour()==HOUR1)&&(now.minute()==MINUTE1)))  // compares the time in the RTC circuit to the input time //
{

if(flag == 1)
{
lcd.setCursor(9,1);
lcd.print(" Med 1 ");
lcd.println();
digitalWrite(LED,HIGH);
delay(10000); //delay for 25sec 
lcd.clear(); //Delete the Display of Medicine after the buzzer
delay(1000); //Delay time for the lcd.clear function
flag = 0; // clear flag 
delay(1000);
}
else {

digitalWrite(LED,LOW);
}
}

i really dont know actually how to integrate the servo code to my alarm code. thanks again :slight_smile:

Servo push button test code.

//zoomkat servo button test 12-29-2011

#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);
  }
  
  /*else {
    servo1.write(90);
  }*/
}