//filename: w8 up exercise Arduino UNO supersonic distance detection (20221030) v2

//#include <DHT.h>

#define trigPin 10   //Ser trigger pin to pin 10
#define echoPin 9     //Set Enable flag to pin 9
#define buzzerPin 12     //Set Enable flag to pin 9

unsigned long distance;    //Set Humidity threshold

//user define function, ping supersonic distance
unsigned long pingSRF() {
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  return pulseIn(echoPin, HIGH, 23200);
}


void setup() {
  // put your setup code here, to run once:
  pinMode(echoPin, INPUT); //set echoPin input
  pinMode(trigPin, OUTPUT); //set trigPin 
  pinMode(buzzerPin, OUTPUT); //set buzzer output when alert

  Serial.begin(19200);
  Serial.println("supersonic distance detection test...");
}

void loop() {
  distance = pingSRF() / 58; //cm
  Serial.println(String("") + distance + " cm");
  if ( distance <= 10 ){
      tone(buzzerPin, 300, 250);
  }
  delay(1000);
}