คือผมทำโครงงาน ตลับเมตรไร้สายอะครับ
แต่พอวัดค่าและมันเป็นแบบนี้อะครับ
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
float pingPin = 10;
float inPin = 9;
long microsecondsToCentimeters(long microseconds);
void setup() {
Serial.begin (9600);
lcd.init();
lcd.init();
lcd.backlight();
}
void loop()
{
long duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (cm > 300) {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("0-300 CM");
lcd.setCursor(0, 1);
lcd.print("Distance:");
lcd.setCursor(10, 1);
lcd.print("?????");
}
else {
lcd.clear();
lcd.setCursor(4,0);
lcd.print("0-300");
lcd.setCursor(0, 1);
lcd.print("Distance:");
lcd.setCursor(10, 1);
lcd.print(cm);
lcd.setCursor(13, 1);
lcd.print("CM");
}
delay(500);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
เป็นแบบนี้แก้ไงหรอครับ
แต่พอวัดค่าและมันเป็นแบบนี้อะครับ
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
float pingPin = 10;
float inPin = 9;
long microsecondsToCentimeters(long microseconds);
void setup() {
Serial.begin (9600);
lcd.init();
lcd.init();
lcd.backlight();
}
void loop()
{
long duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (cm > 300) {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("0-300 CM");
lcd.setCursor(0, 1);
lcd.print("Distance:");
lcd.setCursor(10, 1);
lcd.print("?????");
}
else {
lcd.clear();
lcd.setCursor(4,0);
lcd.print("0-300");
lcd.setCursor(0, 1);
lcd.print("Distance:");
lcd.setCursor(10, 1);
lcd.print(cm);
lcd.setCursor(13, 1);
lcd.print("CM");
}
delay(500);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}