วันอังคารที่ 11 กรกฎาคม พ.ศ. 2560

ใบงานที่ 4 การวัดระยะด้วยเซนเซอร์แสดงผลด้วย LED

การวัดระยะด้วยเซนเซอร์แสดงผลด้วย LED


จัดทำโดย

นาย สุดใจ เพชรสูงเนิน
นางสาวเกษร จันทร์สมดี

อุปกรณ์
1.บอร์ดทดลอง Arduino UNO R3    1   บอร์ด
2.สาย USB                                    1   เส้น
3.เซนเซอร์Ultrasonic                                          1   ตัว
4.สายไฟ                                        13   เส้น
5.โฟโต้บอร์ด                                  1   แผง
6.ลำโพง                                        1  ตัว
7.ตัวต้านทาน 150 โอมป์                 2  ตัว  
8.LED                                           2  ตัว  



























    int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
int Pin01 = A0;
int Pin02 = A1;
int Pin03 = A2;
int Pin04 = A3;
int Pin05 = A4;
int Pin06 = A5;
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); 
pinMode(Pin02, OUTPUT); 
pinMode(Pin03, OUTPUT); 
pinMode(Pin04, OUTPUT); 
pinMode(Pin05, OUTPUT); 
pinMode(Pin06, OUTPUT);
}
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 <=30 && distance >= 20) {
// Buzz
digitalWrite(Pin01, HIGH);
digitalWrite(Pin02, HIGH);
digitalWrite(buzzPin, HIGH);
delay(1000);
digitalWrite(buzzPin, LOW);
delay(1000);
}else if(distance <=19 && distance >= 10) {
digitalWrite(Pin01, HIGH);
digitalWrite(Pin02, HIGH);
digitalWrite(Pin03, HIGH);
digitalWrite(Pin04, HIGH);
digitalWrite(buzzPin, HIGH);
delay(500);
digitalWrite(buzzPin, LOW);
delay(500);

}else if(distance <=9 && distance >= 5) {
digitalWrite(Pin01, HIGH);
digitalWrite(Pin02, HIGH);
digitalWrite(Pin03, HIGH);
digitalWrite(Pin04, HIGH);
digitalWrite(Pin05, HIGH);
digitalWrite(Pin06, HIGH);
digitalWrite(buzzPin, HIGH);
delay(200);
digitalWrite(buzzPin, LOW);
delay(200);

}else if(distance <=5 && distance >= 0) {
digitalWrite(Pin01, HIGH);
digitalWrite(Pin02, HIGH);
digitalWrite(Pin03, HIGH);
digitalWrite(Pin04, HIGH);
digitalWrite(Pin05, HIGH);
digitalWrite(Pin06, HIGH);
digitalWrite(buzzPin, HIGH);
delay(200);



}
}                        

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

ใบงานที่ 4 การวัดระยะด้วยเซนเซอร์แสดงผลด้วย LED

การวัดระยะด้วยเซนเซอร์แสดงผลด้วย LED จัดทำโดย นาย สุดใจ เพชรสูงเนิน นางสาวเกษร จันทร์สมดี อุปกรณ์ 1.บอร์ดทดลอง Arduino UNO R3    1...