材料:Arduino UNO3 x1、LCD 1602+I2C顯示器 x1、杜邦線
下載後解壓所至Arduino IDE安裝路徑下的libraries\目錄下即可。
佈線圖:
佈線圖:
線路圖:
#include <Wire.h> // Comes with Arduino IDE #include <LiquidCrystal_I2C.h> //設定LCD位址 LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address String str_Line_1 = "Hello, world!"; String str_Line_2 = "Have a nice day to you"; //整體文字閃爍功能函式 void blinkString(int num, int second){ for(int i = 1; i <= num; i++){ lcd.noDisplay(); delay(second); lcd.display(); delay(second); } return; } //向左向右逐字顯示及清除功能函式 void byCharDisplay(int turns){ for(int x=1; x<=turns; x++){ for (int i = 0; i < 16; i++){ //顯示行1內容 lcd.setCursor(i, 0); lcd.print(str_Line_1.substring(i, i+1)); //顯示行2內容 lcd.setCursor(16-i-1, 1); lcd.print(str_Line_2.substring(15-i, 16)); Serial.println(16-i-1); delay(80); } delay(1000); for (int i = 16; i > 0; i--){ //清除行1內容 lcd.setCursor(i-1, 0); lcd.print(" "); //清除行2內容 lcd.setCursor(16-i, 1); lcd.print(" "); delay(80); } delay(700); } } void setup(){ Serial.begin(9600); // 開啟LCD背光: lcd.begin(16, 2); } void loop(){ /* ------ 字串閃爍 ------*/ lcd.setCursor(0, 0); lcd.print(str_Line_1.substring(0,16)); lcd.setCursor(0, 1); lcd.print(str_Line_2.substring(0,16)); delay(1000); blinkString(3, 350); delay(1000); /* ------ L1向左、L2向右移動 ------*/ int move_length = max(str_Line_1.length(), 16); for (int i = 0; i < move_length+1; i++){ lcd.setCursor(0, 0); //防止Line1字串超長折行 lcd.print(str_Line_1.substring(i, i+16)); //防止Line2字串超長折行 if(i >= 16){ lcd.setCursor(16, 1); }else{ lcd.setCursor(i, 1); } lcd.print(str_Line_2.substring(0, 16)); delay(300); lcd.clear(); } /* ------ L1向右、L2向左移動顯示到位置(0,0) ------*/ for (int i = str_Line_1.length(); i >= 0; i--){ lcd.clear(); lcd.setCursor(0, 0); lcd.print(str_Line_1.substring(i)); //防止Line2字串超長折行 if ( i > 16){ lcd.setCursor(16, 1); }else{ lcd.setCursor(i, 1); } lcd.print(str_Line_2.substring(0, 16)); delay(300); lcd.clear(); } /* ------ 由位置(0, 0),L2向右、L1向左移動並顯示到尾端 ------*/ move_length = max(str_Line_2.length(), 16); for (int i = 0; i < move_length; i++){ //防止Line1字串超長折行 if(i >= 16){ lcd.setCursor(16, 1); }else{ lcd.setCursor(i, 0); } lcd.print(str_Line_1.substring(0, 16)); lcd.setCursor(0, 1); lcd.print(str_Line_2.substring(i, i+16)); delay(300); lcd.clear(); } delay(1000); //特效1顯示2次 byCharDisplay(2); delay(1000); }
沒有留言:
張貼留言