Hi, need help, I have a problem.

Hi, I am having trouble doing a small practice and I dont really know what is going wrong. I want to make a servo move when I press a button. While servo is moving a redLED should blink and a arduino should generate a beep. I get all of this right, however I cant get the beep sound. The thing I dont understand is that my beep() does work completely out of the program. If I put beep() at the begining of the loop it does beep and blink. If I put it in another place it just blinks but it doesnt beep. I hope you know what is happening. Im new to the forum so I dont know if this is the place to post this. Thank you for your help.
The code:

#include <Servo.h>

int Alarm = 9; //Alarm Sound
int AlarmLED = 10; //Alarm Light
int Button = 7; // Button Pin
int ServoPin = 8; // Where servo is hooked
int ButtonState;
int servoDelay = 500;
int Position = 30;
int WaterTime = 5000; // Some arbitrary time
int AlarmBeeps = WaterTime / 500;
Servo myServo; // Create a servo object called my servo

void beep() // Creating command beep that combines sound and blinking
{ analogWrite(Alarm, 20);
analogWrite(AlarmLED, 255);
delay(250);
analogWrite (Alarm, 0);
digitalWrite(AlarmLED, LOW);
delay (250);

}

void setup() {
Serial.begin (9600);
pinMode (Alarm, OUTPUT);
pinMode (ServoPin, OUTPUT);
pinMode (Button, INPUT);

}

void loop() {
while (1 == 1) { //Creating a infinite loop that breaks when I press button)

ButtonState = digitalRead(Button);
if (ButtonState == 0) { //Pressing button
break;
}
myServo.attach(ServoPin); // Shut on the servo
myServo.write(180); //Put it into an opened position
analogWrite(Alarm, 20); // Waiting while opened
for (int j = 1; j <= AlarmBeeps; j = j + 1) {
beep(); // Just blinking no sound :frowning:
}
myServo.write(0); // Closing the servo
beep(); // Just blinking no sound :frowning:
beep(); // Just blinking no sound :frowning:
myServo.detach(); // Shut off the servo
beep(); // Just blinking no sound :frowning:
beep(); // Just blinking no sound :frowning:

All your code please, this time in code tags.

  1. Most people here have problems and need help. Write a descriptive subject.
  2. Read how to post in the sticky notes. Put your code in code tags
  3. What is your hardware?
  4. Does changing your ONLY your loop to
void loop() {
    beep();
    delay(1000);
}

REALLY work?
5. Your infinit loop is unnecessary. loop is already an infinite loop that you can break with return.
6. Shutting of the server is probably also not a good idea. AFAIS you will not be able to use this a second time without restarting the Arduino. Servo doc gives my a 502 at the moment, though.

Have a look at the demo Several Things at a Time nd maybe also at Planning and Implementing a Program

...R

Try changing your ALARM to a digital pin that is either HIGH or LOW.

Or you can replace your ALARM with the tone() function.