จัดทำโดย
1.นาย สุดใจ เพชรสูงเนิน
2.นางสาวเกษร จันทร์สมดี
อุปกรณ์ที่ใช้
1.บอร์ดทดลอง Arduino UNO R3 1 บอร์ด
2.เซนเซอร์Ultrasonic 1 ตัว
3.สายไฟ 18 เส้น
4.บอร์ดรีเลย์ 1 บอร์ด
5.โฟโต้บอร์ด 1 แผง
6.มอเตอร์ 1 ตัว
7.ลำโพง 1 ตัว
8.ตัวต้านทาน 150 โอมป์ 2 ตัว
9.LED 2 ตัว
10.แหล่งจ่ายไฟ 12 v
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
int Pin01 = 5;
int Pin02 = 6;
void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
pinMode(Pin01, OUTPUT); // Motor
pinMode(Pin02, OUTPUT); // Motor
}
void loop()
{
// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 50 && distance >= 0) {
// Buzz
digitalWrite(buzzPin, HIGH);
digitalWrite(Pin01, HIGH); // Motor
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
digitalWrite(Pin01, LOW); // Motor
digitalWrite(Pin02, HIGH); // Motor
delay(500);
digitalWrite(Pin02, LOW); // Motor
}
// Waiting 60 ms won't hurt any one
delay(60);
}
int const echoPin = 9;
int const buzzPin = 2;
int Pin01 = 5;
int Pin02 = 6;
void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
pinMode(Pin01, OUTPUT); // Motor
pinMode(Pin02, OUTPUT); // Motor
}
void loop()
{
// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 50 && distance >= 0) {
// Buzz
digitalWrite(buzzPin, HIGH);
digitalWrite(Pin01, HIGH); // Motor
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
digitalWrite(Pin01, LOW); // Motor
digitalWrite(Pin02, HIGH); // Motor
delay(500);
digitalWrite(Pin02, LOW); // Motor
}
// Waiting 60 ms won't hurt any one
delay(60);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น