您的位置:首页 > 编程语言

led灯闪烁代码_如何设置LED灯并使其通过代码闪烁

2020-08-21 09:56 756 查看

led灯闪烁代码

Coding an LED Light is introductory project that shows you how software and hardware interact with each other. It's a simple project you can complete in a weekend that'll help you learn some basic aspects of hardware.

对LED灯进行编码是一个介绍性项目,向您展示软件和硬件之间如何交互。 这是一个简单的项目,您可以在周末完成,这将帮助您学习硬件的一些基本方面。

By the end of the project, you will code your own LED light, have the knowledge to manipulate the LED to on/off in intervals you choose, and learn basic principles of hardware.

在项目结束时,您将编写自己的LED灯,具有在选择的时间间隔内操作LED开/关的知识,并学习硬件的基本原理。

The Elegoo Uno starter kit has all the hardware as well as instructions you need to make a simple LED Light. The LED Light is the first project offered with their kit.

Elegoo Uno入门套件具有所有硬件以及制作简单LED灯所需的说明。 LED灯是其套件提供的第一个项目。

Elegoo Uno comes with many other projects and takes you through from beginner to advanced projects. Each project in the box advances your skills in a simple, easy to follow way.

Elegoo Uno附带了许多其他项目,并带您从初学者到高级项目。 框中的每个项目都以简单,易于遵循的方式提高了您的技能。

您需要的组件 (Components You'll Need)

Elegoo Uno R3 (Elegoo Uno R3)

The Elogoo Uno R3 is a microcontroller board. Microcontrollers are embedded inside devices to control the actions and features of a product. They are compact integrated circuits designed to control operations.

Elogoo Uno R3是微控制器板。 微控制器嵌入设备内部,以控制产品的动作和功能。 它们是设计用于控制操作的紧凑型集成电路。

The microcontroller that is included in the Elogoo Uno R3 has 14 digital input/output pins, 6 analog inputs, a USB connection, a power jack, and a reset button. This board has everything you need to support the microcontroller. Simply plug in the USB Cable to turn the microcontroller on.

Elogoo Uno R3中包含的微控制器具有14个数字输入/输出引脚,6个模拟输入,USB连接,电源插Kong和复位按钮。 该评估板提供了支持微控制器所需的一切。 只需插入USB电缆即可打开微控制器。

USB电缆 (USB Cable)

You need a USB Cable to connect the Elegoo Uno R3 to your computer and turn it on. USB stands for Universal Serial Bus. The USB is used to connect your computer to devices such as digital cameras, printers, scanners, and external hard drives.

您需要USB电缆将Elegoo Uno R3连接到计算机并打开它。 USB代表通用串行总线。 USB用于将您的计算机连接到设备,例如数码相机,打印机,扫描仪和外部硬盘驱动器。

In our project, we will use a USB cable to connect our microcontroller to our computer.

在我们的项目中,我们将使用USB电缆将微控制器连接到计算机。

发光二极管 (LEDs)

LED stands for light emitting diode. It has a positive and negative lead. The longer side is the positive lead.

LED代表发光二极管。 它有正面和负面的线索。 较长的一面是正极。

如何组装零件 (How to assemble the components)

In this project, we are only going to make the LED blink.

在这个项目中,我们只会使LED闪烁。

First, we need to plug in the USB Cable to the board and then the computer.

首先,我们需要将USB电缆插入板子,然后再插入计算机。

Then we need to plug in the LED to GND (GND is the reference point in an electrical circuit from which voltages are measured, and is a common return path for electric current) and the 13 input on the board.

然后,我们需要将LED插入GND(GND是电路中的基准点,从中可以测量电压,并且是电流的通用返回路径)和板上的13个输入。

使LED闪烁的代码: (Code to make the LED flash on/off:)

After the microcontroller board is plugged into the computer and the LED is on the board itself, we need to write some simple code to make the LED blink.

将微控制器板插入计算机并且LED本身就在板之后,我们需要编写一些简单的代码来使LED闪烁。

// the setup function runs once when you press reset or power the board

void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on
delay(1000);                       // wait for a second
digitalWrite(LED_BUILTIN, LOW);    // turn the LED off
delay(1000);                       // wait for a second
}

The code above basically turns the LED on for 1 second and then turns it off for one second.

上面的代码基本上将LED点亮1秒钟,然后将其熄灭1秒钟。

This function is in a continuous loop. The

digitalWrite
is a function that takes in 2 parameters,
LED_BUILTIN
and
HIGH || LOW
. The loop basically takes in the LED, and then turns the volt to
HIGH
which turns it on. Then after 1 second it turns the same LED off by turning the volt to
LOW
.

此功能处于连续循环中。

digitalWrite
是一个带有两个参数的函数,
LED_BUILTIN
HIGH || LOW
HIGH || LOW
。 环路基本上接收LED,然后将电压转到
HIGH
,然后将其打开。 然后,在1秒钟后,通过将电压转到
LOW
来关闭同一LED。

这是最终产品: (Here's the Final Product:)

The goal of this little LED Light Coding project was to introduce you to elementary principles of how hardware and software can be combined. I hope you enjoyed it!

这个小巧的LED Light Coding项目的目的是向您介绍如何组合硬件和软件的基本原理。 我希望你喜欢它!

翻译自: https://www.freecodecamp.org/news/code-behind-an-led-light/

led灯闪烁代码

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐