13.紅外線測距儀
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,16,2); // 若LCD 無法啟用,請將0X3F 改為0X27
int x;
float V, Distance ;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop() {
x = analogRead(A0);
V = x*5.000000/1023.000000 ;
Distance = 60.374*pow(V , -1.16); //GP2Y0A02YK0F (20150)
//Distance = 29.988*pow(V , -1.173); //GP2Y0A21YK (1080)
//Distance = 12.08*pow(V , -1.058); //GP2YA41SK0F (0430)
lcd.setCursor(0,0);
lcd.print("A0=");
lcd.print(Distance);
lcd.print(" ");
delay(100);
}