พยายามต่อเซ็นเซอร์เข้ากับบอร์ด arduino UNO ให้มันส่งสัญญานผ่านbuzzer แล้วไม่ได้ผล คือโซน่าร์วัดระยะไม่ค่อยถูกแล้วตัวbuzzerไม่ทำงาน ช่วยแนะนำหน่อยค่ะ
นี่โค้ดค่ะ
// Define pins for ultrasonic and buzzer
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
void setup()
{
Serial.begin(9600);
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
}
void loop()
{
//ll 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);
Serial.print("duration ");
Serial.print(duration);
Serial.print("\n");
// Duration wi
// Distance is half the duration devided by 29.1 (from datasheet)
// distance = (duration/2) / 29.1;
distance = (duration*0.034/2);
Serial.print("distance ");
Serial.print(distance*100);
Serial.print("\n");
// 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);
Serial.print("Shuld beep\n");
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
Serial.print("I no beep\n");
}
// Waiting 60 ms won't hurt any one
delay(60);
มีปัญหาโครงงานเกี่ยวกับโซน่าร์ค่ะ มีใครพอช่วยได้บ้าง?
นี่โค้ดค่ะ
// Define pins for ultrasonic and buzzer
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
void setup()
{
Serial.begin(9600);
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
}
void loop()
{
//ll 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);
Serial.print("duration ");
Serial.print(duration);
Serial.print("\n");
// Duration wi
// Distance is half the duration devided by 29.1 (from datasheet)
// distance = (duration/2) / 29.1;
distance = (duration*0.034/2);
Serial.print("distance ");
Serial.print(distance*100);
Serial.print("\n");
// 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);
Serial.print("Shuld beep\n");
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
Serial.print("I no beep\n");
}
// Waiting 60 ms won't hurt any one
delay(60);