can't get piezo to beep in my program

I found the following code to get a piezo to beep, and tried it out and it worked great:

int speakerOut = 10;         // piezo



void setup() 
{ 
  pinMode(speakerOut, OUTPUT);
} 

void loop() 
{  


analogWrite(speakerOut,128);
delay(250);
digitalWrite(speakerOut, LOW);
delay(250);

  
}

It works great, but when I try to implement it into my own program, it doesn’t seem to beep.

// Controlling a full rotational servo

// Attach red wire of servo to TO SEPARATE POWER SUPPLY
// Attach black wire of servo to GND on Arduino
// Attach control wire of servo to pin 9 on Arduino


// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int topLimitSwitch = 4; // the upper limit switch
const int bottomLimitSwitch = 8; // the lower limit switch
const int ledPin =  13;     // the number of the LED pin (Currently not used)

// Variables will change:
int ledState = LOW;         // the current state of the output pin
int lastButtonState = LOW;  // the previous debounced button state
int lastReading= LOW;       // the previous reading from the input pin
int speakerOut = 10;         // piezo

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the input pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers



int servoState = 1500; // the current state of the servo

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 



void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  
  pinMode(buttonPin, INPUT);
  pinMode(topLimitSwitch, INPUT);
  pinMode(bottomLimitSwitch, INPUT);
  pinMode(ledPin, OUTPUT);
  
  pinMode(speakerOut, OUTPUT);
} 

void loop() 
{  

  //myservo.writeMicroseconds(1500); //stay idle
  
  //myservo.writeMicroseconds(1500); //
  //Serial.print("Running motor at: ");
  
  
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
  
  // read the state of the upper and lower limit switches into a local variable:
  int upperSwitchReading = digitalRead(topLimitSwitch);
  int lowerSwitchReading = digitalRead(bottomLimitSwitch);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastReading) {
    // reset the debouncing timer
    lastDebounceTime = millis();
    // save the reading.  Next time through the loop,
    // it'll be lastReading:
    lastReading = reading;
  }
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so accept the button changed state:
  
    // toggle the LED if the state of the button changes from LOW to HIGH:
    if (lastButtonState == LOW && reading == HIGH) {
          
      
      // MAIN CODE GOES HERE
      
      // Turn on servo clockwise to raise cover until it hits the upper limit switch
      while (digitalRead(topLimitSwitch) == LOW) {
        myservo.writeMicroseconds(3000);
      }
      
      // Make servo be idle again
      myservo.writeMicroseconds(1520);
      
      // Wait 5 seconds
      delay(5000);
      
      
       // beep a few times to give the cat a warning before closing door
        for (long i = 1; i < 7; i++)
        {
          analogWrite(speakerOut,128);
          delay(250);
          digitalWrite(speakerOut, LOW);
          delay(250);
        }
      
      
      
      // Turn on servo couter-clockwise to lower cover until it hits the lower limit switch
      while (digitalRead(bottomLimitSwitch) == LOW) {
        // ok, now close the door (rotate servo counter-clockwise)
        myservo.writeMicroseconds(1400);     
      }
      
      // Make servo be idle again
      myservo.writeMicroseconds(1520);
      
      
      // END MAIN CODE
      
    }
    lastButtonState = reading;
  }
  
  
}

I don’t understand why it works in the first program, but not in mine. When I upload the program to my Arduino, I’m not making any changes to my circuit or anything. And in my program, it’s in a loop, so I don’t understand what’s going on.

When you are using servos, the timer that operates PWM on pins 10 and 11 is hijacked for use by the servos. PWM on those 2 pins no longer works. Try a different PWM pin.

Wow, thanks. I didn't know that (obviously). I'll try it when I get home!

If you're using a modern Arduino IDE, you'd be better using the tone command, much simpler.

tone(11,440,150);

Sounds 440 Hz for 150 ms on pin 11. It works 'in the background' too so it doesn't tie up the controller whilst its playing.

Doesn't the tone command use a timer? If so, which one? What PWM pins does that timer impact?

PaulS,

Trying a different PWM pin worked! Thank you so much.

Pluggy:

Thanks for the suggestion - I'll check into that.