蜂鳴器接腳8與GND
// HCSR04Ultrasonic/examples/UltrasonicDemo/UltrasonicDemo.pde
#include
#define TRIGGER_PIN 12
#define ECHO_PIN 13
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
void setup()
{
Serial.begin(9600);
Serial.println("CLEARDATA");
Serial.println("LABEL,Time,Timer, D");
}
void loop()
{
float cmMsec, inMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM); // 計算距離,單位: 公分
//inMsec = ultrasonic.convert(microsec, Ultrasonic::IN); // 計算距離,單位: 英吋
// Serial.print("MS: ");
Serial.print("DATA,TIME");
Serial.print(",");
Serial.print("TIMER");
Serial.print(",");
Serial.println(cmMsec);
tone(8, cmMsec*40,500); //cmMsec*40音頻,時間500
// Serial.print(microsec);
// Serial.print(", CM: ");
// Serial.println(cmMsec);
//Serial.print(", IN: ");
// Serial.println(inMsec);
delay(100);
}
沒有留言:
張貼留言