第1步:成分:
電子產(chǎn)品
面包板
LCD *
2個(gè)50k電位器
150歐姆電阻
跳線,以及成噸的跳線!
如果您不想使用LCD,則必須使用串行監(jiān)視器。無論在哪里看到lcd.print,只需將其更改為Serial.print即可。而不是lcd.begin更改為Serial.begin(9600);。
步驟2:LCD
所以我要做的第一件事是去Arduino網(wǎng)站,看看如何連接我的LCD屏幕。我已附上原理圖。
要使LCD上的背光引腳(K)接地,并通過150歐姆電阻將LCD上的引腳(A)連接到Arduino上的引腳10。
了解如何使用此LCD屏幕,因此我在此提供了一些信息。 Arduino已經(jīng)為LCD提供了一個(gè)庫,因此我們需要包含它。為此,我們可以在設(shè)置之前輸入代碼, #include 。現(xiàn)在包括我們的LCD庫。接下來要做的是告訴Arduino我們將哪些引腳連接到LCD。為此,請(qǐng)?jiān)谠O(shè)置功能 LiquidCrystal lcd(12,11,5,5,4,3,2);之前鍵入此代碼; 這些是Arduino通過PIN將數(shù)據(jù)發(fā)送到LCD的引腳。
所以這是到目前為止的草圖。 #include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
現(xiàn)在,讓我們學(xué)習(xí)一些有關(guān)使用LCD功能的知識(shí)。
lcd.begin(16,2);//這是用于設(shè)置列數(shù)和行數(shù)。如果您查看圖片2和3,則可以看到列的數(shù)量,而在圖片3中,可以看到行的數(shù)量。
lcd.print(“”);//這就是我們在屏幕上打印文本的方式。您的文字放在引號(hào)之間。引號(hào)是必需的。
lcd.clear();//這就是我們清除屏幕上所有文本的方式。
lcd.setCursor(0,1);//這就是我們將文本放在第二行的方式。
因此,請(qǐng)充分利用所有這些。連接屏幕,然后輸入此代碼。 #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()
{
}
如果將其復(fù)制,粘貼到草圖中并上傳到板上,則應(yīng)該會(huì)看到LCD亮起,并且出現(xiàn)文本“我很酷”。
:
我所看到的只是白色方塊,或者屏幕只是藍(lán)色(或您的LCD背光顏色)!
屏幕不亮!
解決方案:
將電位器一直轉(zhuǎn)到一側(cè),然后再轉(zhuǎn)到另一側(cè)。您應(yīng)該看到文本出現(xiàn)。如果是這樣,請(qǐng)慢慢調(diào)整電位器,直到文本清除為止
確保所有連接正確。
確保將[lcdbl]設(shè)置為[OUTPUT]
現(xiàn)在,讓我們在底部一行添加一些文本。如果您還記得,我們需要偏移光標(biāo)。通過鍵入lcd.setCursor(0,1);
請(qǐng)參閱修改后的代碼。 #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,看看會(huì)發(fā)生什么!
步驟3:連接第二個(gè)電位計(jì)
好吧,既然我們知道如何使用LCD,我們需要移動(dòng)一個(gè)。第一步是連接第二個(gè)電位器。我們將兩個(gè)外部引腳連接到5v和GND,將中間引腳連接到Arduino引腳A0。
現(xiàn)在,我們當(dāng)然需要使用Arduino進(jìn)行設(shè)置。方法如下:
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);
讓我們更詳細(xì)地檢查這一行
int sensorValue = AnalogRead(A0)-我們將單詞‘sensorValue’設(shè)置為相等從引腳A0到模擬讀數(shù)。
浮點(diǎn)英寸= sensorValue *(500/1023.0); -我們將“英寸”一詞設(shè)置為等于我們的新讀數(shù)(0-500);如果您想要鍋調(diào)整腳的數(shù)量,可以將“ inches”(英寸)改為“ feet”(英尺)。
基本上,所有這些操作都告訴Arduino我們在引腳A0處有一個(gè)模擬讀數(shù)(從0-1023開始)。下一行用于將讀數(shù)(0-1023)轉(zhuǎn)換為我們喜歡的值,在這種情況下為500。我們需要將其放入循環(huán)塊中。到目前為止,這就是我們的代碼。
#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);
}
注意,我已經(jīng)擺脫了測試文本。我真的不認(rèn)為我們會(huì)需要它:D。
步驟4:將底池值打印到LCD
我們需要告訴LCD打印一些東西,但是我們告訴它打印什么呢?我們要做的是鍵入 lcd.print( inches );現(xiàn)在為什么要英寸?好吧,請(qǐng)記住,我們將A0的模擬讀取命名為“英寸”,這就是我們應(yīng)該輸入的內(nèi)容。請(qǐng)勿使用引號(hào),因?yàn)橐?hào)僅用于放置文本。
#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);
}
因此,我們應(yīng)該將讀數(shù)打印到LCD上??!因此,上傳代碼!但是,哦,不!整個(gè)屏幕上只是一大堆數(shù)字!為什么會(huì)這樣呢?仔細(xì)研究我們的代碼,我們看到此函數(shù)在循環(huán)部分中,因此其中的任何操作都將不斷重復(fù)。如果您注意到,沒有延遲或中斷,那么它將瘋狂地打印到我們的LCD上。這就是我們要解決的難題。我們將使用一個(gè)簡單的lcd.clear函數(shù)來清除屏幕,然后添加一個(gè)小的延遲?,F(xiàn)在它將打印英寸數(shù),等待一秒鐘,清除屏幕,然后重復(fù)上述步驟直到下一次重置。因此,我們的新代碼將如下所示。
#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();
}
因此,現(xiàn)在要做的是打印數(shù)量AnalogRead值(英寸),將其在LCD上放置100毫秒,清除它,然后重新開始。現(xiàn)在,您應(yīng)該可以轉(zhuǎn)動(dòng)電位計(jì),并實(shí)時(shí)了解最新情況。現(xiàn)在將旋鈕調(diào)大,例如350。
第5步:數(shù)學(xué)!
現(xiàn)在是時(shí)候做一點(diǎn)數(shù)學(xué)了。我希望我的Arduino告訴我有幾英寸,然后讓屏幕變黑,然后告訴我有幾英尺。為了找出以英寸為單位的英尺,我們需要除以12。因此,這就是我們要設(shè)置代碼行的方式。
feet = (inches / 12);
如果我們想將英寸轉(zhuǎn)換為厘米,我們可以這樣做:
centimeters = (inches * 2.54);
所以我們的代碼看起來像這樣。注意,我已將草圖插入 int英尺; 如果您不這樣做,則會(huì)收到一條錯(cuò)誤消息,如圖所示。您必須定義什么是“腳”。下面的代碼應(yīng)執(zhí)行以下操作:
顯示英寸數(shù),其后帶有單詞“ inchs”。
清除屏幕
在其后顯示帶有“ foot”字樣的英尺數(shù)。
如果像我一樣將英寸保留為350英寸步驟4,每英尺的數(shù)量應(yīng)為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:更多編程。 。
到目前為止,草圖的問題是無法通過實(shí)時(shí)更新更改英寸。上面的代碼只會(huì)打印一次英寸數(shù)?,F(xiàn)在,這對(duì)于英尺的數(shù)量或我們要轉(zhuǎn)換的尺寸都可以,但是對(duì)于我們的變量,這太可怕了!我們甚至無法選擇想要的英寸數(shù)!為了解決這個(gè)問題,我使用了一個(gè)小程序?qū)?shù)據(jù)(英寸)打印到LCD上并將其清除了50次,這使我根據(jù)鍋的轉(zhuǎn)動(dòng)次數(shù)有了一個(gè)相當(dāng)不錯(cuò)的數(shù)字變化。下面的代碼將塊之間的所有代碼重復(fù)50次,并且將延遲設(shè)置為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();
}
在上面的草圖中,僅看到一次英寸數(shù),它將重復(fù)多次,使您可以在更新時(shí)看到它(與轉(zhuǎn)動(dòng)第二個(gè)底池一致)。
上傳以下代碼應(yīng)執(zhí)行以下操作:
“歡迎使用單位轉(zhuǎn)換器!”
“調(diào)整旋鈕”,使其下方具有英寸數(shù)。這將顯示5秒鐘。開鍋!
打印英寸數(shù)
清除
打印英寸數(shù)
返回“調(diào)整旋鈕”
#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:添加更多數(shù)學(xué)!
這是Arduino將英寸的長度轉(zhuǎn)換為英尺,碼,厘米和米。然后返回“調(diào)整旋鈕”。繼續(xù)閱讀整個(gè)代碼,并確保您理解所有內(nèi)容。
我將英尺,碼,厘米和米的換算值添加了。不要忘記在草圖的開頭定義這些內(nèi)容。
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);
}
以下是上述草圖的概述:
啟動(dòng)時(shí),“歡迎使用單位轉(zhuǎn)換器”
“調(diào)整旋鈕”,其下方的英寸數(shù)。轉(zhuǎn)動(dòng)旋鈕!
顯示英寸數(shù)
顯示英尺數(shù)
顯示厘米數(shù)
顯示數(shù)量或米
責(zé)任編輯:wv
-
轉(zhuǎn)換器
+關(guān)注
關(guān)注
27文章
9038瀏覽量
151679 -
Arduino
+關(guān)注
關(guān)注
190文章
6497瀏覽量
192023
發(fā)布評(píng)論請(qǐng)先 登錄

USB轉(zhuǎn)485/422轉(zhuǎn)換器拆解


變頻變壓器415V60HZ轉(zhuǎn)220V50HZ【山東科嘉電氣-變頻變壓電源轉(zhuǎn)換器】
不同類型ACDC轉(zhuǎn)換器優(yōu)缺點(diǎn) ACDC轉(zhuǎn)換器負(fù)載能力分析
HDMI接口轉(zhuǎn)換器的使用技巧

禾潤一級(jí)代理HT7179高功率異步升壓轉(zhuǎn)換器2.7V至25V寬輸入電壓15A開關(guān)電流能力#異步升壓轉(zhuǎn)換器
同軸轉(zhuǎn)換器為什么容易壞 同軸轉(zhuǎn)換器對(duì)音質(zhì)的影響
什么是DC/DC轉(zhuǎn)換器?
同軸轉(zhuǎn)換器怎么用
根據(jù)標(biāo)準(zhǔn)正降壓轉(zhuǎn)換器設(shè)計(jì)負(fù)升壓轉(zhuǎn)換器

轉(zhuǎn)換器供電和不供電什么區(qū)別
同步降壓轉(zhuǎn)換器的工作頻率是什么
光電型波長轉(zhuǎn)換器和全光型波長轉(zhuǎn)換器的區(qū)別

評(píng)論