|
你指的是1602嗎?
我寫了LCD 1602版本與OLED 12864版本
LCD:
[mw_shl_code=c,true]#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int trig = 6;
const int echo = 7;
int inter_time = 1000;
int time = 0;
void setup() {
lcd.begin(16, 2);
pinMode (trig, OUTPUT);
pinMode (echo, INPUT);
}
void loop() {
float duration, distance;
digitalWrite(trig, HIGH);
delayMicroseconds(1000);
digitalWrite(trig, LOW);
duration = pulseIn (echo, HIGH);
distance = (duration/50);
lcd.setCursor (0,0);
lcd.print("Data No.");
lcd.print (time);
time = time +1;
lcd.setCursor(0, 1);
lcd.print ("Distance:");
lcd.print(distance);
lcd.print ("cm");
delay(900);
lcd.clear();
delay(100);
}[/mw_shl_code]
OLED:
[mw_shl_code=c,true]/*-----Library-----*/
#include <Wire.h>
#include <U8glib.h>
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
/*-----HC-SR04 Setup-----*/
const int trig = 6; //聲納輸出腳位
const int echo = 7; //聲納接收腳位
int inter_time = 1000; //偵測時間
int time = 0;
/*-----Char Setup-----*/
char str[10];
void drawTest()
{
u8g.setFont(u8g_font_osb18);
u8g.drawStr( 0, 30, "Loading...");
}
void setup()
{
Serial.begin(9600);
delay(500);
Serial.println("Loading...");
u8g.firstPage();
u8g.nextPage();
pinMode (trig,OUTPUT);
pinMode (echo,INPUT);
do
{
drawTest();
} while(u8g.nextPage());
}
void loop()
{
/*-----HC-SR04 Setup-----*/
float duration, distance;
digitalWrite(trig,HIGH);
delayMicroseconds(1000);
digitalWrite(trig,LOW);
duration = pulseIn (echo,HIGH);
distance = (duration/50);
time = time + 1;
delay(900);
/*-----Print in Monitor-----*/
Serial.print("Distance: ");
Serial.print(distance);
Serial.print("cm");
/*-----print in OLED-----*/
u8g.firstPage();
do {
u8g.setFont(u8g_font_helvB08); //字型
u8g.drawStr( 0, 30, "Distance:");
u8g.drawStr( 50, 30, dtostrf(distance, 5, 2, str));
u8g.drawStr( 90, 30, "cm");
} while( u8g.nextPage() );
delay(100);
}[/mw_shl_code]
|
|