อุปกรณ์
SparkFun Arduino Pro Mini 328 - 5V/16MHz5 Ultrasonicsensor SlideSwitch 5 mmLED:Red Buzzr Motor Code const int pingTrigPin = 12; const int pingEchoPin = 10; int buz=5; void setup() { Serial.begin(9600); pinMode(buz, OUTPUT); } void loop() { long duration, cm; pinMode(pingTrigPin, OUTPUT); digitalWrite(pingTrigPin, LOW); delayMicroseconds(2); digitalWrite(pingTrigPin, HIGH); delayMicroseconds(5); digitalWrite(pingTrigPin, LOW); pinMode(pingEchoPin, INPUT); duration = pulseIn(pingEchoPin, HIGH); cm = microsecondsToCentimeters(duration); if(cm<=50 && cm>0) { int d= map(cm, 1, 100, 20, 2000); digitalWrite(buz, HIGH); delay(100); digitalWrite(buz, LOW); delay(d); } Serial.print(cm); Serial.print("cm"); Serial.println(); delay(100); } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; } |
ไม่มีความคิดเห็น:
แสดงความคิดเห็น