piezo buzzer + servo motor

Ciao,
sono una studentessa di disegno industriale e sono alle prime armi con Arduino.
Dovrei far illuminare dei led e far suonare il buzzer contemporaneamente e premendo il pulsante dovrebbe partire il servo motore.
Ho un problema con il funzionamento del buzzer e del servo motor, penso che il delaymicroseconds per la programmazione del buzzer fa partire anche il servo motore prima di premere il pulsante. Sembra che il servomotor vada a tempo di musica!!! :wink:

#include <Servo.h> 

int switchPin = 7;
int yellowPin = 11; 
int switchValue;

Servo myservo; 
int pos = 0;

#define  c     3830    // 261 Hz 
#define  d     3400    // 294 Hz 
#define  e     3038    // 329 Hz 
#define  f     2864    // 349 Hz 
#define  g     2550    // 392 Hz 
#define  a     2272    // 440 Hz 
#define  b     2028    // 493 Hz 
#define  C     1912    // 523 Hz 
// Define a special note, 'R', to represent a rest
#define  R     0

// SETUP ============================================
// Set up speaker on a PWM pin (digital 9, 10 or 11)
int speakerOut = 10;
// Do we want debugging on serial out? 1 for yes, 0 for no
int DEBUG = 1;
 

void setup()                    // run once, when the sketch starts
{
  myservo.attach(9);
 pinMode(yellowPin, OUTPUT);  
 pinMode(switchPin, INPUT);        // sets the switchPin to be an input
 digitalWrite(switchPin, HIGH);       // sets the default (unpressed) state of switchPin to HIGH 

 pinMode(speakerOut, OUTPUT);
  if (DEBUG) { 
    Serial.begin(9600);

}
}




int melody[] = {  C,  b,  g,  C,  b,   e,  R,  C,  c,  g, a, C };
int beats[]  = { 16, 16, 16,  8,  8,  16, 32, 16, 16, 16, 8, 8 }; 
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables
int tone = 0;
int beat = 0;
long duration  = 0;

// PLAY TONE  ==============================================
// Pulse the speaker to play a tone for a particular duration
void playTone() {
  long elapsed_time = 0;
  if (tone > 0) { // if this isn't a Rest beat, while the tone has 
    //  played less long than 'duration', pulse speaker HIGH and LOW
    while (elapsed_time < duration) {

      digitalWrite(speakerOut,HIGH);
      delayMicroseconds(tone / 2);

      // DOWN
      digitalWrite(speakerOut, LOW);
      delayMicroseconds(tone / 2);

      // Keep track of how long we pulsed
      elapsed_time += (tone);
    } 
  }
  else { // Rest beat; loop times delay
    for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
      delayMicroseconds(duration);  
    }                                
  }                                 
}






void loop()                     // run over and over again
{
  
  
   for (int i=0; i<MAX_COUNT; i++) {
    tone = melody[i];
    beat = beats[i];

    duration = beat * tempo; // Set up timing

    playTone(); 
    // A pause between notes...
    delayMicroseconds(pause);

    if (DEBUG) { // If debugging, report loop, tone, beat, and duration
      Serial.print(i);
      Serial.print(":");
      Serial.print(beat);
      Serial.print(" ");    
      Serial.print(tone);
      Serial.print(" ");
      Serial.println(duration);
    }
  }
 
 
 
 

  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {    
   analogWrite(yellowPin, fadeValue);
     delay(50);    
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
   analogWrite(yellowPin, fadeValue);
 delay(50); 
  }
  
   switchValue = digitalRead(switchPin);        // check to see if the switch is pressed 

  if (switchValue == HIGH)  {

  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
 
 } 
   

 }
}

Sorry :-[, here's the Translation:
I am a student of industrial design and without experience with Arduino.
I am trying to illuminate the LED and the buzzer to sound simultaneously and pressing the button should start the servo motor.
I have a problem with the operation of the buzzer and the servo motor: The "block" of programming the buzzer is interfering with the servo motor, which moves in the notes, before pressing the button. It seems that the servomotor go to the music!

If you comment out the code to play the music, does the servo operate correctly (moving only when the button is pushed)?

I'm wondering if you have the servo hooked up correctly.

My servo has 3 wires, red, brown, and black. Normally, red is +V and black is Gnd. But, on my servo, brown is Gnd, and black is the control wire.

I wonder if the delayMicroseconds routine is blocking interrupts preventing the servos from generating pulses.

Try adding this code to your sketch and replacing the calls to delayMicroseconds with calls to delayMicrosecondsNoint

void delayMicrosecondsNoint(unsigned int us)
{
#if F_CPU >= 16000000L
      if (--us == 0)
            return;
      us <<= 2;
      us -= 2;
#else
      if (--us == 0)
            return;
      if (--us == 0)
            return;
      us <<= 1;
        us--;
#endif
      // busy wait
      __asm__ __volatile__ (
            "1: sbiw %0,1" "\n\t" // 2 cycles
            "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
      );

}

Thank you very much. Now there isn't any interference, but the servo motor does not work after pressing the switch. The servo motor starts at the end of the cycle of the sound of piezo buzzer and the leds.

pinMode(switchPin, INPUT); // sets the switchPin to be an input
digitalWrite(switchPin, HIGH); // sets the default (unpressed) state of switchPin to HIGH

You have this in your setup method. Try setting switchPin to LOW, instead.

setting the state of an input pin HIGH turns the pull-up resistor on. keep that code as it is.

you should change:
if (switchValue == HIGH)
to
if(switchValue == LOW)