int sndsnsr = A0;
int pot = A1;
int led = 3;
int alarmled = 5;
int onoff = 4;
int trig = 12;
int echo = 13;

int bpm;
#define MIN_BPM 20
#define MAX_BPM 240

void setup (){
  pinMode (led, OUTPUT);
  pinMode (alarmled, OUTPUT);
  pinMode (onoff, INPUT);
  pinMode (trig, OUTPUT);
  pinMode (echo, INPUT);
  
  Serial.begin(9600);
}
void loop(){
  if (digitalRead (onoff)== HIGH)
  on();
}
void on(){
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long duration, inches;
  duration = pulseIn(echo, HIGH);
  inches = duration/74/2;
  Serial.println(inches);

  int value = analogRead (sndsnsr);
  Serial.println(value);
  
  if((value > 550)||(inches < 15)){
  metronome();
 }
  else {
  alarm();
 }
}
void alarm(){
  digitalWrite (alarmled, HIGH);
  delay (50);
  digitalWrite (alarmled, LOW);
  delay (50);
}

void metronome(){
  bpm = map(analogRead(pot), 0, 1023, MIN_BPM, MAX_BPM);  
  tone(led, 2000);
  delay(6000 / bpm);
  noTone(led);
  delay(54000 / bpm);
}
