第1步:成分:
面包板
LCD *
2個50k電位器
150歐姆電阻
跳線,以及成噸的跳線!
如果您不想使用LCD,則必須使用串行監視器。無論在哪里看到lcd.print,只需將其更改為Serial.print即可。而不是lcd.begin更改為Serial.begin(9600);。
步驟2:LCD
所以我要做的第一件事是去Arduino網站,看看如何連接我的LCD屏幕。我已附上原理圖。
要使LCD上的背光引腳(K)接地,并通過150歐姆電阻將LCD上的引腳(A)連接到Arduino上的引腳10。
了解如何使用此LCD屏幕,因此我在此提供了一些信息。 Arduino已經為LCD提供了一個庫,因此我們需要包含它。為此,我們可以在設置之前輸入代碼, #include 。現在包括我們的LCD庫。接下來要做的是告訴Arduino我們將哪些引腳連接到LCD。為此,請在設置功能 LiquidCrystal lcd(12,11,5,5,4,3,2);之前鍵入此代碼; 這些是Arduino通過PIN將數據發送到LCD的引腳。
所以這是到目前為止的草圖。 #include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
現在,讓我們學習一些有關使用LCD功能的知識。
lcd.begin(16,2);//這是用于設置列數和行數。如果您查看圖片2和3,則可以看到列的數量,而在圖片3中,可以看到行的數量。
lcd.print(“”);//這就是我們在屏幕上打印文本的方式。您的文字放在引號之間。引號是必需的。
lcd.clear();//這就是我們清除屏幕上所有文本的方式。
lcd.setCursor(0,1);//這就是我們將文本放在第二行的方式。
因此,請充分利用所有這些。連接屏幕,然后輸入此代碼。 #include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10; // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10
void setup()
{
lcd.begin(16,2);
digitalWrite(lcdbl, HIGH); // turning on the backlight
pinMode(lcdbl, OUTPUT); // set pin 11 to output
lcd.print(“I‘m cool!”);
}
void loop()
{
}
如果將其復制,粘貼到草圖中并上傳到板上,則應該會看到LCD亮起,并且出現文本“我很酷”。
:
我所看到的只是白色方塊,或者屏幕只是藍色(或您的LCD背光顏色)!
屏幕不亮!
解決方案:
將電位器一直轉到一側,然后再轉到另一側。您應該看到文本出現。如果是這樣,請慢慢調整電位器,直到文本清除為止
確保所有連接正確。
確保將[lcdbl]設置為[OUTPUT]
現在,讓我們在底部一行添加一些文本。如果您還記得,我們需要偏移光標。通過鍵入lcd.setCursor(0,1);
請參閱修改后的代碼。 #include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10; // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10
void setup()
{
lcd.begin(16,2);
digitalWrite(lcdbl, HIGH);
pinMode(lcdbl, OUTPUT); // set pin 11 to output
lcd.print(“I’m cool!”);
lcd.setCursor(0,1); // set the text below to the second row
lcd.print(“In my dreams :(”);
}
void loop()
{
}
將此上傳到您的Arduino,看看會發生什么!
步驟3:連接第二個電位計
好吧,既然我們知道如何使用LCD,我們需要移動一個。第一步是連接第二個電位器。我們將兩個外部引腳連接到5v和GND,將中間引腳連接到Arduino引腳A0。
現在,我們當然需要使用Arduino進行設置。方法如下:
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (500 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a value (0-500);
讓我們更詳細地檢查這一行
int sensorValue = AnalogRead(A0)-我們將單詞‘sensorValue’設置為相等從引腳A0到模擬讀數。
浮點英寸= sensorValue *(500/1023.0); -我們將“英寸”一詞設置為等于我們的新讀數(0-500);如果您想要鍋調整腳的數量,可以將“ inches”(英寸)改為“ feet”(英尺)。
基本上,所有這些操作都告訴Arduino我們在引腳A0處有一個模擬讀數(從0-1023開始)。下一行用于將讀數(0-1023)轉換為我們喜歡的值,在這種情況下為500。我們需要將其放入循環塊中。到目前為止,這就是我們的代碼。
#include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10; // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10
void setup()
{
lcd.begin(16,2);
digitalWrite(lcdbl, HIGH);
pinMode(lcdbl, OUTPUT); // set pin 11 to output
}
void loop()
{
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (500 / 1023);
}
注意,我已經擺脫了測試文本。我真的不認為我們會需要它:D。
步驟4:將底池值打印到LCD
我們需要告訴LCD打印一些東西,但是我們告訴它打印什么呢?我們要做的是鍵入 lcd.print( inches );現在為什么要英寸?好吧,請記住,我們將A0的模擬讀取命名為“英寸”,這就是我們應該輸入的內容。請勿使用引號,因為引號僅用于放置文本。
#include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10; // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10
void setup()
{
lcd.begin(16,2);
digitalWrite(lcdbl, HIGH);
pinMode(lcdbl, OUTPUT); // set pin 11 to output
}
void loop()
{
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (500 / 1023.0);
lcd.print(inches);
}
因此,我們應該將讀數打印到LCD上??!因此,上傳代碼!但是,哦,不!整個屏幕上只是一大堆數字!為什么會這樣呢?仔細研究我們的代碼,我們看到此函數在循環部分中,因此其中的任何操作都將不斷重復。如果您注意到,沒有延遲或中斷,那么它將瘋狂地打印到我們的LCD上。這就是我們要解決的難題。我們將使用一個簡單的lcd.clear函數來清除屏幕,然后添加一個小的延遲。現在它將打印英寸數,等待一秒鐘,清除屏幕,然后重復上述步驟直到下一次重置。因此,我們的新代碼將如下所示。
#include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10; // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10
void setup()
{
lcd.begin(16,2);
digitalWrite(lcdbl, HIGH);
pinMode(lcdbl, OUTPUT); // set pin 11 to output
}
void loop()
{
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (500 / 1023.0);
lcd.print(inches);
delay(100);
lcd.clear();
}
因此,現在要做的是打印數量AnalogRead值(英寸),將其在LCD上放置100毫秒,清除它,然后重新開始?,F在,您應該可以轉動電位計,并實時了解最新情況。現在將旋鈕調大,例如350。
第5步:數學!
現在是時候做一點數學了。我希望我的Arduino告訴我有幾英寸,然后讓屏幕變黑,然后告訴我有幾英尺。為了找出以英寸為單位的英尺,我們需要除以12。因此,這就是我們要設置代碼行的方式。
feet = (inches / 12);
如果我們想將英寸轉換為厘米,我們可以這樣做:
centimeters = (inches * 2.54);
所以我們的代碼看起來像這樣。注意,我已將草圖插入 int英尺; 如果您不這樣做,則會收到一條錯誤消息,如圖所示。您必須定義什么是“腳”。下面的代碼應執行以下操作:
顯示英寸數,其后帶有單詞“ inchs”。
清除屏幕
在其后顯示帶有“ foot”字樣的英尺數。
如果像我一樣將英寸保留為350英寸步驟4,每英尺的數量應為29。
#include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10; // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10
int feet;
int wait = 3000; // assign the word “wait” to 3000 milliseconds
void setup()
{
lcd.begin(16,2);
digitalWrite(lcdbl, HIGH);
pinMode(lcdbl, OUTPUT); // set pin 11 to output
}
void loop()
{
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (500 / 1023.0);
delay(1000);
lcd.print(inches);
lcd.print(“ inches:”);
delay(wait);
lcd.clear();
delay(1000);
feet = (inches / 12); // conversion from feet to inches Here this is telling Arduino that
// feet is equal to inches divided by 12
lcd.print(feet);
lcd.print(“ feet:”);
delay(wait);
lcd.clear();
delay(1000);
}
步驟6:更多編程。 。
到目前為止,草圖的問題是無法通過實時更新更改英寸。上面的代碼只會打印一次英寸數?,F在,這對于英尺的數量或我們要轉換的尺寸都可以,但是對于我們的變量,這太可怕了!我們甚至無法選擇想要的英寸數!為了解決這個問題,我使用了一個小程序將數據(英寸)打印到LCD上并將其清除了50次,這使我根據鍋的轉動次數有了一個相當不錯的數字變化。下面的代碼將塊之間的所有代碼重復50次,并且將延遲設置為100,即大約5秒(50 * 100)。
for(int i=0; i《50; ++i)
{
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (500 / 1023.0);
lcd.print(“Adjust the knob”);
lcd.setCursor(0,1);
lcd.print(inches);
delay(100);
lcd.clear();
}
在上面的草圖中,僅看到一次英寸數,它將重復多次,使您可以在更新時看到它(與轉動第二個底池一致)。
上傳以下代碼應執行以下操作:
“歡迎使用單位轉換器!”
“調整旋鈕”,使其下方具有英寸數。這將顯示5秒鐘。開鍋!
打印英寸數
清除
打印英寸數
返回“調整旋鈕”
#include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10; // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10
int feet;
int wait = 3000; // assign the word “wait” to 3000 milliseconds
void setup()
{
lcd.begin(16,2);
digitalWrite(lcdbl, HIGH);
pinMode(lcdbl, OUTPUT); // set pin 11 to output
lcd.print(“Welcome to the”);
lcd.setCursor(0,1); // the cursor is like the rows on your LCD. After this, it will print
// the text on the bottom line of your LCD screen.
lcd.print(“unit converter!”);
delay(wait); /* if you look here, intead of the usual time in milliseconds
it says “wait”。 If we look what “wait” is, at the beginning of the code,
we see that “wait” is assigned to 3000 milliseconds, so whenever I type in
delay(wait); it wil have a delay of 3000 milliseconds, or 3 seconds. */
lcd.clear();
delay(1000);
}
void loop()
{
for(int i=0; i《50; ++i)
{
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (500 / 1023.0);
lcd.print(“Adjust the knob”);
lcd.setCursor(0,1);
lcd.print(inches);
delay(100);
lcd.clear();
}
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (500 / 1023.0);
delay(1000);
lcd.print(inches);
lcd.print(“ inches:”);
delay(wait);
lcd.clear();
delay(1000);
feet = (inches / 12); // conversion from feet to inches Here this is telling Arduino that
// feet is equal to inches divided by 12
lcd.print(feet);
lcd.print(“ feet:”);
delay(wait);
lcd.clear();
delay(1000);
}
步驟7:添加更多數學!
這是Arduino將英寸的長度轉換為英尺,碼,厘米和米。然后返回“調整旋鈕”。繼續閱讀整個代碼,并確保您理解所有內容。
我將英尺,碼,厘米和米的換算值添加了。不要忘記在草圖的開頭定義這些內容。
yards = (feet / 3);
centimeters = (inches * 2.54);
meters = (centimeters * 100);
Iint feet;
int yards;
long inches;
long centimeters;
long meters;
int lcdbl = 10; // set the backlight pin to pin 10
int wait = 3000; // assign the word “wait” to 3000 milliseconds
#include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
pinMode(lcdbl, OUTPUT); // setting the lcdbl (lcd backlight) pin to an OUPUT
analogWrite(lcdbl, 255); // set the backlight brightness to on
lcd.print(“Welcome to the”);
lcd.setCursor(0,1); // the cursor is like the rows on your LCD. After this, it will print
// the text on the bottom line of your LCD screen.
lcd.print(“unit converter!”);
delay(wait); /* if you look here, intead of the usual time in milliseconds
it says “wait”。 If we look what “wait” is, at the beginning of the code,
we see that “wait” is assigned to 3000 milliseconds, so whenever I type in
delay(wait); it wil have a delay of 3000 milliseconds, or 3 seconds. */
lcd.clear();
delay(1000);
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (100 / 1023.0); // Convert the analog reading
}
// the loop routine runs over and over again forever:
void loop()
{
for(int i=0; i《50; ++i)
{
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (500 / 1023.0);
lcd.print(“Adjust the knob”);
lcd.setCursor(0,1);
lcd.print(inches);
delay(100);
lcd.clear();
}
int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (500 / 1023.0);
// for feet
lcd.print(inches);
lcd.print(“ inches:”);
delay(wait);
lcd.clear();
delay(1000);
// for feet
feet = (inches / 12); // conversion from feet to inches Here this is telling Arduino that
// inches is equal to feet times 12
lcd.print(feet);
lcd.print(“ feet:”);
delay(wait);
lcd.clear();
delay(1000);
yards = (feet / 3); // conversion to yards
lcd.print(yards);
lcd.print(“ yards:”);
delay(wait);
lcd.clear();
delay(1000);
centimeters = (inches * 2.54); // conversion to centimeters
lcd.print(centimeters);
lcd.setCursor(0,1);
lcd.print(“centimeters”);
delay(wait);
lcd.clear();
delay(1000);
meters = (centimeters / 100); // conversion to meters
lcd.print(meters);
lcd.print(“ meters”);
delay(wait);
lcd.clear();
delay(1000);
}
以下是上述草圖的概述:
啟動時,“歡迎使用單位轉換器”
“調整旋鈕”,其下方的英寸數。轉動旋鈕!
顯示英寸數
顯示英尺數
顯示厘米數
顯示數量或米
責任編輯:wv
-
轉換器
+關注
關注
27文章
8763瀏覽量
148435 -
Arduino
+關注
關注
188文章
6478瀏覽量
188287
發布評論請先 登錄
相關推薦
不同類型ACDC轉換器優缺點 ACDC轉換器負載能力分析
HDMI接口轉換器的使用技巧

同軸轉換器為什么容易壞 同軸轉換器對音質的影響
什么是DC/DC轉換器?
同軸轉換器怎么用
轉換器供電和不供電什么區別
同步降壓轉換器的工作頻率是什么
光電型波長轉換器和全光型波長轉換器的區別



評論