Хэрэглэгдэхүүн:
- Ардиуно 1ш
- Хэт авиа мэдрэгч 1ш
- Баззер 1ш
- Утас
Зарчмын схем
Зарчмын схем
Программ
const int pingTrigPin = 12; //Trigger connected to PIN 7
const int pingEchoPin = 10; //Echo connected yo PIN 8
int buz=5; //Buzzer to PIN 4
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;
}