Arduino开发之Blink LED

环境搭建:
1. Arduino UNO R3开发板,


2. Arduino IDE。
目前最新版是1.8.3。可以在 https://www.arduino.cc/en/Main/Software下载并安装。
安装好之后,桌面会有如下图标。

示例开发:
1.连接设备。
本例中我们以DFR0021-R LED为例,基于Arduino Uno R3和Arduino IDE开发。
DFR0021-R的引脚和Arduino Uno开发板的连接方式如下,
DFR0021-RArduino Uno R3
VCC3.3V
GNDGND
信号引脚数字信号引脚6
2. 编码。
连接好之后,用数据线连接Arduino开发板和电脑。同时打开Arduino IDE。输入下述代码。
const int ledPin = 6;// the number of the LED pin

// Variables will change :
int ledState = LOW; // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated

// constants won't change :
const long interval = 1000; // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);

Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}

void loop() {
// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}

// set the LED with the ledState of the variable:
Serial.println(ledState);
digitalWrite(ledPin, ledState);
}
}
然后保存文件。

选择Arduino Uno开发板。

编译上传大到开发板。

3.运行。
选择COM口信息,

然后选择端口监视工具,查看程序运行信息。

串口监视信息,

上面的数据就是Red LED 返回当前状态(1表示开,0表示关)。

整体运行界面:

以下是几种使用Arduino板实现闪灯(blink)功能的代码示例: ### 示例一 ```cpp // 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 (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } ``` 此代码可在Arduino开发环境的example中直接找到,打开后即可在开发板上看到闪灯效果 [^3]。 ### 示例二 ```cpp // 2022.11.12 // Author:Qiu #define LED_L 13 // 引脚号 #define SPEED 1000 // 控制闪烁间隔1000ms // 参数初始化,仅执行一次 void setup() { // 初始化数字引脚13作为输出 pinMode(LED_L, OUTPUT); } // 主循环 void loop() { digitalWrite(LED_L, HIGH); // 向13引脚写入高电平 delay(SPEED); // 等待 digitalWrite(LED_L, LOW); // 向13引脚写入低电平 delay(SPEED); // 等待 } ``` 编译成功后,上传至板子中,实验现象为LED灯以1Hz的频率闪烁。同样的实现也可以在Arduino自带的示例中找到,路径为:文件 - 示例 - Basics - Blink [^4]。 ### 示例三 ```cpp /* 闪烁 打开LED1秒,再关上1秒,如此往复 这个例程可以公开 */ // 大多数Arduino 的Pin 13 上已经有一个 LED // 起个名字: int led = 13 ; // setup过程只运行一次: void setup ( ) { // 把这个数字引脚初始化为输出引脚 pinMode (led , OUTPUT ) ; } // loop过程永远循环 void loop ( ) { digitalWrite (led , HIGH ) ; // 打开LED (电压为HIGH) delay ( 1000 ) ; // 等1秒 digitalWrite (led , LOW ) ; // 降低电压关上LED delay ( 1000 ) ; // 等一秒 } ``` 该代码实现了LED灯打开1秒,再关上1秒,如此往复闪烁的功能 [^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值