Prove arduino

Buzzer_ping :

#define trigPin 13
#define echoPin 12
 
#define DO 10
#define RE 20
#define MI 30
#define FA 40
#define SOL 50
#define LA 60
#define SI 70
 
 
int buzzer = 8 ;
int caso ;
int variabile;
int giro = 0;
long duration, distanza;
 
 
void setup() {
  Serial.begin (115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzer, OUTPUT);
}
 
void loop() {
  delay(50); // ??? lasciare ?
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distanza = ((duration/2)*10) / 291;
 
  if (distanza >= 200 || distanza <= 0){
    Serial.println("Fuori portata");
  }
  else {
    Serial.print(distanza);
    Serial.println(" cm");
  }
 
  suona();
  programma();
  pausa();
}
 
void suona() {
	if (distanza < DO) {               //in base alla distanza
		noTone(buzzer);                //fa silenzio
	}
	else if (distanza < RE ) {
		tone(buzzer, 262);             //o riproduce una nota
		caso = 1;
	}
	else if (distanza < MI) {
		tone(buzzer, 294);
		caso = 2;
	}
	else if (distanza < FA) {
		tone(buzzer, 330);
		caso = 3;
	}
	else if (distanza < SOL) {
		tone(buzzer, 349);
		caso = 4;
	}
	else if (distanza < LA) {
		tone(buzzer, 392);
		caso = 5;
	}
	else {
	    noTone(buzzer);
	}
}
 
void programma() {           //serve per avviare la serie di confronti della prossima funzione
	if (giro == 0) {
		variabile = caso;
		++giro;              //dopo il primo loop si disattiva
	}
	else {
 
	}
}
 
void pausa() {
	if (variabile == caso) { //se la nota appena riprodotta e la precedente sono la stessa non accade nulla
 
	}
	else {                  //altrimenti c'è una pausa
		delay(100);
		variabile = caso;   //viene memorizzata la nota corrente
	}
}

Buzzer_NewPing :

/*  library: https://bitbucket.org/teckel12/arduino-new-ping/downloads 
    home project: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
	  arduino forum: http://forum.arduino.cc/index.php?topic=106043.0
    for verctor_7 error edit NewPing.h file ---> set TIMER_ENABLED false
    or use NewTone : https://bitbucket.org/teckel12/arduino-new-ping/wiki/Multiple%20Definition%20of%20%22__vector_7%22%20Error
	*/
 
 
#include <NewPing.h>
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
 
#define DO 10
#define RE 20
#define MI 30
#define FA 40
#define SOL 50
#define LA 60
#define SI 70
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
 
int buzzer = 8 ;
int distanza ;
int caso ;
int variabile;
int giro = 0;
 
 
 
void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pinMode(buzzer, OUTPUT);
}
 
void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(sonar.convert_cm(uS));  // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  distanza = sonar.convert_cm(uS);
  Serial.println("cm");
  suona();
  programma();
  pausa();
}
 
void suona() {
	if (distanza < DO) {               //in base alla distanza
		noTone(buzzer);                //fa silenzio
	}
	else if (distanza < RE ) {
		tone(buzzer, 262);             //o riproduce una nota
		caso = 1;
	}
	else if (distanza < MI) {
		tone(buzzer, 294);
		caso = 2;
	}
	else if (distanza < FA) {
		tone(buzzer, 330);
		caso = 3;
	}
	else if (distanza < SOL) {
		tone(buzzer, 349);
		caso = 4;
	}
	else if (distanza < LA) {
		tone(buzzer, 392);
		caso = 5;
	}
	else {
	    noTone(buzzer);
	}
}
 
void programma() {           //serve per avviare la serie di confronti della prossima funzione
	if (giro == 0) {
		variabile = caso;
		++giro;              //dopo il primo loop si disattiva
	}
	else {
 
	}
}
 
void pausa() {
	if (variabile == caso) { //se la nota appena riprodotta e la precedente sono la stessa non accade nulla
 
	}
	else {                  //altrimenti c'è una pausa
		delay(100);
		variabile = caso;   //viene memorizzata la nota corrente
	}
}

*TEST* Button :

// NB: ALL NOTES DEFINED WITH STANDARD ENGLISH NAMES, EXCEPT FROM "A" 
//THAT IS CALLED WITH THE ITALIAN NAME "LA" BECAUSE A0,A1...ARE THE ANALOG PINS ON ARDUINO.
// (Ab IS CALLED Ab AND NOT LAb)
#define	C0 16.35
#define	Db0	17.32
#define	D0	18.35
#define	Eb0	19.45
#define	E0	20.60
#define	F0	21.83
#define	Gb0	23.12
#define	G0	24.50
#define	Ab0	25.96
#define	LA0	27.50
#define	Bb0	29.14
#define	B0	30.87
#define	C1	32.70
#define	Db1	34.65
#define	D1	36.71
#define	Eb1	38.89
#define	E1	41.20
#define	F1	43.65
#define	Gb1	46.25
#define	G1	49.00
#define	Ab1	51.91
#define	LA1	55.00
#define	Bb1	58.27
#define	B1	61.74
#define	C2	65.41
#define	Db2	69.30
#define	D2	73.42
#define	Eb2	77.78
#define	E2	82.41
#define	F2	87.31
#define	Gb2	92.50
#define	G2	98.00
#define	Ab2	103.83
#define	LA2	110.00
#define	Bb2	116.54
#define	B2	123.47
#define	C3	130.81
#define	Db3	138.59
#define	D3	146.83
#define	Eb3	155.56
#define	E3	164.81
#define	F3	174.61
#define	Gb3	185.00
#define	G3	196.00
#define	Ab3	207.65
#define	LA3	220.00
#define	Bb3	233.08
#define	B3	246.94
#define	C4	261.63
#define	Db4	277.18
#define	D4	293.66
#define	Eb4	311.13
#define	E4	329.63
#define	F4	349.23
#define	Gb4	369.99
#define	G4	392.00
#define	Ab4	415.30
#define	LA4	440.00
#define	Bb4	466.16
#define	B4	493.88
#define	C5	523.25
#define	Db5	554.37
#define	D5	587.33
#define	Eb5	622.25
#define	E5	659.26
#define	F5	698.46
#define	Gb5	739.99
#define	G5	783.99
#define	Ab5	830.61
#define	LA5	880.00
#define	Bb5	932.33
#define	B5	987.77
#define	C6	1046.50
#define	Db6	1108.73
#define	D6	1174.66
#define	Eb6	1244.51
#define	E6	1318.51
#define	F6	1396.91
#define	Gb6	1479.98
#define	G6	1567.98
#define	Ab6	1661.22
#define	LA6	1760.00
#define	Bb6	1864.66
#define	B6	1975.53
#define	C7	2093.00
#define	Db7	2217.46
#define	D7	2349.32
#define	Eb7	2489.02
#define	E7	2637.02
#define	F7	2793.83
#define	Gb7	2959.96
#define	G7	3135.96
#define	Ab7	3322.44
#define	LA7	3520.01
#define	Bb7	3729.31
#define	B7	3951.07
#define	C8	4186.01
#define	Db8	4434.92
#define	D8	4698.64
#define	Eb8	4978.03
// DURATION OF THE NOTES 
#define BPM 120    //  you can change this value changing all the others
#define H 2*Q //half 2/4
#define Q 60000/BPM //quarter 1/4 
#define E Q/2   //eighth 1/8
#define S Q/4 // sixteenth 1/16
#define W 4*Q // whole 4/4
 
 
int button =13;
int state =0;
int programma = 0;
 
void setup() {
pinMode(button,INPUT);
pinMode(8, OUTPUT);   
pinMode(9, OUTPUT);       
digitalWrite(9,LOW);
}
 
 
void loop() {
 incremento();
 if (programma) {
   funzione();
}
 if (programma == ) {
   funzione2();
 }
 if (programma == ) {
   funzione3();
 }
}
 
 
void incremento() {
    state=digitalRead(button);
  if (state ==HIGH){
    programma=programma+1;
 
    if (programma>2) {
      programma=0;
    }
    else {
 
    }
  }
  else{
 
  }
  Serial.print("Programma: ");
  Serial.println(programma);
  delay(50);
}
 
 
 
void funzione2 () {
 
}
 
void funzione3 () {
 
  }
 
 
 
 
// the loop routine runs over and over again forever:
void funzione () {
  //tone(pin, note, duration)
    tone(8,LA3,Q); 
    delay(1+Q); //delay duration should always be 1 ms more than the note in order to separate them.
    tone(8,LA3,Q);
    delay(1+Q);
    tone(8,LA3,Q);
    delay(1+Q);
    tone(8,F3,E+S);
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
 
    tone(8,LA3,Q);
    delay(1+Q);
    tone(8,F3,E+S);
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
    tone(8,LA3,H);
    delay(1+H);
 
    tone(8,E4,Q); 
    delay(1+Q); 
    tone(8,E4,Q);
    delay(1+Q);
    tone(8,E4,Q);
    delay(1+Q);
    tone(8,F4,E+S);
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
 
    tone(8,Ab3,Q);
    delay(1+Q);
    tone(8,F3,E+S);
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
    tone(8,LA3,H);
    delay(1+H);
 
    tone(8,LA4,Q);
    delay(1+Q);
    tone(8,LA3,E+S);
    delay(1+E+S);
    tone(8,LA3,S);
    delay(1+S);
    tone(8,LA4,Q);
    delay(1+Q);
    tone(8,Ab4,E+S);
    delay(1+E+S);
    tone(8,G4,S);
    delay(1+S);
 
    tone(8,Gb4,S);
    delay(1+S);
    tone(8,E4,S);
    delay(1+S);
    tone(8,F4,E);
    delay(1+E);
    delay(1+E);//PAUSE
    tone(8,Bb3,E);
    delay(1+E);
    tone(8,Eb4,Q);
    delay(1+Q);
    tone(8,D4,E+S);
    delay(1+E+S);
    tone(8,Db4,S);
    delay(1+S);
 
    tone(8,C4,S);
    delay(1+S);
    tone(8,B3,S);
    delay(1+S);
    tone(8,C4,E);
    delay(1+E);
    delay(1+E);//PAUSE QUASI FINE RIGA
    tone(8,F3,E);
    delay(1+E);
    tone(8,Ab3,Q);
    delay(1+Q);
    tone(8,F3,E+S);
    delay(1+E+S);
    tone(8,LA3,S);
    delay(1+S);
 
    tone(8,C4,Q);
    delay(1+Q);
     tone(8,LA3,E+S);
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
    tone(8,E4,H);
    delay(1+H);
 
     tone(8,LA4,Q);
    delay(1+Q);
    tone(8,LA3,E+S);
    delay(1+E+S);
    tone(8,LA3,S);
    delay(1+S);
    tone(8,LA4,Q);
    delay(1+Q);
    tone(8,Ab4,E+S);
    delay(1+E+S);
    tone(8,G4,S);
    delay(1+S);
 
    tone(8,Gb4,S);
    delay(1+S);
    tone(8,E4,S);
    delay(1+S);
    tone(8,F4,E);
    delay(1+E);
    delay(1+E);//PAUSE
    tone(8,Bb3,E);
    delay(1+E);
    tone(8,Eb4,Q);
    delay(1+Q);
    tone(8,D4,E+S);
    delay(1+E+S);
    tone(8,Db4,S);
    delay(1+S);
 
    tone(8,C4,S);
    delay(1+S);
    tone(8,B3,S);
    delay(1+S);
    tone(8,C4,E);
    delay(1+E);
    delay(1+E);//PAUSE QUASI FINE RIGA
    tone(8,F3,E);
    delay(1+E);
    tone(8,Ab3,Q);
    delay(1+Q);
    tone(8,F3,E+S);
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
 
    tone(8,LA3,Q);
    delay(1+Q);
     tone(8,F3,E+S);
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
    tone(8,LA3,H);
    delay(1+H);
 
    delay(2*H);
 
}

*TEST* button 1.0 :

// NB: ALL NOTES DEFINED WITH STANDARD ENGLISH NAMES, EXCEPT FROM "A" 
//THAT IS CALLED WITH THE ITALIAN NAME "LA" BECAUSE A0,A1...ARE THE ANALOG PINS ON ARDUINO.
// (Ab IS CALLED Ab AND NOT LAb)
#define  C0 16.35
#define Db0 17.32
#define D0  18.35
#define Eb0 19.45
#define E0  20.60
#define F0  21.83
#define Gb0 23.12
#define G0  24.50
#define Ab0 25.96
#define LA0 27.50
#define Bb0 29.14
#define B0  30.87
#define C1  32.70
#define Db1 34.65
#define D1  36.71
#define Eb1 38.89
#define E1  41.20
#define F1  43.65
#define Gb1 46.25
#define G1  49.00
#define Ab1 51.91
#define LA1 55.00
#define Bb1 58.27
#define B1  61.74
#define C2  65.41
#define Db2 69.30
#define D2  73.42
#define Eb2 77.78
#define E2  82.41
#define F2  87.31
#define Gb2 92.50
#define G2  98.00
#define Ab2 103.83
#define LA2 110.00
#define Bb2 116.54
#define B2  123.47
#define C3  130.81
#define Db3 138.59
#define D3  146.83
#define Eb3 155.56
#define E3  164.81
#define F3  174.61
#define Gb3 185.00
#define G3  196.00
#define Ab3 207.65
#define LA3 220.00
#define Bb3 233.08
#define B3  246.94
#define C4  261.63
#define Db4 277.18
#define D4  293.66
#define Eb4 311.13
#define E4  329.63
#define F4  349.23
#define Gb4 369.99
#define G4  392.00
#define Ab4 415.30
#define LA4 440.00
#define Bb4 466.16
#define B4  493.88
#define C5  523.25
#define Db5 554.37
#define D5  587.33
#define Eb5 622.25
#define E5  659.26
#define F5  698.46
#define Gb5 739.99
#define G5  783.99
#define Ab5 830.61
#define LA5 880.00
#define Bb5 932.33
#define B5  987.77
#define C6  1046.50
#define Db6 1108.73
#define D6  1174.66
#define Eb6 1244.51
#define E6  1318.51
#define F6  1396.91
#define Gb6 1479.98
#define G6  1567.98
#define Ab6 1661.22
#define LA6 1760.00
#define Bb6 1864.66
#define B6  1975.53
#define C7  2093.00
#define Db7 2217.46
#define D7  2349.32
#define Eb7 2489.02
#define E7  2637.02
#define F7  2793.83
#define Gb7 2959.96
#define G7  3135.96
#define Ab7 3322.44
#define LA7 3520.01
#define Bb7 3729.31
#define B7  3951.07
#define C8  4186.01
#define Db8 4434.92
#define D8  4698.64
#define Eb8 4978.03
// DURATION OF THE NOTES 
#define BPM 120    //  you can change this value changing all the others
#define H 2*Q //half 2/4
#define Q 60000/BPM //quarter 1/4 
#define E Q/2   //eighth 1/8
#define S Q/4 // sixteenth 1/16
#define W 4*Q // whole 4/4
 
 
int button =13;
int state =0;
int programma = 0;
 
#include <NewPing.h>
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define RITARDO 150
 
#define DO 10
#define RE 20
#define MI 30
#define FA 40
#define SOL 50
#define LA 60
#define SI 70
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
 
 
int buzzer = 8 ;
int distanza ;
int caso ;
int variabile;
int giro = 0;
int mode;
 
void primapausa();
 
 
 
 
void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
    pinMode(button,INPUT);
    pinMode(8, OUTPUT);   
    pinMode(9, OUTPUT);       
    digitalWrite(9,LOW);
    pinMode(buzzer, OUTPUT);
}
 
 
void loop() {
 incremento();
 mode = programma;
 while (programma == 0) {
   funzione();
   incremento();
}
 while (programma == 1) {
   funzione2();
   incremento();
 }
 while (programma == 2) {
   funzione3();
   incremento();
 }
}
 
void check () {
  incremento();
  if (mode == programma){
 
  }
  else {
 
    loop(); 
    noTone(8);
  }
}
 
 
 
 
void incremento() {
    state=digitalRead(button);
  if (state ==HIGH){
    programma=programma+1;
    delay(100);
 
    if (programma>2) {
      programma=0;
    }
    else {
 
    }
  }
  else{
 
  }
  Serial.print("Programma: ");
  Serial.println(programma);
  delay(50);
}
 
void funzione2() {
  delay(RITARDO);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
//  Serial.print("Ping: ");
//  Serial.print(sonar.convert_cm(uS));  // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  distanza = sonar.convert_cm(uS);
//  Serial.println("cm");
  suona();
//  primapausa();
//  pausa();
}
 
void primapausa() {
  if (giro == 0) {    //serve per avviare la serie di confronti della prossima funzione
    variabile = caso;
    ++giro;              //dopo il primo loop si disattiva
  }
  if (giro ==1) {
    pausa();
 
  }
}
 
 
/*void suona() {
  if (distanza < DO) {               //in base alla distanza
    noTone(buzzer);                //fa silenzio
  }
  else if (distanza < RE ) {
    tone(buzzer, 262);             //o riproduce una nota
 
    caso = 1;
  }
  else if (distanza < MI) {
    tone(buzzer, 294);
 
    caso = 2;
  }
  else if (distanza < FA) {
    tone(buzzer, 330);
 
    caso = 3;
  }
  else if (distanza < SOL) {
    tone(buzzer, 349);
 
    caso = 4;
  }
  else if (distanza < LA) {
    tone(buzzer, 392);
 
    caso = 5;
  }
  else {
      noTone(buzzer);
  }
}*/
 
void suona() {
  if (distanza < DO) {               //in base alla distanza
    noTone(buzzer);                //fa silenzio
  }
  else if (distanza < RE ) {
    tone(buzzer, 262, 1000);             //o riproduce una nota
 
    caso = 1;
  }
  else if (distanza < MI) {
    tone(buzzer, 294, 1000);
 
    caso = 2;
  }
  else if (distanza < FA) {
    tone(buzzer, 330, 1000);
 
    caso = 3;
  }
  else if (distanza < SOL) {
    tone(buzzer, 349, 1000);
 
    caso = 4;
  }
  else if (distanza < LA) {
    tone(buzzer, 392, 1000);
 
    caso = 5;
  }
  else {
 
  }
  check();
}
 
 
void pausa() {
  if (variabile == caso) { //se la nota appena riprodotta e la precedente sono la stessa non accade nulla
 
  }
 
  if (variabile != caso) {                  //altrimenti c'è una pausa
    delay(100);
    variabile = caso;   //viene memorizzata la nota corrente
  }
}
 
 
 
void funzione3 () {
  check();
  }
 
 
 
// the loop routine runs over and over again forever:
void funzione () {
  //tone(pin, note, duration)
    tone(8,LA3,Q); 
      check();
    delay(1+Q); //delay duration should always be 1 ms more than the note in order to separate them.
    tone(8,LA3,Q);
    delay(1+Q);
  check();
    tone(8,LA3,Q);
    delay(1+Q);
    tone(8,F3,E+S);
      check();
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
  check();
 
    tone(8,LA3,Q);
    delay(1+Q);
    tone(8,F3,E+S);
    delay(1+E+S);
  check();
    tone(8,C4,S);
    delay(1+S);
    tone(8,LA3,H);
    delay(1+H);
    check();
    tone(8,E4,Q); 
    delay(1+Q); 
    tone(8,E4,Q);
      check();
    delay(1+Q);
    tone(8,E4,Q);
    delay(1+Q);
  check();
    tone(8,F4,E+S);
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
    check();
    tone(8,Ab3,Q);
    delay(1+Q);
    tone(8,F3,E+S);
      check();
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
    tone(8,LA3,H);
    delay(1+H);
    check();
    tone(8,LA4,Q);
    delay(1+Q);
    tone(8,LA3,E+S);
      check();
    delay(1+E+S);
    tone(8,LA3,S);
    delay(1+S);
    tone(8,LA4,Q);
  check();
    delay(1+Q);
    tone(8,Ab4,E+S);
    delay(1+E+S);
    tone(8,G4,S);
    delay(1+S);
    check();
    tone(8,Gb4,S);
    delay(1+S);
    tone(8,E4,S);
    delay(1+S);
      check();
    tone(8,F4,E);
    delay(1+E);
    delay(1+E);//PAUSE
    tone(8,Bb3,E);
    delay(1+E);
  check();
    tone(8,Eb4,Q);
    delay(1+Q);
    tone(8,D4,E+S);
    delay(1+E+S);
    tone(8,Db4,S);
    delay(1+S);
    check();
    tone(8,C4,S);
    delay(1+S);
    tone(8,B3,S);
      check();
    delay(1+S);
    tone(8,C4,E);
    delay(1+E);
    delay(1+E);//PAUSE QUASI FINE RIGA
    tone(8,F3,E);
    delay(1+E);
  check();
    tone(8,Ab3,Q);
    delay(1+Q);
    tone(8,F3,E+S);
      check();
    delay(1+E+S);
    tone(8,LA3,S);
    delay(1+S);
    check();
    tone(8,C4,Q);
    delay(1+Q);
     tone(8,LA3,E+S);
    delay(1+E+S);
  check();
    tone(8,C4,S);
    delay(1+S);
    tone(8,E4,H);
    delay(1+H);
    check();
     tone(8,LA4,Q);
    delay(1+Q);
    tone(8,LA3,E+S);
    delay(1+E+S);
    tone(8,LA3,S);
    delay(1+S);
  check();
    tone(8,LA4,Q);
    delay(1+Q);
    tone(8,Ab4,E+S);
      check();
    delay(1+E+S);
    tone(8,G4,S);
    delay(1+S);
    check();
    tone(8,Gb4,S);
    delay(1+S);
    tone(8,E4,S);
    delay(1+S);
    tone(8,F4,E);
    delay(1+E);
  check();
    delay(1+E);//PAUSE
    tone(8,Bb3,E);
    delay(1+E);
    tone(8,Eb4,Q);
      check();
    delay(1+Q);
    tone(8,D4,E+S);
    delay(1+E+S);
    tone(8,Db4,S);
    delay(1+S);
    check();
    tone(8,C4,S);
    delay(1+S);
    tone(8,B3,S);
    delay(1+S);
  check();
    tone(8,C4,E);
    delay(1+E);
    delay(1+E);//PAUSE QUASI FINE RIGA
    tone(8,F3,E);
      check();
    delay(1+E);
    tone(8,Ab3,Q);
    delay(1+Q);
  check();
    tone(8,F3,E+S);
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
    check();
    tone(8,LA3,Q);
    delay(1+Q);
     tone(8,F3,E+S);
    delay(1+E+S);
    tone(8,C4,S);
    delay(1+S);
    tone(8,LA3,H);
    delay(1+H);
    check();
    delay(2*H);
 
}

Buzze_Newping 2 :

 
/*  library: https://bitbucket.org/teckel12/arduino-new-ping/downloads 
    home project: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
	  arduino forum: http://forum.arduino.cc/index.php?topic=106043.0
    for verctor_7 error edit NewPing.h file ---> set TIMER_ENABLED false
    or use NewTone : https://bitbucket.org/teckel12/arduino-new-ping/wiki/Multiple%20Definition%20of%20%22__vector_7%22%20Error
	*/
 
 
#include <NewPing.h>
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define RITARDO 150
 
#define DO 10
#define RE 20
#define MI 30
#define FA 40
#define SOL 50
#define LA 60
#define SI 70
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
 
 
int buzzer = 8 ;
int distanza ;
int caso ;
int variabile;
int giro = 0;
 
void primapausa();
 
void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pinMode(buzzer, OUTPUT);
}
 
void loop() {
  delay(RITARDO);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
//  Serial.print("Ping: ");
//  Serial.print(sonar.convert_cm(uS));  // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  distanza = sonar.convert_cm(uS);
//  Serial.println("cm");
  suona();
//  primapausa();
//  pausa();
}
 
void primapausa() {
	if (giro == 0) {    //serve per avviare la serie di confronti della prossima funzione
		variabile = caso;
		++giro;              //dopo il primo loop si disattiva
	}
	if (giro ==1) {
	  pausa();
 
	}
}
 
 
/*void suona() {
	if (distanza < DO) {               //in base alla distanza
		noTone(buzzer);                //fa silenzio
	}
	else if (distanza < RE ) {
		tone(buzzer, 262);             //o riproduce una nota
 
		caso = 1;
	}
	else if (distanza < MI) {
		tone(buzzer, 294);
 
		caso = 2;
	}
	else if (distanza < FA) {
		tone(buzzer, 330);
 
		caso = 3;
	}
	else if (distanza < SOL) {
		tone(buzzer, 349);
 
		caso = 4;
	}
	else if (distanza < LA) {
		tone(buzzer, 392);
 
		caso = 5;
	}
	else {
	    noTone(buzzer);
	}
}*/
 
void suona() {
	if (distanza < DO) {               //in base alla distanza
		noTone(buzzer);                //fa silenzio
	}
	else if (distanza < RE ) {
		tone(buzzer, 262, 1000);             //o riproduce una nota
 
		caso = 1;
	}
	else if (distanza < MI) {
		tone(buzzer, 294, 1000);
 
		caso = 2;
	}
	else if (distanza < FA) {
		tone(buzzer, 330, 1000);
 
		caso = 3;
	}
	else if (distanza < SOL) {
		tone(buzzer, 349, 1000);
 
		caso = 4;
	}
	else if (distanza < LA) {
		tone(buzzer, 392, 1000);
 
		caso = 5;
	}
	else {
	    noTone(buzzer);
	}
}
 
 
void pausa() {
	if (variabile == caso) { //se la nota appena riprodotta e la precedente sono la stessa non accade nulla
 
	}
 
	if (variabile != caso) {                  //altrimenti c'è una pausa
		delay(100);
		variabile = caso;   //viene memorizzata la nota corrente
	}
}