0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Arduino单位转换器的制作

454398 来源:网络整理 作者:佚名 2019-11-08 14:15 次阅读

第1步:成分:

电子产品

Arduino

面包板

LCD *

2个50k电位器

150欧姆电阻

跳线,以及成吨的跳线!

如果您不想使用LCD,则必须使用串行监视器。无论在哪里看到lcd.print,只需将其更改为Serial.print即可。而不是lcd.begin更改为Serial.begin(9600);。

步骤2:LCD

Arduino单位转换器的制作

所以我要做的第一件事是去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毫秒,清除它,然后重新开始。现在,您应该可以转动电位计,并实时了解最新情况。现在将旋钮调大,例如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:更多编程。 。

到目前为止,草图的问题是无法通过实时更新更改英寸。上面的代码只会打印一次英寸数。现在,这对于英尺的数量或我们要转换的尺寸都可以,但是对于我们的变量,这太可怕了!我们甚至无法选择想要的英寸数!为了解决这个问题,我使用了一个小程序将数据(英寸)打印到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

    文章

    8768

    浏览量

    148509
  • Arduino
    +关注

    关注

    188

    文章

    6478

    浏览量

    188356
收藏 人收藏

    评论

    相关推荐

    不同类型ACDC转换器优缺点 ACDC转换器负载能力分析

    ACDC转换器是将交流电(AC)转换为直流电(DC)的设备,在电力电子领域具有广泛的应用。以下是不同类型ACDC转换器的优缺点以及ACDC转换器负载能力的分析。 一、不同类型ACDC
    的头像 发表于 12-09 10:53 777次阅读

    HDMI接口转换器的使用技巧

    HDMI接口转换器的类型 HDMI转VGA/DVI转换器 :用于将HDMI信号转换为VGA或DVI信号,适用于老式显示或投影仪。 HDMI转DisplayPort
    的头像 发表于 11-27 14:35 902次阅读

    同轴转换器为什么容易坏 同轴转换器对音质的影响

    同轴转换器为什么容易坏 同轴转换器容易坏的原因可以从多个方面来分析: 质量问题 :转换器的质量是直接影响其耐用性的关键因素。低质量的转换器往往采用较为廉价的材料和元器件,这些材料和元器
    的头像 发表于 10-06 14:16 1357次阅读

    什么是DC/DC转换器

    DC/DC转换器,顾名思义,是指将一种直流电压转换为另一种直流电压的装置。它广泛应用于需要电压变换的电子设备中,通过调整输出电压来满足不同设备的特定需求。DC/DC转换器通常被称为线性稳压
    发表于 09-29 15:26

    同轴转换器怎么用

    同轴转换器(Coaxial Converter)的使用方法和具体应用场景可能会因不同的设备和转换器型号而有所不同。但一般来说,同轴转换器主要用于将同轴电缆信号转换为其他类型的信号,如光
    的头像 发表于 09-09 09:07 1574次阅读

    根据标准正降压转换器设计负升压转换器

    电子发烧友网站提供《根据标准正降压转换器设计负升压转换器.pdf》资料免费下载
    发表于 08-26 10:01 0次下载
    根据标准正降压<b class='flag-5'>转换器</b>设计负升压<b class='flag-5'>转换器</b>

    转换器供电和不供电什么区别

    引言 在现代电子设备中,转换器是一种常见的电子元件,用于将一种形式的能量转换为另一种形式。转换器可以是直流-直流转换器、交流-直流转换器、直
    的头像 发表于 08-19 14:26 1013次阅读

    同步降压转换器的工作频率是什么

    同步降压转换器(Synchronous Buck Converter)的工作频率是指其内部开关元件(如MOSFET)的通断频率,也就是控制在控制电压转换过程中,开关元件在单位时间内开
    的头像 发表于 08-14 10:08 485次阅读

    光电型波长转换器和全光型波长转换器的区别

    光电型波长转换器和全光型波长转换器是光通信领域中两种重要的波长转换技术,它们在转换机理、性能特点、应用场景等方面存在显著差异。以下是对这两种波长转换
    的头像 发表于 08-09 15:08 1047次阅读

    迪龙电源12kW大功率车载DCDC转换器直流变换#DC/DC #DC-DC #直流转换器

    直流转换器
    迪龙新能源
    发布于 :2024年07月30日 09:17:47

    电源转换器的浪涌电流可能比稳态电流高很多倍

    电源转换器
    深圳崧皓电子
    发布于 :2024年06月14日 06:59:06

    提升汽车DC-DC转换器效率#车规级大电流电感VSBX系列

    DC转换器
    科达嘉电感
    发布于 :2024年05月14日 11:15:35

    单位双电源缓冲电压信号转换器2N7001T数据表

    电子发烧友网站提供《单位双电源缓冲电压信号转换器2N7001T数据表.pdf》资料免费下载
    发表于 05-09 11:14 0次下载
    <b class='flag-5'>单位</b>双电源缓冲电压信号<b class='flag-5'>转换器</b>2N7001T数据表

    并行ad转换器的特点有哪些

    并行AD转换器,也被称为闪存式AD转换器或闪速AD转换器,是一种能够同时处理多个模拟信号并将其转换为数字信号的设备。这种类型的AD转换器具有
    的头像 发表于 05-02 15:07 977次阅读
    并行ad<b class='flag-5'>转换器</b>的特点有哪些