GND => GND
VCC => 5V
SDA => A4
SCL => A5
D2 => 自復位按鈕 ==> GND
D3 => 自復位按鈕 ==> GND
#include <Wire.h>// Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2,1,0,4,5, 6,7, 3, POSITIVE);// Set the LCD I2C address
int pos[] = {0,0};
char *s = "Page 1", *t = "Page 2";
int pgCnt = 25;
#define DEBOUNCE_DELAY 200 //解決物理按鈕多次切換問題
static unsigned long lastDebounceTime;
void setup(){
Serial.begin(9600);
pos[0] = 0;
pos[1] = 0;
bgBlink();
lcd.backlight();
lcd.setCursor(0,0);
pinMode(3, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
attachInterrupt(0, pgCntPlus, FALLING);
attachInterrupt(1, pgCntMinus, FALLING);
}
void loop(){
showMsg(pos, pgCnt);
}
void pgCntPlus(){
unsigned long currentTime = millis();
if((currentTime - lastDebounceTime) > DEBOUNCE_DELAY){
lastDebounceTime = currentTime;
pgCnt++;
}
}
void pgCntMinus(){
unsigned long currentTime = millis();
if((currentTime - lastDebounceTime) > DEBOUNCE_DELAY){
lastDebounceTime = currentTime;
pgCnt--;
}
}
void bgBlink(){
lcd.begin(16,2);// initialize the lcd for 20 chars 4 lines and turn on backlight
for(int i = 0; i< 3; i++){
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
}
void showMsg(int *pos, int arrt){
//(col,row)
lcd.setCursor(pos[0],pos[1]);
lcd.print(pgCnt);
}
void showMsg(int *pos, char *c){
lcd.setCursor(pos[0],pos[1]);
while(*c){
lcd.print(*c++);
}
}
//字串相加(String Concatenation)
//將"abc"加上"def"變成"abcdef"
void strcat(char *s1, char *s2) {
int i, j;
for (i = strlen(s1), j = 0; s1[i] = s2[j]; i++, j++)
;
}
//字串長度
int strlen(char s[]) {
int i;
for (i = 0; s[i] != 0; i++)
;
return i;
}